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