LongRange.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.1.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 
024 import java.io.Serializable;
025 import java.util.stream.LongStream;
026 
027 /**
028  * Long range class.
029  *
030  * @implSpec
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 LongRange implements Serializable {
038 
039     private static final long serialVersionUID = 1L;
040 
041     private final long _min;
042     private final long _max;
043 
044     private LongRange(final long min, final long 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 long range.
057      *
058      @return the minimum value of the long range
059      */
060     public long getMin() {
061         return _min;
062     }
063 
064     /**
065      * Return the maximum value of the long range.
066      *
067      @return the maximum value of the long range
068      */
069     public long getMax() {
070         return _max;
071     }
072 
073     /**
074      * Returns a sequential ordered {@code LongStream} from {@link #getMin()}
075      * (inclusive) to {@link #getMax()} (exclusive) by an incremental step of
076      * {@code 1}.
077      <p>
078      * An equivalent sequence of increasing values can be produced sequentially
079      * using a {@code for} loop as follows:
080      <pre>{@code
081      * for (long i = range.getMin(); i < range.getMax(); ++i) {
082      *     ...
083      * }
084      * }</pre>
085      *
086      @since 3.4
087      *
088      @return a sequential {@link LongStream} for the range of {@code long}
089      *         elements
090      */
091     public LongStream stream() {
092         return LongStream.range(_min, _max);
093     }
094 
095     /**
096      * Create a new {@code LongRange} object with the given {@code min} and
097      * {@code max} values.
098      *
099      @param min the lower bound of the long range
100      @param max the upper bound of the long range
101      @return a new {@code LongRange} object
102      @throws IllegalArgumentException if {@code min > max}
103      */
104     public static LongRange of(final long min, final long max) {
105         return new LongRange(min, max);
106     }
107 
108     /**
109      * Return a new (half open) range, which contains only the given value:
110      * {@code [value, value + 1)}.
111      *
112      @since 4.0
113      *
114      @param value the value of the created (half open) integer range
115      @return a new (half open) range, which contains only the given value
116      */
117     public static LongRange of(final long value) {
118         return of(value, value + 1);
119     }
120 
121     @Override
122     public int hashCode() {
123         return (int)(_min + 31*_max);
124     }
125 
126     @Override
127     public boolean equals(final Object obj) {
128         return obj == this ||
129             obj instanceof LongRange &&
130             _min == ((LongRange)obj)._min &&
131             _max == ((LongRange)obj)._max;
132     }
133 
134     @Override
135     public String toString() {
136         return "[" + _min + ", " + _max + "]";
137     }
138 
139 }