View Javadoc
1   /**
2    * This file is part of Indicators.
3    *
4    * Indicators is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * Indicators is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with Indicators. If not, see <https://www.gnu.org/licenses/>.
16   */
17  package fr.inrae.agroclim.indicators.model.data;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import lombok.Getter;
24  
25  /**
26   * Resource is a data source for climate, soil or phenology.
27   * @param <T>
28   *            data type
29   */
30  public abstract class Resource<T extends Cloneable> extends DataLoadingListenerHandler implements Cloneable {
31      /**
32       * Default CSV separator.
33       */
34      public static final String DEFAULT_SEP = ";";
35  
36      /**
37       * UUID for Serialisable.
38       */
39      private static final long serialVersionUID = 7164381207421561734L;
40  
41      /**
42       * Daily data.
43       */
44      @Getter
45      private final List<T> data;
46  
47      /**
48       * Missing climatic variables, to check in aggregation indicators and
49       * criteria.
50       */
51      @Getter
52      private final List<String> missingVariables;
53  
54      /**
55       * Computed list of years.
56       */
57      @Getter
58      private final List<Integer> years;
59  
60      /**
61       * Constructor.
62       */
63      protected Resource() {
64          data = new ArrayList<>();
65          years = new ArrayList<>();
66          missingVariables = new ArrayList<>();
67      }
68  
69      /**
70       * @param year
71       *            year of daily data
72       */
73      protected final void addYear(final Integer year) {
74          if (!years.contains(year)) {
75              years.add(year);
76          }
77      }
78  
79      @Override
80      @SuppressWarnings("checkstyle:DesignForExtension")
81      public Resource<T> clone() throws CloneNotSupportedException {
82          @SuppressWarnings("unchecked")
83          final Resource<T> clone = (Resource<T>) super.clone();
84          clone.setMissingVariables(getMissingVariables());
85          clone.getYears().addAll(getYears());
86          return clone;
87      }
88  
89      /**
90       * @return true if the resource contains no daily data.
91       */
92      public final boolean isEmpty() {
93          return data.isEmpty();
94      }
95  
96      /**
97       * @param variables
98       *            Missing variables, to check in aggregation indicators and
99       *            criteria.
100      */
101     public final void setMissingVariables(final Collection<String> variables) {
102         missingVariables.clear();
103         missingVariables.addAll(variables);
104     }
105 }