001 /*
002 * Java Genetic Algorithm Library (jenetics-6.2.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.readInt;
025 import static io.jenetics.internal.util.SerialIO.writeInt;
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.IntStream;
034
035 /**
036 * Integer 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 IntRange implements Serializable {
046
047 private static final long serialVersionUID = 2L;
048
049 private final int _min;
050 private final int _max;
051
052 private IntRange(final int min, final int 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 integer range.
065 *
066 * @return the minimum value of the integer range
067 */
068 public int min() {
069 return _min;
070 }
071
072 /**
073 * Return the maximum value of the integer range.
074 *
075 * @return the maximum value of the integer range
076 */
077 public int max() {
078 return _max;
079 }
080
081 /**
082 * Return the size of the {@code IntRange}: {@code max - min}.
083 *
084 * @since 3.9
085 *
086 * @return the size of the int range
087 */
088 public int size() {
089 return _max - _min;
090 }
091
092 /**
093 * Returns a sequential ordered {@code IntStream} from {@link #min()}
094 * (inclusive) to {@link #max()} (exclusive) by an incremental step of
095 * {@code 1}.
096 * <p>
097 * An equivalent sequence of increasing values can be produced sequentially
098 * using a {@code for} loop as follows:
099 * <pre>{@code
100 * for (int i = range.min(); i < range.max(); ++i) {
101 * ...
102 * }
103 * }</pre>
104 *
105 * @since 3.4
106 *
107 * @return a sequential {@link IntStream} for the range of {@code int}
108 * elements
109 */
110 public IntStream stream() {
111 return IntStream.range(_min, _max);
112 }
113
114 /**
115 * Create a new {@code IntRange} object with the given {@code min} and
116 * {@code max} values.
117 *
118 * @param min the lower bound of the integer range
119 * @param max the upper bound of the integer range
120 * @return a new {@code IntRange} object
121 * @throws IllegalArgumentException if {@code min > max}
122 */
123 public static IntRange of(final int min, final int max) {
124 return new IntRange(min, max);
125 }
126
127 /**
128 * Return a new (half open) range, which contains only the given value:
129 * {@code [value, value + 1)}.
130 *
131 * @since 4.0
132 *
133 * @param value the value of the created (half open) integer range
134 * @return a new (half open) range, which contains only the given value
135 */
136 public static IntRange of(final int value) {
137 return of(value, value + 1);
138 }
139
140 @Override
141 public int hashCode() {
142 return hash(_min, hash(_max));
143 }
144
145 @Override
146 public boolean equals(final Object obj) {
147 return obj == this ||
148 obj instanceof IntRange &&
149 _min == ((IntRange)obj)._min &&
150 _max == ((IntRange)obj)._max;
151 }
152
153 @Override
154 public String toString() {
155 return "[" + _min + ", " + _max + "]";
156 }
157
158
159 /* *************************************************************************
160 * Java object serialization
161 * ************************************************************************/
162
163 private Object writeReplace() {
164 return new Serial(Serial.INT_RANGE, this);
165 }
166
167 private void readObject(final ObjectInputStream stream)
168 throws InvalidObjectException
169 {
170 throw new InvalidObjectException("Serialization proxy required.");
171 }
172
173 void write(final DataOutput out) throws IOException {
174 writeInt(_min, out);
175 writeInt(_max, out);
176 }
177
178 static IntRange read(final DataInput in) throws IOException {
179 return of(readInt(in), readInt(in));
180 }
181
182 }
|