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.indicator.listener;
18
19 import java.util.Objects;
20
21 import fr.inrae.agroclim.indicators.model.Evaluation;
22 import fr.inrae.agroclim.indicators.model.indicator.CompositeIndicator;
23 import fr.inrae.agroclim.indicators.model.indicator.Indicator;
24 import lombok.Getter;
25
26 /**
27 * Event when something changed for an indicator.
28 *
29 * Last changed : $Date$
30 *
31 * @author $Author$
32 * @version $Revision$
33 */
34 public final class IndicatorEvent {
35 /**
36 * Type of event.
37 */
38 public enum Type {
39 /**
40 * An indicator has been added to the evaluation.
41 */
42 ADD(Indicator.class),
43 /**
44 * An aggregation is missing for the indicator.
45 */
46 AGGREGATION_MISSING(CompositeIndicator.class),
47 /**
48 * Properties of an indicator has been changed.
49 */
50 CHANGE(Indicator.class),
51 /**
52 * There is not any climatic indicator for the phase.
53 */
54 CLIMATIC_MISSING(CompositeIndicator.class),
55 /**
56 * The evaluation failed computing.
57 */
58 COMPUTE_FAILURE(Evaluation.class),
59 /**
60 * The evaluation start computing.
61 */
62 COMPUTE_START(Evaluation.class),
63 /**
64 * The evaluation succeeded computing.
65 */
66 COMPUTE_SUCCESS(Evaluation.class),
67 /**
68 * The indicator is not computable.
69 */
70 NOT_COMPUTABLE(Indicator.class),
71 /**
72 * The evaluation does not contain any phase.
73 */
74 PHASE_MISSING(Evaluation.class),
75 /**
76 * An indicator has been removed from the evaluation.
77 */
78 REMOVE(Indicator.class),
79 /**
80 * Value of indicator was updated.
81 */
82 UPDATED_VALUE(Indicator.class);
83
84 /**
85 * Class of indicator, to prevent wrong arguments.
86 */
87 @Getter
88 private final Class<? extends Indicator> clazz;
89
90 /**
91 * Constructor.
92 *
93 * @param value class of indicator
94 */
95 Type(final Class<? extends Indicator> value) {
96 clazz = value;
97 }
98
99 /**
100 * Build an IndicatorEvent.
101 *
102 * @param ind indicator source
103 * @return event
104 */
105 public IndicatorEvent event(final Indicator ind) {
106 return new IndicatorEvent(this, ind);
107 }
108
109 }
110
111 /**
112 * The IndicatorEvent.Type used to create this event.
113 */
114 @Getter
115 private final Type associatedType;
116
117 /**
118 * The indicator source for this event.
119 */
120 @Getter
121 private final Indicator source;
122
123 /**
124 * Constructor.
125 *
126 * @param type associated event type
127 * @param indicator indicator source
128 */
129 private IndicatorEvent(final Type type, final Indicator indicator) {
130 Objects.requireNonNull(type,
131 "IndicatorEvent.Type must not be null!");
132 Objects.requireNonNull(indicator,
133 "Indicator indicator must not be null!");
134 if (!type.getClazz().isAssignableFrom(indicator.getClass())) {
135 throw new IllegalArgumentException("Associated class for "
136 + type.name() + " is " + type.getClazz().getCanonicalName()
137 + ", but provided = "
138 + indicator.getClass().getCanonicalName());
139 }
140 associatedType = type;
141 source = indicator;
142 }
143
144 }