DoubleRange.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.2.0).
003  * Copyright (c) 2007-2018 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 
024 import java.io.Serializable;
025 
026 /**
027  * Double range class.
028  *
029  * @implNote
030  * This class is immutable and thread-safe.
031  *
032  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
033  @version 3.2
034  @since 3.2
035  */
036 public final class DoubleRange implements Serializable {
037 
038     private static final long serialVersionUID = 1L;
039 
040     private final double _min;
041     private final double _max;
042 
043     private DoubleRange(final double min, final double max) {
044         if (min > max) {
045             throw new IllegalArgumentException(format(
046                 "Min greater than max: %s > %s", min, max
047             ));
048         }
049 
050         _min = min;
051         _max = max;
052     }
053 
054     /**
055      * Return the minimum value of the double range.
056      *
057      @return the minimum value of the double range
058      */
059     public double getMin() {
060         return _min;
061     }
062 
063     /**
064      * Return the maximum value of the double range.
065      *
066      @return the maximum value of the double range
067      */
068     public double getMax() {
069         return _max;
070     }
071 
072     /**
073      * Create a new {@code DoubleRange} object with the given {@code min} and
074      * {@code max} values.
075      *
076      @param min the lower bound of the double range
077      @param max the upper bound of the double range
078      @return a new {@code DoubleRange} object
079      @throws IllegalArgumentException if {@code min > max}
080      */
081     public static DoubleRange of(final double min, final double max) {
082         return new DoubleRange(min, max);
083     }
084 
085     @Override
086     public int hashCode() {
087         int hash  = 17;
088         hash += 31*Double.hashCode(_max37;
089         hash += 31*Double.hashCode(_min37;
090         return hash;
091     }
092 
093     @Override
094     public boolean equals(final Object obj) {
095         return obj == this ||
096             obj instanceof DoubleRange &&
097             Double.compare(_min, ((DoubleRange)obj)._min== &&
098             Double.compare(_max, ((DoubleRange)obj)._max== 0;
099     }
100 
101     @Override
102     public String toString() {
103         return "[" + _min + ", " + _max + "]";
104     }
105 
106 }