org.omnifaces.utils.math.Range Maven / Gradle / Ivy
package org.omnifaces.utils.math;
import static java.util.Comparator.naturalOrder;
import static java.util.Objects.requireNonNull;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.math.BigDecimal;
import java.util.Objects;
import org.omnifaces.utils.data.MutableRange;
/**
* An abstract base class for ranges of numbers.
*
* @deprecated This class has been replaced by the {@link org.omnifaces.utils.data.Range} and {@link MutableRange} interfaces, please use those instead
* @param the
*/
@Deprecated
public abstract class Range> extends org.omnifaces.utils.data.AbstractRange implements MutableRange, Serializable {
private static final long serialVersionUID = 1L;
private boolean minInclusive = true;
private boolean maxInclusive = true;
private N min;
private N max;
/**
* Creates a new range with the given min and max values.
*
* @param min
* the min value
* @param max
* the max value
* @param
* the generic type of the min and max values
*
* @return a new range
*
* @throws NullPointerException
* if both min and max are null
* @deprecated Please use {@link org.omnifaces.utils.data.Range#ofClosed(Comparable, Comparable)} or {@link MutableRange#ofClosed(Comparable,
* Comparable)} instead
*/
@SuppressWarnings("unchecked")
public static > Range of(N min, N max) {
if (min == null && max == null) {
throw new NullPointerException("min and max may not be null");
}
Class type = (Class) (min != null ? min.getClass() : max.getClass());
return of(type, min, max);
}
/**
* Creates a new fully closed range for the given type and min and max values.
*
* @param type
* the type of the min and max values
* @param min
* the min value
* @param max
* the max value
* @param
* the type of the min and max values
*
* @return a new range instance with the given min and max
*
* @see MutableRange
* @see org.omnifaces.utils.data.Range
* @deprecated Use {@link org.omnifaces.utils.data.Range#ofClosed(Comparable, Comparable)} or {@link MutableRange#ofClosed(Comparable, Comparable)} instead.
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static > Range of(Class type, N min, N max) {
Range range;
if (type.equals(Long.class)) {
range = new LongRange();
}
else if (type.equals(Integer.class)) {
range = new IntegerRange();
}
else if (type.equals(BigDecimal.class)) {
range = new BigDecimalRange();
}
else {
throw new UnsupportedOperationException(type + " range not supported");
}
range.setMin(min);
range.setMax(max);
return range;
}
private void checkNonNull(Range> range) {
requireNonNull(range, "other");
requireNonNull(range.getMin(), (range != this ? "other " : "") + "min");
requireNonNull(range.getMax(), (range != this ? "other " : "") + "max");
}
@SuppressWarnings("unchecked")
public N[] toArray() {
checkNonNull(this);
N[] array = (N[]) Array.newInstance(getMin() != null ? getMin().getClass() : getMax().getClass(), 2);
array[0] = getMin();
array[1] = getMax();
return array;
}
protected int compare(N left, N right) {
return Objects.compare(left, right, naturalOrder());
}
public N getMin() {
return min;
}
public void setMin(N min) {
if (min != null && max != null && compare(min, getMax()) > 0) {
throw new IllegalArgumentException("min cannot be greater than max");
}
this.min = min;
}
public N getMax() {
return max;
}
public void setMax(N max) {
if (max != null && min != null && compare(getMin(), max) > 0) {
throw new IllegalArgumentException("max cannot be lesser than min");
}
this.max = max;
}
protected abstract Range newInstance();
@Override
public boolean isMinInclusive() {
return true;
}
@Override
public boolean isMaxInclusive() {
return true;
}
public void setMinInclusive(boolean minInclusive) {
this.minInclusive = minInclusive;
}
public void setMaxInclusive(boolean maxInclusive) {
this.maxInclusive = maxInclusive;
}
@Override
public Range withMinInclusive(boolean newMinInclusive) {
return newInstance(min, max, newMinInclusive, maxInclusive);
}
@Override
public Range withMaxInclusive(boolean newMaxInclusive) {
return newInstance(min, max, minInclusive, newMaxInclusive);
}
public Range withMin(N newMin) {
return newInstance(newMin, max, minInclusive, maxInclusive);
}
public Range withMax(N newMax) {
return newInstance(min, newMax, minInclusive, maxInclusive);
}
private Range newInstance(N min, N max, boolean minInclusive, boolean maxInclusive) {
Range newRange = newInstance();
newRange.setMin(min);
newRange.setMax(max);
newRange.setMinInclusive(minInclusive);
newRange.setMaxInclusive(maxInclusive);
return newRange;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy