001 /*
002 * Java Genetic Algorithm Library (jenetics-5.0.0).
003 * Copyright (c) 2007-2019 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.LongStream;
027
028 /**
029 * Long 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 LongRange implements Serializable {
039
040 private static final long serialVersionUID = 1L;
041
042 private final long _min;
043 private final long _max;
044
045 private LongRange(final long min, final long 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 long range.
058 *
059 * @return the minimum value of the long range
060 */
061 public long getMin() {
062 return _min;
063 }
064
065 /**
066 * Return the maximum value of the long range.
067 *
068 * @return the maximum value of the long range
069 */
070 public long getMax() {
071 return _max;
072 }
073
074 /**
075 * Returns a sequential ordered {@code LongStream} from {@link #getMin()}
076 * (inclusive) to {@link #getMax()} (exclusive) by an incremental step of
077 * {@code 1}.
078 * <p>
079 * An equivalent sequence of increasing values can be produced sequentially
080 * using a {@code for} loop as follows:
081 * <pre>{@code
082 * for (long i = range.getMin(); i < range.getMax(); ++i) {
083 * ...
084 * }
085 * }</pre>
086 *
087 * @since 3.4
088 *
089 * @return a sequential {@link LongStream} for the range of {@code long}
090 * elements
091 */
092 public LongStream stream() {
093 return LongStream.range(_min, _max);
094 }
095
096 /**
097 * Create a new {@code LongRange} object with the given {@code min} and
098 * {@code max} values.
099 *
100 * @param min the lower bound of the long range
101 * @param max the upper bound of the long range
102 * @return a new {@code LongRange} object
103 * @throws IllegalArgumentException if {@code min > max}
104 */
105 public static LongRange of(final long min, final long max) {
106 return new LongRange(min, max);
107 }
108
109 /**
110 * Return a new (half open) range, which contains only the given value:
111 * {@code [value, value + 1)}.
112 *
113 * @since 4.0
114 *
115 * @param value the value of the created (half open) integer range
116 * @return a new (half open) range, which contains only the given value
117 */
118 public static LongRange of(final long value) {
119 return of(value, value + 1);
120 }
121
122 @Override
123 public int hashCode() {
124 return hash(_min, hash(_max, hash(getClass())));
125 }
126
127 @Override
128 public boolean equals(final Object obj) {
129 return obj == this ||
130 obj instanceof LongRange &&
131 _min == ((LongRange)obj)._min &&
132 _max == ((LongRange)obj)._max;
133 }
134
135 @Override
136 public String toString() {
137 return "[" + _min + ", " + _max + "]";
138 }
139
140 }
|