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