1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
37
38
39
40
41
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
51
52 private static final long serialVersionUID = 9205643160821888602L;
53
54
55
56
57 @Setter
58 private List<Integer> years;
59
60
61
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
135 }
136
137 }