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