001/*
002 * Java Genetic Algorithm Library (jenetics-8.3.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;
023
024import java.io.Serializable;
025import java.util.Optional;
026
027/**
028 * Double range class.
029 *
030 * @param min the minimum value of the range
031 * @param max the maximum value of the range
032 *
033 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
034 * @version 8.3
035 * @since 3.2
036 */
037public record DoubleRange(double min, double max) implements Serializable {
038
039        /**
040         * Create a new {@code DoubleRange} object with the given {@code min} and
041         * {@code max} values.
042         *
043         * @param min the lower bound of the double range
044         * @param max the upper bound of the double range
045         * @throws IllegalArgumentException if {@code min > max} or one of the
046         *         parameters is not finite
047         */
048        public DoubleRange {
049                if (!Double.isFinite(min) || !Double.isFinite(max) || min > max) {
050                        throw new IllegalArgumentException(format(
051                                "Min greater than max: %s > %s", min, max
052                        ));
053                }
054        }
055
056        /**
057         * Checks whether the given {@code value} is within the range
058         * {@code [min, max)}.
059         *
060         * @since 8.0
061         *
062         * @param value the value to check
063         * @return {@code true} if the {@code value} is with the range
064         *         {@code [min, max)}, {@code false} otherwise
065         */
066        public boolean contains(final double value) {
067                return Double.isFinite(value) &&
068                        Double.compare(value, min) >= 0 &&
069                        Double.compare(value, max) < 0;
070        }
071
072        /**
073         * Return the intersection of {@code this} range with the {@code other}.
074         *
075         * @since 8.0
076         *
077         * @param other the intersection range or {@link Optional#empty()} if there
078         *        is none
079         * @return the range intersection
080         */
081        public Optional<DoubleRange> intersect(final DoubleRange other) {
082                if (Double.compare(max, other.min) <= 0 ||
083                        Double.compare(min, other.max) >= 0)
084                {
085                        return Optional.empty();
086                } else {
087                        return Optional.of(
088                                new DoubleRange(
089                                        Math.max(min, other.min),
090                                        Math.min(max, other.max)
091                                )
092                        );
093                }
094        }
095
096        /**
097         * Create a new {@code DoubleRange} object with the given {@code min} and
098         * {@code max} values.
099         *
100         * @param min the lower bound of the double range
101         * @param max the upper bound of the double range
102         * @return a new {@code DoubleRange} object
103         * @throws IllegalArgumentException if {@code min > max}
104         *
105         * @deprecated Class is a record now, and this factory method will be
106         *             removed in the next major version. Use
107         *             {@link #DoubleRange(double, double)} instead.
108         */
109        @Deprecated(since = "8.2", forRemoval = true)
110        public static DoubleRange of(final double min, final double max) {
111                return new DoubleRange(min, max);
112        }
113
114        @Override
115        public String toString() {
116                return "[" + min + ", " + max + "]";
117        }
118
119}