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