LongRange.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-6.3.0).
003  * Copyright (c) 2007-2021 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 import static io.jenetics.internal.util.SerialIO.readLong;
025 import static io.jenetics.internal.util.SerialIO.writeLong;
026 
027 import java.io.DataInput;
028 import java.io.DataOutput;
029 import java.io.IOException;
030 import java.io.InvalidObjectException;
031 import java.io.ObjectInputStream;
032 import java.io.Serializable;
033 import java.util.stream.LongStream;
034 
035 /**
036  * Long range class.
037  *
038  * @implNote
039  * This class is immutable and thread-safe.
040  *
041  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
042  @version 6.0
043  @since 3.2
044  */
045 public final /*record*/ class LongRange implements Serializable {
046 
047     private static final long serialVersionUID = 2L;
048 
049     private final long _min;
050     private final long _max;
051 
052     private LongRange(final long min, final long max) {
053         if (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 long range.
065      *
066      @return the minimum value of the long range
067      */
068     public long min() {
069         return _min;
070     }
071 
072     /**
073      * Return the maximum value of the long range.
074      *
075      @return the maximum value of the long range
076      */
077     public long max() {
078         return _max;
079     }
080 
081     /**
082      * Returns a sequential ordered {@code LongStream} from {@link #min()}
083      * (inclusive) to {@link #max()} (exclusive) by an incremental step of
084      * {@code 1}.
085      <p>
086      * An equivalent sequence of increasing values can be produced sequentially
087      * using a {@code for} loop as follows:
088      <pre>{@code
089      * for (long i = range.min(); i < range.max(); ++i) {
090      *     ...
091      * }
092      * }</pre>
093      *
094      @since 3.4
095      *
096      @return a sequential {@link LongStream} for the range of {@code long}
097      *         elements
098      */
099     public LongStream stream() {
100         return LongStream.range(_min, _max);
101     }
102 
103     /**
104      * Create a new {@code LongRange} object with the given {@code min} and
105      * {@code max} values.
106      *
107      @param min the lower bound of the long range
108      @param max the upper bound of the long range
109      @return a new {@code LongRange} object
110      @throws IllegalArgumentException if {@code min > max}
111      */
112     public static LongRange of(final long min, final long max) {
113         return new LongRange(min, max);
114     }
115 
116     /**
117      * Return a new (half open) range, which contains only the given value:
118      * {@code [value, value + 1)}.
119      *
120      @since 4.0
121      *
122      @param value the value of the created (half open) integer range
123      @return a new (half open) range, which contains only the given value
124      */
125     public static LongRange of(final long value) {
126         return of(value, value + 1);
127     }
128 
129     @Override
130     public int hashCode() {
131         return hash(_min, hash(_max, hash(getClass())));
132     }
133 
134     @Override
135     public boolean equals(final Object obj) {
136         return obj == this ||
137             obj instanceof LongRange &&
138             _min == ((LongRange)obj)._min &&
139             _max == ((LongRange)obj)._max;
140     }
141 
142     @Override
143     public String toString() {
144         return "[" + _min + ", " + _max + "]";
145     }
146 
147 
148     /* *************************************************************************
149      *  Java object serialization
150      * ************************************************************************/
151 
152     private Object writeReplace() {
153         return new Serial(Serial.LONG_RANGE, this);
154     }
155 
156     private void readObject(final ObjectInputStream stream)
157         throws InvalidObjectException
158     {
159         throw new InvalidObjectException("Serialization proxy required.");
160     }
161 
162     void write(final DataOutput outthrows IOException {
163         writeLong(_min, out);
164         writeLong(_max, out);
165     }
166 
167     static LongRange read(final DataInput inthrows IOException {
168         return of(readLong(in), readLong(in));
169     }
170 
171 }