DoubleRange.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-5.2.0).
003  * Copyright (c) 2007-2020 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019  */
020 package io.jenetics.util;
021 
022 import static java.lang.String.format;
023 import static io.jenetics.internal.util.Hashes.hash;
024 
025 import java.io.Serializable;
026 
027 /**
028  * Double range class.
029  *
030  * @implNote
031  * This class is immutable and thread-safe.
032  *
033  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
034  @version 5.2
035  @since 3.2
036  */
037 public final /*record*/ class DoubleRange implements Serializable {
038 
039     private static final long serialVersionUID = 1L;
040 
041     private final double _min;
042     private final double _max;
043 
044     private DoubleRange(final double min, final double max) {
045         if (min > max) {
046             throw new IllegalArgumentException(format(
047                 "Min greater than max: %s > %s", min, max
048             ));
049         }
050 
051         _min = min;
052         _max = max;
053     }
054 
055     /**
056      * Return the minimum value of the double range.
057      *
058      @return the minimum value of the double range
059      */
060     public double min() {
061         return _min;
062     }
063 
064     /**
065      * Return the maximum value of the double range.
066      *
067      @return the maximum value of the double range
068      */
069     public double max() {
070         return _max;
071     }
072 
073     /**
074      * Return the minimum value of the double range.
075      *
076      @return the minimum value of the double range
077      @deprecated Use {@link #min()} instead
078      */
079     @Deprecated
080     public double getMin() {
081         return _min;
082     }
083 
084     /**
085      * Return the maximum value of the double range.
086      *
087      @return the maximum value of the double range
088      @deprecated Use {@link #max()} instead
089      */
090     @Deprecated
091     public double getMax() {
092         return _max;
093     }
094 
095     /**
096      * Create a new {@code DoubleRange} object with the given {@code min} and
097      * {@code max} values.
098      *
099      @param min the lower bound of the double range
100      @param max the upper bound of the double range
101      @return a new {@code DoubleRange} object
102      @throws IllegalArgumentException if {@code min > max}
103      */
104     public static DoubleRange of(final double min, final double max) {
105         return new DoubleRange(min, max);
106     }
107 
108     @Override
109     public int hashCode() {
110         return hash(_min, hash(_max));
111     }
112 
113     @Override
114     public boolean equals(final Object obj) {
115         return obj == this ||
116             obj instanceof DoubleRange &&
117             Double.compare(_min, ((DoubleRange)obj)._min== &&
118             Double.compare(_max, ((DoubleRange)obj)._max== 0;
119     }
120 
121     @Override
122     public String toString() {
123         return "[" + _min + ", " + _max + "]";
124     }
125 
126 }