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.criteria;
18  
19  import java.beans.PropertyChangeListener;
20  import java.beans.PropertyChangeSupport;
21  import java.io.Serializable;
22  import java.util.List;
23  
24  import fr.inrae.agroclim.indicators.exception.IndicatorsException;
25  import fr.inrae.agroclim.indicators.model.Computable;
26  import fr.inrae.agroclim.indicators.model.HasParameters;
27  import fr.inrae.agroclim.indicators.model.Parameter;
28  import fr.inrae.agroclim.indicators.model.criteria.visitor.CriteriaVisitable;
29  import fr.inrae.agroclim.indicators.model.criteria.visitor.CriteriaVisitor;
30  import fr.inrae.agroclim.indicators.model.data.DailyData;
31  import fr.inrae.agroclim.indicators.model.data.UseVariables;
32  import jakarta.xml.bind.annotation.XmlAccessType;
33  import jakarta.xml.bind.annotation.XmlAccessorType;
34  import jakarta.xml.bind.annotation.XmlElement;
35  import jakarta.xml.bind.annotation.XmlElementWrapper;
36  import jakarta.xml.bind.annotation.XmlRootElement;
37  import jakarta.xml.bind.annotation.XmlTransient;
38  import lombok.AccessLevel;
39  import lombok.EqualsAndHashCode;
40  import lombok.Getter;
41  import lombok.Setter;
42  
43  /**
44   * Ancestor of all criteria.
45   *
46   * Last changed : $Date$
47   *
48   * @author jcufi
49   * @author $Author$
50   * @version $Revision$
51   */
52  @XmlAccessorType(XmlAccessType.FIELD)
53  @XmlRootElement
54  @EqualsAndHashCode(
55          callSuper = false,
56          of = {"parameters"}
57          )
58  public abstract class Criteria
59          implements Cloneable, CriteriaVisitable, Computable, HasParameters, Serializable, UseVariables {
60      /**
61       * UUID for Serializable.
62       */
63      private static final long serialVersionUID = -4284278102762199668L;
64  
65      /**
66       * Parameters for indicator criteria.
67       */
68      @XmlElementWrapper(name = "parameters")
69      @XmlElement(name = "parameter")
70      @Getter
71      @Setter
72      private List<Parameter> parameters;
73  
74      /**
75       * Handling PropertyChangeListeners.
76       */
77      @XmlTransient
78      @Getter(AccessLevel.PROTECTED)
79      private final PropertyChangeSupport propertySupport;
80  
81      /**
82       * Constructor.
83       */
84      protected Criteria() {
85          this.propertySupport = new PropertyChangeSupport(this);
86      }
87  
88      @Override
89      public final void accept(final CriteriaVisitor v) {
90          v.visit(this);
91      }
92  
93      /**
94       * @param listener listener
95       */
96      public final void addPropertyChangeListener(
97              final PropertyChangeListener listener) {
98          if (propertySupport.getPropertyChangeListeners() != null) {
99              for (final PropertyChangeListener l
100                     : propertySupport.getPropertyChangeListeners()) {
101                 if (listener.equals(l)) {
102                     return;
103                 }
104             }
105         }
106         propertySupport.addPropertyChangeListener(listener);
107     }
108 
109     @Override
110     public abstract Criteria clone();
111 
112     /**
113      * @param data
114      *            data to compare
115      * @return comparison result
116      * @throws IndicatorsException
117      *             exception during evaluation
118      */
119     public abstract boolean eval(DailyData data) throws IndicatorsException;
120 
121     /**
122      * @param indent
123      *            indentation string
124      * @return Structured string representation.
125      */
126     public abstract String toStringTree(String indent);
127 
128 }