View Javadoc
1   /*
2    * Copyright (C) 2021 INRAE AgroClim
3    *
4    * This file is part of Indicators.
5    *
6    * Indicators is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * Indicators is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with Indicators. If not, see <http://www.gnu.org/licenses/>.
18   */
19  package fr.inrae.agroclim.indicators.model.function.normalization;
20  
21  import java.io.Serializable;
22  
23  import jakarta.xml.bind.annotation.XmlRootElement;
24  import jakarta.xml.bind.annotation.XmlTransient;
25  import lombok.EqualsAndHashCode;
26  import lombok.Getter;
27  import lombok.Setter;
28  import lombok.ToString;
29  
30  /**
31   * Linear function on the related interval.
32   *
33   * Last change $Date$
34   *
35   * @author $Author$
36   * @version $Revision$
37   */
38  @XmlRootElement
39  @EqualsAndHashCode(of = {"min", "max", "linear"})
40  @ToString
41  public final class MultiLinearInterval implements Cloneable, Serializable {
42      /**
43       * UUID for Serializable.
44       */
45      private static final long serialVersionUID = 4872847709393688050L;
46  
47      /**
48       * Minimum value of the interval, included.
49       */
50      @Getter
51      @Setter
52      private Double min;
53      /**
54       * Maximum value of the interval, excluded.
55       */
56      @Getter
57      @Setter
58      private Double max;
59      /**
60       * Linear function to apply.
61       */
62      @Getter
63      @Setter
64      private Linear linear = new Linear();
65  
66      @Override
67      public MultiLinearInterval clone() {
68          final MultiLinearInterval clone = new MultiLinearInterval();
69          clone.min = this.min;
70          clone.max = this.max;
71          if (this.linear != null) {
72              clone.linear = linear.clone();
73          }
74          return clone;
75      }
76  
77      /**
78       * @return A in A.x + B.
79       */
80      @XmlTransient
81      public double getLinearA() {
82          return linear.getLinearA();
83      }
84  
85      /**
86       * @return B in A.x + B.
87       */
88      @XmlTransient
89      public double getLinearB() {
90          return linear.getLinearB();
91      }
92  
93      /**
94       * Check if the value is in the interval.
95       *
96       * @param value value to check
97       * @return true if the value is in the interval
98       */
99      public boolean matches(final double value) {
100         if (min == null) {
101             if (max != null) {
102                 return value < max;
103             }
104             return true;
105         }
106         if (max == null) {
107             return min <= value;
108         }
109         return min <= value && value < max;
110     }
111 
112     /**
113      * @param linearA A in A.x + B.
114      */
115     public void setLinearA(final double linearA) {
116         linear.setLinearA(linearA);
117     }
118 
119     /**
120      * @param linearB B in A.x + B.
121      */
122     public void setLinearB(final double linearB) {
123         linear.setLinearB(linearB);
124     }
125 
126 }