BrightSide Workbench Full Report + Source Code
AbstractOccurrenceAPI.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.occurrence;
20 
21 import java.util.List;
22 import java.util.stream.Collectors;
23 import org.turro.jpa.Dao;
24 import org.turro.jpa.DaoTransaction;
25 
30 public abstract class AbstractOccurrenceAPI<E> implements IOccurrenceAPI {
31 
32  private List<E> list;
33 
34  @Override
35  public long getCount() {
36  return list == null ? 0 : list.size();
37  }
38 
39  @Override
40  public boolean hasOccurrences(String value) {
41  list = getOccurrences(value).stream()
42  .filter(t -> getValue(t).equals(value)).collect(Collectors.toList());
43  return getCount() != 0;
44  }
45 
46  public List<E> getList() {
47  return list;
48  }
49 
50  @Override
51  public void changeOccurrences(String newValue) {
52  try(DaoTransaction transaction = new DaoTransaction(getDao())) {
53  for(E e : getList()) {
54  setValue(e, newValue);
55  transaction.saveObject(e);
56  }
57  }
58  }
59 
60  @Override
61  public void removeOccurrences() {
62  try(DaoTransaction transaction = new DaoTransaction(getDao())) {
63  for(E e : getList()) {
64  transaction.remove(e);
65  }
66  }
67  }
68 
69  /* Dao */
70 
71  private Dao _dao;
72 
73  protected Dao getDao() {
74  if(_dao == null) {
75  _dao = createDao();
76  }
77  return _dao;
78  }
79 
80  protected abstract Dao createDao();
81  protected abstract List<E> getOccurrences(String value);
82  protected abstract String getValue(E value);
83  protected abstract void setValue(E value, String newValue);
84 
85 }
abstract void setValue(E value, String newValue)
abstract List< E > getOccurrences(String value)