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