001 /*
002 * Java Genetic Algorithm Library (jenetics-4.3.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 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 3.2
036 * @since 3.2
037 */
038 public final 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 getMin() {
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 getMax() {
071 return _max;
072 }
073
074 /**
075 * Return the size of the {@code IntRange}: {@code max - min}.
076 *
077 * @since 3.9
078 *
079 * @return the size of the int range
080 */
081 public int size() {
082 return _max - _min;
083 }
084
085 /**
086 * Returns a sequential ordered {@code IntStream} from {@link #getMin()}
087 * (inclusive) to {@link #getMax()} (exclusive) by an incremental step of
088 * {@code 1}.
089 * <p>
090 * An equivalent sequence of increasing values can be produced sequentially
091 * using a {@code for} loop as follows:
092 * <pre>{@code
093 * for (int i = range.getMin(); i < range.getMax(); ++i) {
094 * ...
095 * }
096 * }</pre>
097 *
098 * @since 3.4
099 *
100 * @return a sequential {@link IntStream} for the range of {@code int}
101 * elements
102 */
103 public IntStream stream() {
104 return IntStream.range(_min, _max);
105 }
106
107 /**
108 * Create a new {@code IntRange} object with the given {@code min} and
109 * {@code max} values.
110 *
111 * @param min the lower bound of the integer range
112 * @param max the upper bound of the integer range
113 * @return a new {@code IntRange} object
114 * @throws IllegalArgumentException if {@code min > max}
115 */
116 public static IntRange of(final int min, final int max) {
117 return new IntRange(min, max);
118 }
119
120 /**
121 * Return a new (half open) range, which contains only the given value:
122 * {@code [value, value + 1)}.
123 *
124 * @since 4.0
125 *
126 * @param value the value of the created (half open) integer range
127 * @return a new (half open) range, which contains only the given value
128 */
129 public static IntRange of(final int value) {
130 return of(value, value + 1);
131 }
132
133 @Override
134 public int hashCode() {
135 return hash(_min, hash(_max, hash(getClass())));
136 }
137
138 @Override
139 public boolean equals(final Object obj) {
140 return obj == this ||
141 obj instanceof IntRange &&
142 _min == ((IntRange)obj)._min &&
143 _max == ((IntRange)obj)._max;
144 }
145
146 @Override
147 public String toString() {
148 return "[" + _min + ", " + _max + "]";
149 }
150
151 }
|