001 /*
002 * Java Genetic Algorithm Library (jenetics-3.8.0).
003 * Copyright (c) 2007-2017 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@gmx.at)
019 */
020 package org.jenetics.util;
021
022 import static java.lang.String.format;
023
024 import java.io.Serializable;
025 import java.util.stream.IntStream;
026
027 /**
028 * Integer range class.
029 *
030 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
031 * @version 3.2
032 * @since 3.2
033 */
034 public final class IntRange implements Serializable {
035
036 private static final long serialVersionUID = 1L;
037
038 private final int _min;
039 private final int _max;
040
041 private IntRange(final int min, final int max) {
042 if (min > max) {
043 throw new IllegalArgumentException(format(
044 "Min greater than max: %s > %s", min, max
045 ));
046 }
047
048 _min = min;
049 _max = max;
050 }
051
052 /**
053 * Return the minimum value of the integer range.
054 *
055 * @return the minimum value of the integer range
056 */
057 public int getMin() {
058 return _min;
059 }
060
061 /**
062 * Return the maximum value of the integer range.
063 *
064 * @return the maximum value of the integer range
065 */
066 public int getMax() {
067 return _max;
068 }
069
070 /**
071 * Returns a sequential ordered {@code IntStream} from {@link #getMin()}
072 * (inclusive) to {@link #getMax()} (exclusive) by an incremental step of
073 * {@code 1}.
074 * <p>
075 * An equivalent sequence of increasing values can be produced sequentially
076 * using a {@code for} loop as follows:
077 * <pre>{@code
078 * for (int i = range.getMin(); i < range.getMax(); ++i) {
079 * ...
080 * }
081 * }</pre>
082 *
083 * @since 3.4
084 *
085 * @return a sequential {@link IntStream} for the range of {@code int}
086 * elements
087 */
088 public IntStream stream() {
089 return IntStream.range(_min, _max);
090 }
091
092 /**
093 * Create a new {@code IntRange} object with the given {@code min} and
094 * {@code max} values.
095 *
096 * @param min the lower bound of the integer range
097 * @param max the upper bound of the integer range
098 * @return a new {@code IntRange} object
099 * @throws IllegalArgumentException if {@code min > max}
100 */
101 public static IntRange of(final int min, final int max) {
102 return new IntRange(min, max);
103 }
104
105 @Override
106 public int hashCode() {
107 return _min + 31*_max;
108 }
109
110 @Override
111 public boolean equals(final Object other) {
112 return other instanceof IntRange &&
113 _min == ((IntRange)other)._min &&
114 _max == ((IntRange)other)._max;
115 }
116
117 @Override
118 public String toString() {
119 return "[" + _min + ", " + _max + "]";
120 }
121
122 }
|