DoubleRange.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-6.3.0).
003  * Copyright (c) 2007-2021 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.DataInput;
026 import java.io.DataOutput;
027 import java.io.IOException;
028 import java.io.InvalidObjectException;
029 import java.io.ObjectInputStream;
030 import java.io.Serializable;
031 
032 /**
033  * Double range class.
034  *
035  * @implNote
036  * This class is immutable and thread-safe.
037  *
038  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
039  @version 6.0
040  @since 3.2
041  */
042 public final /*record*/ class DoubleRange implements Serializable {
043 
044     private static final long serialVersionUID = 2L;
045 
046     private final double _min;
047     private final double _max;
048 
049     private DoubleRange(final double min, final double max) {
050         if (min > max) {
051             throw new IllegalArgumentException(format(
052                 "Min greater than max: %s > %s", min, max
053             ));
054         }
055 
056         _min = min;
057         _max = max;
058     }
059 
060     /**
061      * Return the minimum value of the double range.
062      *
063      @return the minimum value of the double range
064      */
065     public double min() {
066         return _min;
067     }
068 
069     /**
070      * Return the maximum value of the double range.
071      *
072      @return the maximum value of the double range
073      */
074     public double max() {
075         return _max;
076     }
077 
078     /**
079      * Create a new {@code DoubleRange} object with the given {@code min} and
080      * {@code max} values.
081      *
082      @param min the lower bound of the double range
083      @param max the upper bound of the double range
084      @return a new {@code DoubleRange} object
085      @throws IllegalArgumentException if {@code min > max}
086      */
087     public static DoubleRange of(final double min, final double max) {
088         return new DoubleRange(min, max);
089     }
090 
091     @Override
092     public int hashCode() {
093         return hash(_min, hash(_max));
094     }
095 
096     @Override
097     public boolean equals(final Object obj) {
098         return obj == this ||
099             obj instanceof DoubleRange &&
100             Double.compare(_min, ((DoubleRange)obj)._min== &&
101             Double.compare(_max, ((DoubleRange)obj)._max== 0;
102     }
103 
104     @Override
105     public String toString() {
106         return "[" + _min + ", " + _max + "]";
107     }
108 
109 
110     /* *************************************************************************
111      *  Java object serialization
112      * ************************************************************************/
113 
114     private Object writeReplace() {
115         return new Serial(Serial.DOUBLE_RANGE, this);
116     }
117 
118     private void readObject(final ObjectInputStream stream)
119         throws InvalidObjectException
120     {
121         throw new InvalidObjectException("Serialization proxy required.");
122     }
123 
124     void write(final DataOutput outthrows IOException {
125         out.writeDouble(_min);
126         out.writeDouble(_max);
127     }
128 
129     static DoubleRange read(final DataInput inthrows IOException {
130         return of(in.readDouble(), in.readDouble());
131     }
132 
133 }