001/*
002 * Java Genetic Algorithm Library (jenetics-8.2.0).
003 * Copyright (c) 2007-2025 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 */
020package io.jenetics.util;
021
022import static java.lang.String.format;
023import static io.jenetics.internal.util.Hashes.hash;
024
025import java.io.DataInput;
026import java.io.DataOutput;
027import java.io.IOException;
028import java.io.InvalidObjectException;
029import java.io.ObjectInputStream;
030import java.io.Serial;
031import java.io.Serializable;
032import 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 */
044public 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 (!Double.isFinite(min) || !Double.isFinite(max) || 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.isFinite(value) &&
093                        Double.compare(value, _min) >= 0 &&
094                        Double.compare(value, _max) < 0;
095        }
096
097        /**
098         * Return the intersection of {@code this} range with the {@code other}.
099         *
100         * @since 8.0
101         *
102         * @param other the intersection range or {@link Optional#empty()} if there
103         *        is none
104         * @return the range intersection
105         */
106        public Optional<DoubleRange> intersect(final DoubleRange other) {
107                if (Double.compare(_max, other._min) <= 0 ||
108                        Double.compare(_min, other._max) >= 0)
109                {
110                        return Optional.empty();
111                } else {
112                        return Optional.of(
113                                DoubleRange.of(
114                                        Math.max(_min, other._min),
115                                        Math.min(_max, other._max)
116                                )
117                        );
118                }
119        }
120
121        /**
122         * Create a new {@code DoubleRange} object with the given {@code min} and
123         * {@code max} values.
124         *
125         * @param min the lower bound of the double range
126         * @param max the upper bound of the double range
127         * @return a new {@code DoubleRange} object
128         * @throws IllegalArgumentException if {@code min > max}
129         */
130        public static DoubleRange of(final double min, final double max) {
131                return new DoubleRange(min, max);
132        }
133
134        @Override
135        public int hashCode() {
136                return hash(_min, hash(_max));
137        }
138
139        @Override
140        public boolean equals(final Object obj) {
141                return obj instanceof DoubleRange other &&
142                        Double.compare(_min, other._min) == 0 &&
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 out) throws IOException {
169                out.writeDouble(_min);
170                out.writeDouble(_max);
171        }
172
173        static DoubleRange read(final DataInput in) throws IOException {
174                return of(in.readDouble(), in.readDouble());
175        }
176
177}