DoubleRange.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-8.0.0).
003  * Copyright (c) 2007-2024 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.Serial;
031 import java.io.Serializable;
032 import java.util.Optional;
033 
034 /**
035  * Double range class.
036  *
037  * @implNote
038  * This class is immutable and thread-safe.
039  *
040  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
041  @version 6.0
042  @since 3.2
043  */
044 public final /*record*/ class DoubleRange implements Serializable {
045 
046     @Serial
047     private static final long serialVersionUID = 2L;
048 
049     private final double _min;
050     private final double _max;
051 
052     private DoubleRange(final double min, final double max) {
053         if (min > max) {
054             throw new IllegalArgumentException(format(
055                 "Min greater than max: %s > %s", min, max
056             ));
057         }
058 
059         _min = min;
060         _max = max;
061     }
062 
063     /**
064      * Return the minimum value of the double range.
065      *
066      @return the minimum value of the double range
067      */
068     public double min() {
069         return _min;
070     }
071 
072     /**
073      * Return the maximum value of the double range.
074      *
075      @return the maximum value of the double range
076      */
077     public double max() {
078         return _max;
079     }
080 
081     /**
082      * Checks whether the given {@code value} is within the range
083      * {@code [min, max)}.
084      *
085      @since 8.0
086      *
087      @param value the value to check
088      @return {@code true} if the {@code value} is with the range
089      *         {@code [min, max)}, {@code false} otherwise
090      */
091     public boolean contains(final double value) {
092         return Double.compare(value, _min>= &&
093             Double.compare(value, _max0;
094     }
095 
096     /**
097      * Return the intersection of {@code this} range with the {@code other}.
098      *
099      @since 8.0
100      *
101      @param other the intersection range or {@link Optional#empty()} if there
102      *        is none
103      @return the range intersection
104      */
105     public Optional<DoubleRange> intersect(final DoubleRange other) {
106         if (Double.compare(_max, other._min<= ||
107             Double.compare(_min, other._max>= 0)
108         {
109             return Optional.empty();
110         else {
111             return Optional.of(
112                 DoubleRange.of(
113                     Math.max(_min, other._min),
114                     Math.min(_max, other._max)
115                 )
116             );
117         }
118     }
119 
120     /**
121      * Create a new {@code DoubleRange} object with the given {@code min} and
122      * {@code max} values.
123      *
124      @param min the lower bound of the double range
125      @param max the upper bound of the double range
126      @return a new {@code DoubleRange} object
127      @throws IllegalArgumentException if {@code min > max}
128      */
129     public static DoubleRange of(final double min, final double max) {
130         return new DoubleRange(min, max);
131     }
132 
133     @Override
134     public int hashCode() {
135         return hash(_min, hash(_max));
136     }
137 
138     @Override
139     public boolean equals(final Object obj) {
140         return obj == this ||
141             obj instanceof DoubleRange other &&
142             Double.compare(_min, other._min== &&
143             Double.compare(_max, other._max== 0;
144     }
145 
146     @Override
147     public String toString() {
148         return "[" + _min + ", " + _max + "]";
149     }
150 
151 
152     /* *************************************************************************
153      *  Java object serialization
154      * ************************************************************************/
155 
156     @Serial
157     private Object writeReplace() {
158         return new SerialProxy(SerialProxy.DOUBLE_RANGE, this);
159     }
160 
161     @Serial
162     private void readObject(final ObjectInputStream stream)
163         throws InvalidObjectException
164     {
165         throw new InvalidObjectException("Serialization proxy required.");
166     }
167 
168     void write(final DataOutput outthrows IOException {
169         out.writeDouble(_min);
170         out.writeDouble(_max);
171     }
172 
173     static DoubleRange read(final DataInput inthrows IOException {
174         return of(in.readDouble(), in.readDouble());
175     }
176 
177 }