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.phenology;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.Collections;
22  import java.util.HashMap;
23  import java.util.HashSet;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import fr.inrae.agroclim.indicators.model.TimeScale;
29  import fr.inrae.agroclim.indicators.model.data.DataLoadingListenerHandler;
30  import fr.inrae.agroclim.indicators.model.data.ResourcesLoader;
31  import fr.inrae.agroclim.indicators.model.data.Variable;
32  import lombok.EqualsAndHashCode;
33  import lombok.Setter;
34  
35  /**
36   * Builder to provide agroclimatic stages from DOY.
37   *
38   * Last changed : $Date$
39   *
40   * @author $Author$
41   * @version $Revision$
42   */
43  @EqualsAndHashCode(
44          callSuper = false,
45          of = {"doys", "years"}
46          )
47  public final class AnnualStageBuilder extends DataLoadingListenerHandler
48  implements ResourcesLoader<List<AnnualStageData>> {
49      /**
50       * UID for serialization.
51       */
52      private static final long serialVersionUID = 9205643160821888602L;
53  
54      /**
55       * List of years on which build stages.
56       */
57      @Setter
58      private List<Integer> years;
59  
60      /**
61       * Day of year for each stage to build.
62       */
63      @Setter
64      private Map<String, Integer> doys;
65  
66      @Override
67      public ResourcesLoader<List<AnnualStageData>> clone()
68              throws CloneNotSupportedException {
69          final AnnualStageBuilder clone = new AnnualStageBuilder();
70          if (years != null) {
71              clone.years = new ArrayList<>();
72              clone.years.addAll(years);
73          }
74          if (doys != null) {
75              clone.doys = new HashMap<>();
76              clone.doys.putAll(doys);
77          }
78          return clone;
79      }
80  
81      @Override
82      public Map<String, String> getConfigurationErrors() {
83          final Map<String, String> errors = new HashMap<>();
84          if (years == null) {
85              errors.put("phenology.years",
86                      "error.evaluation.phenology.years.missing");
87          } else if (years.isEmpty()) {
88              errors.put("phenology.years",
89                      "error.evaluation.phenology.years.empty");
90          }
91          if (doys == null) {
92              errors.put("phenology.doys",
93                      "error.evaluation.phenology.doys.missing");
94          } else if (doys.isEmpty()) {
95              errors.put("phenology.doys",
96                      "error.evaluation.phenology.doys.empty");
97          }
98          if (errors.isEmpty()) {
99              return null;
100         }
101         return errors;
102     }
103 
104     @Override
105     public Collection<String> getMissingVariables() {
106         throw new RuntimeException("Not implemented for phenology!");
107     }
108 
109     @Override
110     public Set<Variable> getVariables() {
111         return new HashSet<>();
112     }
113 
114     @Override
115     public List<AnnualStageData> load() {
116         final List<AnnualStageData> data = new ArrayList<>();
117         Collections.sort(years);
118         final List<String> stageNames = new ArrayList<>(doys.keySet());
119         Collections.sort(stageNames);
120         years.forEach(year -> {
121             final AnnualStageData annualStageData = new AnnualStageData();
122             annualStageData.setYear(year);
123             stageNames.forEach(name -> {
124                 final int doy = doys.get(name);
125                 annualStageData.add(name, doy);
126             });
127             data.add(annualStageData);
128         });
129         return data;
130     }
131 
132     @Override
133     public void setTimeScale(final TimeScale timeScale) {
134         // do nothing
135     }
136 
137 }