/** * 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); }
/** * Returns the smallest sample dimension type capable to hold the specified range of values. * * @param range The range of values. * @return The smallest sample dimension type for the specified range. */ public static SampleDimensionType getSampleDimensionType(final Range<?> range) { final Class<?> type = range.getElementClass(); if (Double.class.isAssignableFrom(type)) { return REAL_64BITS; } if (Float.class.isAssignableFrom(type)) { return REAL_32BITS; } long min = ((Number) range.getMinValue()).longValue(); long max = ((Number) range.getMaxValue()).longValue(); if (!range.isMinIncluded()) min++; if (!range.isMaxIncluded()) max--; return getSampleDimensionType(min, max); }
/** * Constructs a range with the same type and the same values than the specified range. This is a * copy constructor. * * @param range The range to copy. The elements must be {@link Number} instances. * @since 2.4 */ public NumberRange(final Range<T> range) { super( range.getElementClass(), range.getMinValue(), range.isMinIncluded(), range.getMaxValue(), range.isMaxIncluded()); }
Range<T> union = new Range<T>( range.getElementClass(), (T) range.getMinValue(), range.isMinIncluded(),
/** * 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") // Safe because we checked in the line just above. final NumberRange<N> cast = (NumberRange<N>) wrap((Range) range); return cast; } // TODO: Remove the (Range) cast when we will be allowed to compile for Java 6. return new NumberRange<N>(type, (Range) range); }
/** * Returns the smallest sample dimension type capable to hold the specified range of values. * * @param range The range of values. * @return The smallest sample dimension type for the specified range. */ public static SampleDimensionType getSampleDimensionType(final Range<?> range) { final Class<?> type = range.getElementClass(); if (Double.class.isAssignableFrom(type)) { return REAL_64BITS; } if (Float.class.isAssignableFrom(type)) { return REAL_32BITS; } long min = ((Number) range.getMinValue()).longValue(); long max = ((Number) range.getMaxValue()).longValue(); if (!range.isMinIncluded()) min++; if (!range.isMaxIncluded()) max--; return getSampleDimensionType(min, max); }
/** * Constructs a range with the same type and the same values than the * specified range. This is a copy constructor. * * @param range The range to copy. The elements must be {@link Number} instances. * * @since 2.4 */ public NumberRange(final Range<T> range) { super(range.getElementClass(), range.getMinValue(), range.isMinIncluded(), range.getMaxValue(), range.isMaxIncluded()); }
@Override public Object getDefaultValue( ResourceInfo resource, String dimensionName, DimensionInfo dimension, Class clz) { if (value instanceof Range) { Range r = (Range) value; if (clz.isAssignableFrom(r.getElementClass())) { return r; } else { Comparable min = (Comparable) Converters.convert(r.getMinValue(), clz); Comparable max = (Comparable) Converters.convert(r.getMaxValue(), clz); return new Range(clz, min, max); } } else { return Converters.convert(this.value, clz); } }