/** * Creates a range from the given minimum and maximum values, inclusive. * The default implementation creates a range of {@code float}, but this method is * overridden by {@code ArrayVector.Decimal} which create a range of {@code double}. */ NumberRange<?> createRange(final float min, final float max) { return NumberRange.create(min, true, max, true); }
@Override protected Range<E> newRange(final E lower, final E upper) { return new NumberRange<>(elementType, lower, isMinIncluded, upper, isMaxIncluded); } }
/** * Returns the {@linkplain #getMaxDouble() maximum value} with the specified inclusive or exclusive state. * If this range is unbounded, then {@link Double#POSITIVE_INFINITY} is returned. * * @param inclusive {@code true} for the maximum value inclusive, or * {@code false} for the maximum value exclusive. * @return the maximum value, inclusive or exclusive as requested. */ public double getMaxDouble(final boolean inclusive) { double value = getMaxDouble(); if (inclusive != isMaxIncluded()) { value = next(getElementType(), value, !inclusive); } return value; }
/** * Returns the {@linkplain #getMinDouble() minimum value} with the specified inclusive or exclusive state. * If this range is unbounded, then {@link Double#NEGATIVE_INFINITY} is returned. * * @param inclusive {@code true} for the minimum value inclusive, or * {@code false} for the minimum value exclusive. * @return the minimum value, inclusive or exclusive as requested. */ public double getMinDouble(final boolean inclusive) { double value = getMinDouble(); if (inclusive != isMinIncluded()) { value = next(getElementType(), value, inclusive); } return value; }
/** * 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)); }
boolean minIncluded = r.isMinIncluded(); boolean maxIncluded = r.isMaxIncluded(); final double[] extremums = { r.getMinDouble(), r.getMaxDouble(), r.getMinDouble(!minIncluded), r.getMaxDouble(!maxIncluded)}; original.toConverse.transform(extremums, 0, extremums, 0, extremums.length); if (extremums[minIncluded ? 2 : 0] > extremums[maxIncluded ? 3 : 1]) { // Compare exclusive min/max.
/** * Completes the union computed by {@link Range#union(Range)} with the unions of alternative extremum. */ private ConvertedRange(final NumberRange<Double> union, final ConvertedRange r1, final NumberRange<Double> r2) { super(union, r1.unit()); boolean f; altMinimum = Math.min(r1.getMinDouble(f = !isMinIncluded()), r2.getMinDouble(f)); altMaximum = Math.max(r1.getMaxDouble(f = !isMaxIncluded()), r2.getMaxDouble(f)); }
/** * Tests the sample values range and converged values range after construction of a list of categories. */ @Test public void testRanges() { final CategoryList list = new CategoryList(categories(), null); assertSorted(list); assertTrue ("isMinIncluded", list.range.isMinIncluded()); assertFalse ("isMaxIncluded", list.range.isMaxIncluded()); assertFalse ("converse.isMinIncluded", list.converse.range.isMinIncluded()); // Because computed from maxValue before conversion. assertFalse ("converse.isMaxIncluded", list.converse.range.isMaxIncluded()); assertEquals("minValue", 0, ((Number) list.range .getMinValue()).doubleValue(), STRICT); assertEquals("maxValue", 120, ((Number) list.range .getMaxValue()).doubleValue(), STRICT); assertEquals("converse.minValue", -117, ((Number) list.converse.range.getMinValue()).doubleValue(), STRICT); assertEquals("converse.maxValue", 15, ((Number) list.converse.range.getMaxValue()).doubleValue(), STRICT); assertEquals("converse.minValue", -117, list.converse.range.getMinDouble(false), STRICT); assertEquals("converse.maxValue", 15, list.converse.range.getMaxDouble(false), STRICT); assertEquals("converse.minValue", -116, list.converse.range.getMinDouble(true), CategoryTest.EPS); assertEquals("converse.maxValue", 14.9, list.converse.range.getMaxDouble(true), CategoryTest.EPS); }
/** * Tests the endpoint values of a range of integers. */ @Test public void testIntegerEndpoints() { final NumberRange<Integer> range = NumberRange.create(10, true, 20, true); assertEquals(10, range.getMinDouble( ), 0); assertEquals(10, range.getMinDouble(true ), 0); assertEquals( 9, range.getMinDouble(false), 0); assertEquals(20, range.getMaxDouble( ), 0); assertEquals(20, range.getMaxDouble(true ), 0); assertEquals(21, range.getMaxDouble(false), 0); }
/** * 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)); }
/** * Creates a category for data that are already real values. */ @Test public void testConvertedCategory() { final Random random = TestUtilities.createRandomNumberGenerator(); for (int pass=0; pass<3; pass++) { final double lower = random.nextDouble() * 5; final double upper = random.nextDouble() * 10 + lower; final Category category = new Category("Random", NumberRange.create(lower, true, upper, true), (MathTransform1D) MathTransforms.identity(1), null, null); assertSame ("converse", category, category.converse); assertEquals ("name", "Random", String.valueOf(category.name)); assertEquals ("name", "Random", String.valueOf(category.getName())); assertEquals ("minimum", lower, category.minimum, STRICT); assertEquals ("maximum", upper, category.maximum, STRICT); assertBoundEquals("range.minValue", lower, category.range.getMinValue()); assertBoundEquals("range.maxValue", upper, category.range.getMaxValue()); assertSame ("sampleRange", category.range, category.getSampleRange()); assertSame ("measurementRange", category.range, category.getMeasurementRange().get()); assertSame ("transferFunction", category.toConverse, category.getTransferFunction().get()); assertTrue ("toConverse.isIdentity", category.toConverse.isIdentity()); assertTrue ("isQuantitative", category.isQuantitative()); } }
final NumberRange<?> range = category.range; if (range != null && !category.isQuantitative()) { if (!range.isBounded()) { throw new IllegalStateException(Resources.format(Resources.Keys.CanNotEnumerateValuesInRange_1, range)); widestClass = Numbers.widestClass(widestClass, range.getElementType()); ranges[count++] = range; for (int i=0; i<count; i++) { final NumberRange<?> range = ranges[i]; final Number minimum = range.getMinValue(); final Number maximum = range.getMaxValue(); if (range.isMinIncluded()) noDataValues.add(Numbers.cast(minimum, widestClass)); if (range.isMaxIncluded()) noDataValues.add(Numbers.cast(maximum, widestClass)); if (Numbers.isInteger(range.getElementType())) { long value = minimum.longValue() + 1; // If value was inclusive, then it has already been added to the set. long stop = maximum.longValue() - 1;
/** * Tests union and intersection with {@link Double} values. */ @Test public void testWithDoubles() { NumberRange<Double> r1 = NumberRange.create(10.0, true, 20.0, true); NumberRange<Double> r2 = NumberRange.create(15.0, true, 30.0, true); assertEquals(Double.class, r1.getElementType()); assertEquals(Double.class, r2.getElementType()); assertEquals(NumberRange.create(10.0, true, 30.0, true), r1.union(r2)); assertEquals(NumberRange.create(15.0, true, 20.0, true), r1.intersect(r2)); }
assertEquals("minimum", -2f, range.getMinValue()); assertEquals("maximum", 30f, range.getMaxValue()); assertEquals("minimum", -2d, range.getMinDouble(true), STRICT); assertEquals("maximum", 30d, range.getMaxDouble(true), STRICT); assertEquals("units", Units.CELSIUS, dimension.getUnits().get());
/** * Tests union and intersection involving a cast from integer to double values. */ @Test public void testDoubleWithIntegerArguments() { NumberRange<Double> r1 = NumberRange.create(10.0, true, 20.0, true); NumberRange<Integer> r2 = NumberRange.create(15, true, 30, true); assertEquals(Double .class, r1.getElementType()); assertEquals(Integer.class, r2.getElementType()); assertEquals(NumberRange.create(10.0, true, 30.0, true), r1.unionAny(r2)); assertEquals(NumberRange.create(15, true, 20, true), r1.intersectAny(r2)); r1 = NumberRange.create(10.0, true, 20.5, true); assertEquals(NumberRange.create(15.0f, true, 20.5f, true), r1.intersectAny(r2)); }
/** * Creates a range initialized to the given value. */ private MultiplicityRange(final NumberRange<Integer> range) { if (range != null) { lower = range.getMinValue(); if (lower == null) { lower = NilReason.UNKNOWN.createNilObject(Integer.class); } else if (!range.isMinIncluded()) { lower = Math.incrementExact(lower); } upper = new UnlimitedInteger(range.getMaxValue(), range.isMaxIncluded()); } else { lower = 0; upper = new UnlimitedInteger(); // Initialized to missing value. } }
/** * 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); } }
/** * Tests a category with a NaN value. */ @Test public void testCategoryNaN() { final Category category = new Category("NaN", new NumberRange<>(Float.class, Float.NaN, true, Float.NaN, true), null, null, null); final NumberRange<?> range = category.getSampleRange(); assertSame ("converse", category, category.converse); assertEquals("name", "NaN", String.valueOf(category.name)); assertEquals("name", "NaN", String.valueOf(category.getName())); assertEquals("minimum", Double.NaN, category.minimum, STRICT); assertEquals("maximum", Double.NaN, category.maximum, STRICT); assertNull ("sampleRange", category.range); assertEquals("range.minValue", Float.NaN, range.getMinValue()); assertEquals("range.maxValue", Float.NaN, range.getMaxValue()); assertFalse ("measurementRange", category.getMeasurementRange().isPresent()); assertFalse ("transferFunction", category.getTransferFunction().isPresent()); assertTrue ("toConverse.isIdentity", category.toConverse.isIdentity()); assertFalse ("isQuantitative", category.isQuantitative()); } }
/** * Tests the {@link NumberRange#createBestFit(Number, boolean, Number, boolean)} method. */ @Test public void testCreateBestFit() { assertEquals(NumberRange.create((short) 2, true, (short) 200, true), NumberRange.createBestFit(2, true, 200.0, true)); }
if (range != null && !range.isEmpty()) { final Number min = range.getMinValue(); final Number max = range.getMaxValue(); final boolean isInteger = (min.doubleValue() >= Long.MIN_VALUE && max.doubleValue() <= Long.MAX_VALUE &&