/** * Parses the specified value as a NumberRange if it's in the min/max form, as a Double * otherwise * * @param val */ private Object parseNumberOrRange(String val) { if (val.contains("/")) { String[] splitted = val.split("/"); final String strStart = splitted[0]; final String strEnd = splitted[1]; if (strStart.equals(strEnd)) { return Double.parseDouble(strStart); } double start = Double.parseDouble(strStart); double end = Double.parseDouble(strEnd); return new NumberRange<Double>(Double.class, start, end); } else { return Double.parseDouble(val); } }
@Test public void testCustomCloudCoverDimensionConvertion() throws IOException, ParseException { MockDimensionReader reader = new MockDimensionReader(); reader.metadata.put("HAS_MYDIM_DOMAIN", "true"); reader.metadata.put("MYDIM_DOMAIN_DATATYPE", "java.lang.Integer"); ReaderDimensionsAccessor accessor = new ReaderDimensionsAccessor(reader); List<Object> converted = accessor.convertDimensionValue("MYDIM", Arrays.asList("75/100")); assertEquals(1, converted.size()); NumberRange<Double> expected = new NumberRange<Double>(Double.class, 75d, 100d); assertEquals(expected, converted.get(0)); } }
@Test public void testCustomDepthDimensionConvertion() throws IOException, ParseException { MockDimensionReader reader = new MockDimensionReader(); reader.metadata.put("HAS_MYDIM_DOMAIN", "true"); reader.metadata.put("MYDIM_DOMAIN_DATATYPE", "java.lang.Double"); ReaderDimensionsAccessor accessor = new ReaderDimensionsAccessor(reader); List<Object> converted = accessor.convertDimensionValue("MYDIM", Arrays.asList("10/20")); assertEquals(1, converted.size()); NumberRange<Double> expected = new NumberRange<Double>(Double.class, 10d, 20d); assertEquals(expected, converted.get(0)); }
@Test public void testMultimapConverter() throws Exception { XStreamPersisterFactory factory = new XStreamPersisterFactory(); XStreamPersister xmlPersister = factory.createXMLPersister(); XStream xs = xmlPersister.getXStream(); Multimap<String, Object> mmap = ArrayListMultimap.create(); mmap.put("one", "abc"); mmap.put("one", Integer.valueOf(2)); mmap.put("two", new NumberRange<Integer>(Integer.class, 10, 20)); ByteArrayOutputStream out = new ByteArrayOutputStream(); persister.save(mmap, out); // print(in(out)); Multimap mmap2 = persister.load(in(out), Multimap.class); assertEquals(mmap, mmap2); }
/** * Creates a new range using the same element class than this range. This method will be * overriden by subclasses in order to create a range of a more specific type. */ @Override NumberRange<T> create( final T minValue, final boolean isMinIncluded, final T maxValue, final boolean isMaxIncluded) { return new NumberRange<T>(elementClass, minValue, isMinIncluded, maxValue, isMaxIncluded); }
/** * Wraps the specified {@link Range} in a {@code NumberRange} object. If the specified range is * already an instance of {@code NumberRange}, then it is returned unchanged. * * @param <N> The type of elements in the given range. * @param range The range to wrap. * @return The same range than {@code range} as a {@code NumberRange} object. */ public static <N extends Number & Comparable<? super N>> NumberRange<N> wrap( final Range<N> range) { if (range instanceof NumberRange) { final NumberRange<N> cast = (NumberRange<N>) range; return cast; } // The constructor will ensure that the range element class is a subclass of Number. return new NumberRange<N>(range); }
/** Constructs a category for sample value {@code sample}. */ private Category( final CharSequence name, final int[] ARGB, final Number sample, final boolean isQuantitative) { this(name, ARGB, new NumberRange(sample.getClass(), sample, sample), isQuantitative); }
/** * Constructs a range of {@code short} values. * * @param minimum The minimum value. * @param isMinIncluded Defines whether the minimum value is included in the range. * @param maximum The maximum value. * @param isMaxIncluded Defines whether the maximum value is included in the range. * @since 2.5 */ public static NumberRange<Short> create( final short minimum, final boolean isMinIncluded, final short maximum, final boolean isMaxIncluded) { return new NumberRange<Short>( Short.class, Short.valueOf(minimum), isMinIncluded, Short.valueOf(maximum), isMaxIncluded); }
/** * Constructs a range of {@code byte} values. * * @param minimum The minimum value. * @param isMinIncluded Defines whether the minimum value is included in the range. * @param maximum The maximum value. * @param isMaxIncluded Defines whether the maximum value is included in the range. * @since 2.5 */ public static NumberRange<Byte> create( final byte minimum, final boolean isMinIncluded, final byte maximum, final boolean isMaxIncluded) { return new NumberRange<Byte>( Byte.class, Byte.valueOf(minimum), isMinIncluded, Byte.valueOf(maximum), isMaxIncluded); }
/** * Constructs a range of {@code int} values. * * @param minimum The minimum value. * @param isMinIncluded Defines whether the minimum value is included in the range. * @param maximum The maximum value. * @param isMaxIncluded Defines whether the maximum value is included in the range. * @since 2.5 */ public static NumberRange<Integer> create( final int minimum, final boolean isMinIncluded, final int maximum, final boolean isMaxIncluded) { return new NumberRange<Integer>( Integer.class, Integer.valueOf(minimum), isMinIncluded, Integer.valueOf(maximum), isMaxIncluded); }
/** * Constructs a range of {@code long} values. * * @param minimum The minimum value. * @param isMinIncluded Defines whether the minimum value is included in the range. * @param maximum The maximum value. * @param isMaxIncluded Defines whether the maximum value is included in the range. * @since 2.5 */ public static NumberRange<Long> create( final long minimum, final boolean isMinIncluded, final long maximum, final boolean isMaxIncluded) { return new NumberRange<Long>( Long.class, Long.valueOf(minimum), isMinIncluded, Long.valueOf(maximum), isMaxIncluded); }
/** * Constructs a range of {@code float} values. * * @param minimum The minimum value. * @param isMinIncluded Defines whether the minimum value is included in the range. * @param maximum The maximum value. * @param isMaxIncluded Defines whether the maximum value is included in the range. * @since 2.5 */ public static NumberRange<Float> create( final float minimum, final boolean isMinIncluded, final float maximum, final boolean isMaxIncluded) { return new NumberRange<Float>( Float.class, Float.valueOf(minimum), isMinIncluded, Float.valueOf(maximum), isMaxIncluded); }
/** * Constructs a range of {@code double} values. * * @param minimum The minimum value. * @param isMinIncluded Defines whether the minimum value is included in the range. * @param maximum The maximum value. * @param isMaxIncluded Defines whether the maximum value is included in the range. * @since 2.5 */ public static NumberRange<Double> create( final double minimum, final boolean isMinIncluded, final double maximum, final boolean isMaxIncluded) { return new NumberRange<Double>( Double.class, Double.valueOf(minimum), isMinIncluded, Double.valueOf(maximum), isMaxIncluded); }
/** * Returns a new {@link Range} object initialized with the given values. * * @param lower The lower value, inclusive. * @param upper The upper value, inclusive. */ private Range<T> newRange(final T lower, final T upper) { if (isNumeric) { return new NumberRange(elementClass, lower, upper); } else { return new Range<T>(elementClass, lower, upper); } }
final double newMaximum = 2000d; final NumberRange<Double> range = new NumberRange<Double>(Double.class, newMinimum, newMaximum); coverageDim.setRange(range);
final double newMaximum = 2000d; final NumberRange<Double> range = new NumberRange<Double>(Double.class, newMinimum, newMaximum); coverageDim.setRange(range);
/** * Casts the specified range to the specified type. If this class is associated to a unit of * measurement, then this method convert the {@code range} units to the same units than this * instance. This method is overriden by {@link MeasurementRange} only in the way described * above. * * @param type The class to cast to. Must be one of {@link Byte}, {@link Short}, {@link * Integer}, {@link Long}, {@link Float} or {@link Double}. * @return The casted range, or {@code range} if no cast is needed. * @throws IllegalArgumentException if the values are not convertible to the specified class. */ <N extends Number & Comparable<? super N>> NumberRange<N> convertAndCast( final Range<? extends Number> range, final Class<N> type) throws IllegalArgumentException { if (type.equals(range.getElementClass())) { @SuppressWarnings({ "unchecked", "rawtypes" }) // Safe because we checked in the line just above. final NumberRange<N> cast = (NumberRange) wrap((Range) range); return cast; } return new NumberRange<N>(type, range); }
this.signed = signed; this.real = real; this.range = new NumberRange(c, lower, upper); this.positiveRange = signed ? null : new NumberRange(c, one, upper); final int ordinal = code.ordinal(); assert MAP[ordinal] == null : code;
new NumberRange<Double>( Double.class, ((Number) adaptee.getMinimum()).doubleValue(),
/** Returns min and max value applying the expression over the feature collection */ public static NumberRange<Double> getMinMax( ListFeatureCollection fc, Expression attributeExpression) { DoubleSummaryStatistics stats = fc.stream() .collect( Collectors.summarizingDouble( f -> attributeExpression.evaluate(f, Double.class))); return new NumberRange<>(Double.class, stats.getMin(), stats.getMax()); }