/** * Creates a new range using the same element type than this range. This method will * be overridden by subclasses in order to create a range of a more specific type. */ @Override Range<E> create(final E minValue, final boolean isMinIncluded, final E maxValue, final boolean isMaxIncluded) { return new NumberRange<>(elementType, minValue, isMinIncluded, maxValue, isMaxIncluded); }
@Override protected Range<E> newRange(final E lower, final E upper) { return new NumberRange<>(elementType, lower, isMinIncluded, upper, isMaxIncluded); } }
/** * Creates a new range using the same element type than this range. This method will * be overridden by subclasses in order to create a range of a more specific type. */ @Override Range<E> create(final E minValue, final boolean isMinIncluded, final E maxValue, final boolean isMaxIncluded) { return new NumberRange<>(elementType, minValue, isMinIncluded, maxValue, isMaxIncluded); }
@Override protected Range<E> newRange(final E lower, final E upper) { return new NumberRange<>(elementType, lower, isMinIncluded, upper, isMaxIncluded); } }
/** * Returns the specified {@link Range} as a {@code NumberRange} object. * If the specified range is already an instance of {@code NumberRange}, then it is returned unchanged. * Otherwise a new number range is created using the {@linkplain #NumberRange(Range) copy constructor}. * * @param <N> the type of elements in the given range. * @param range the range to cast or copy. * @return the same range than {@code range} as a {@code NumberRange} object. */ public static <N extends Number & Comparable<? super N>> NumberRange<N> castOrCopy(final Range<N> range) { if (range instanceof NumberRange<?>) { return (NumberRange<N>) range; } // The constructor will ensure that the range element type is a subclass of Number. // Do not invoke unique(NumberRange) because the returned range is often temporary. return new NumberRange<>(range); }
/** * Returns the specified {@link Range} as a {@code NumberRange} object. * If the specified range is already an instance of {@code NumberRange}, then it is returned unchanged. * Otherwise a new number range is created using the {@linkplain #NumberRange(Range) copy constructor}. * * @param <N> the type of elements in the given range. * @param range the range to cast or copy. * @return the same range than {@code range} as a {@code NumberRange} object. */ public static <N extends Number & Comparable<? super N>> NumberRange<N> castOrCopy(final Range<N> range) { if (range instanceof NumberRange<?>) { return (NumberRange<N>) range; } // The constructor will ensure that the range element type is a subclass of Number. // Do not invoke unique(NumberRange) because the returned range is often temporary. return new NumberRange<>(range); }
/** * Constructs a range of {@code short} values. * This method may return a shared instance, at implementation choice. * * @param minValue the minimal value. * @param isMinIncluded {@code true} if the minimal value is inclusive, or {@code false} if exclusive. * @param maxValue the maximal value. * @param isMaxIncluded {@code true} if the maximal value is inclusive, or {@code false} if exclusive. * @return the new range of numeric values for the given endpoints. */ public static NumberRange<Short> create(final short minValue, final boolean isMinIncluded, final short maxValue, final boolean isMaxIncluded) { final Short min = minValue; final Short max = (minValue == maxValue) ? min : maxValue; return unique(new NumberRange<>(Short.class, min, isMinIncluded, max, isMaxIncluded)); }
/** * Constructs a range of {@code long} values. * This method may return a shared instance, at implementation choice. * * @param minValue the minimal value. * @param isMinIncluded {@code true} if the minimal value is inclusive, or {@code false} if exclusive. * @param maxValue the maximal value. * @param isMaxIncluded {@code true} if the maximal value is inclusive, or {@code false} if exclusive. * @return the new range of numeric values for the given endpoints. */ public static NumberRange<Long> create(final long minValue, final boolean isMinIncluded, final long maxValue, final boolean isMaxIncluded) { final Long min = minValue; final Long max = (minValue == maxValue) ? min : maxValue; return unique(new NumberRange<>(Long.class, min, isMinIncluded, max, isMaxIncluded)); }
/** * Constructs a range of {@code short} values. * This method may return a shared instance, at implementation choice. * * @param minValue the minimal value. * @param isMinIncluded {@code true} if the minimal value is inclusive, or {@code false} if exclusive. * @param maxValue the maximal value. * @param isMaxIncluded {@code true} if the maximal value is inclusive, or {@code false} if exclusive. * @return the new range of numeric values for the given endpoints. */ public static NumberRange<Short> create(final short minValue, final boolean isMinIncluded, final short maxValue, final boolean isMaxIncluded) { return unique(new NumberRange<>(Short.class, Short.valueOf(minValue), isMinIncluded, Short.valueOf(maxValue), isMaxIncluded)); }
/** * Constructs a range of {@code byte} values. * This method may return a shared instance, at implementation choice. * * @param minValue the minimal value. * @param isMinIncluded {@code true} if the minimal value is inclusive, or {@code false} if exclusive. * @param maxValue the maximal value. * @param isMaxIncluded {@code true} if the maximal value is inclusive, or {@code false} if exclusive. * @return the new range of numeric values for the given endpoints. */ public static NumberRange<Byte> create(final byte minValue, final boolean isMinIncluded, final byte maxValue, final boolean isMaxIncluded) { return unique(new NumberRange<>(Byte.class, Byte.valueOf(minValue), isMinIncluded, Byte.valueOf(maxValue), isMaxIncluded)); }
/** * Constructs a range of {@code long} values. * This method may return a shared instance, at implementation choice. * * @param minValue the minimal value. * @param isMinIncluded {@code true} if the minimal value is inclusive, or {@code false} if exclusive. * @param maxValue the maximal value. * @param isMaxIncluded {@code true} if the maximal value is inclusive, or {@code false} if exclusive. * @return the new range of numeric values for the given endpoints. */ public static NumberRange<Long> create(final long minValue, final boolean isMinIncluded, final long maxValue, final boolean isMaxIncluded) { return unique(new NumberRange<>(Long.class, Long.valueOf(minValue), isMinIncluded, Long.valueOf(maxValue), isMaxIncluded)); }
/** Computes the minimal and maximal values in this vector. */ @SuppressWarnings({"unchecked","rawtypes"}) @Override public final NumberRange<?> range() { Number min = get(0); Number max = get(length - 1); if (((Comparable) max).compareTo((Comparable) min) < 0) { Number tmp = min; min = max; max = tmp; } return new NumberRange(type, min, true, max, true); } }
/** Computes the minimal and maximal values in this vector. */ @SuppressWarnings({"unchecked","rawtypes"}) @Override public NumberRange<?> range() { long min = first; long max = first + increment * (length - 1); if (max < min) { min = max; max = first; } return new NumberRange(type, Numbers.wrap(min, type), true, Numbers.wrap(max, type), true); } }
/** * Returns the value as a number range, or {@code null} if none. */ NumberRange<Integer> value() { if (lower != null && upper != null) { final Integer h = upper.value(); if (h != null || upper.isInfinite()) { return new NumberRange<>(Integer.class, lower, true, h, true); } } return null; }
/** Computes the minimal and maximal values in this vector. */ @SuppressWarnings({"unchecked","rawtypes"}) @Override public NumberRange<?> range() { double min = first; double max = first + increment * (length - 1); if (max < min) { min = max; max = first; } return new NumberRange(type, Numbers.wrap(min, type), true, Numbers.wrap(max, type), true); } }
/** Computes the minimal and maximal values in this vector. */ @SuppressWarnings({"unchecked","rawtypes"}) @Override public NumberRange<?> range() { long min = first; long max = first + increment * (length - 1); if (max < min) { min = max; max = first; } return new NumberRange(type, Numbers.wrap(min, type), true, Numbers.wrap(max, type), true); } }
/** * Creates a multiplicity instance with the same content than in {@value #FILENAME} file. * * @param requireInclusive 0 if we are allowed to create range with exclusive bounds, or * 1 if all bounds shall be inclusive. */ private FeatureAttributeMock create(final int requireInclusive) { final FeatureAttributeMock f = new FeatureAttributeMock(); f.memberName = Names.createLocalName(null, null, "Multiplicity test"); f.cardinality = new Multiplicity( new NumberRange<>(Integer.class, 0, true, 233333 - requireInclusive, requireInclusive != 0), new NumberRange<>(Integer.class, requireInclusive, requireInclusive != 0, null, false)); return f; }
/** * Constructs a range of {@code double} values. * The values can not be {@link Double#NaN}. * This method may return a shared instance, at implementation choice. * * @param minValue the minimal value, or {@link Double#NEGATIVE_INFINITY} if none. * @param isMinIncluded {@code true} if the minimal value is inclusive, or {@code false} if exclusive. * @param maxValue the maximal value, or {@link Double#POSITIVE_INFINITY} if none. * @param isMaxIncluded {@code true} if the maximal value is inclusive, or {@code false} if exclusive. * @return the new range of numeric values for the given endpoints. */ public static NumberRange<Double> create(final double minValue, final boolean isMinIncluded, final double maxValue, final boolean isMaxIncluded) { return unique(new NumberRange<>(Double.class, valueOf("minValue", minValue, Double.NEGATIVE_INFINITY), isMinIncluded, valueOf("maxValue", maxValue, Double.POSITIVE_INFINITY), isMaxIncluded)); }
/** * Constructs a range of {@code float} values. * The values can not be {@link Float#NaN}. * This method may return a shared instance, at implementation choice. * * @param minValue the minimal value, or {@link Float#NEGATIVE_INFINITY} if none. * @param isMinIncluded {@code true} if the minimal value is inclusive, or {@code false} if exclusive. * @param maxValue the maximal value, or {@link Float#POSITIVE_INFINITY} if none. * @param isMaxIncluded {@code true} if the maximal value is inclusive, or {@code false} if exclusive. * @return the new range of numeric values for the given endpoints. */ public static NumberRange<Float> create(final float minValue, final boolean isMinIncluded, final float maxValue, final boolean isMaxIncluded) { return unique(new NumberRange<>(Float.class, valueOf("minValue", minValue, Float.NEGATIVE_INFINITY), isMinIncluded, valueOf("maxValue", maxValue, Float.POSITIVE_INFINITY), isMaxIncluded)); }
/** * Tests the construction using the {@link ValueRange} annotation. * The annotation used for this test is declared on this test method. * * @throws NoSuchMethodException Should never happen. */ @Test @ValueRange(minimum=4, maximum=8, isMaxIncluded=false) public void testValueRangeAnnotation() throws NoSuchMethodException { final ValueRange values = NumberRangeTest.class .getMethod("testValueRangeAnnotation").getAnnotation(ValueRange.class); assertNotNull("Annotation not found.", values); final NumberRange<Short> range = new NumberRange<>(Short.class, values); assertEquals(NumberRange.create((short) 4, true, (short) 8, false), range); } }