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