/** * Casts this range to the specified type. * * @param <N> The class to cast to. * @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 this} if this range already uses the specified type. * @throws IllegalArgumentException if the values are not convertible to the specified class. */ public <N extends Number & Comparable<? super N>> NumberRange<N> castTo(final Class<N> type) throws IllegalArgumentException { return convertAndCast(this, type); }
/** * Returns the union of this range with the given range. Widening conversions will be applied as * needed. */ @Override @SuppressWarnings({"unchecked", "rawtypes"}) public NumberRange<?> union(Range<?> range) { final Class<? extends Number> type = getWidestClass(elementClass, getElementClass(range)); range = convertAndCast((Range) range, (Class) type); return (NumberRange<?>) castTo((Class) type).unionNC((Range) range); }
/** Returns the range of values that are in this range but not in the given range. */ @Override @SuppressWarnings({"unchecked", "rawtypes"}) public NumberRange<?>[] subtract(Range<?> range) { Class<? extends Number> type = getWidestClass(elementClass, getElementClass(range)); return (NumberRange[]) castTo((Class) type).subtractNC(convertAndCast((Range) range, (Class) type)); }
/** Returns {@code true} if this range intersects the given range. */ @Override @SuppressWarnings({"unchecked", "rawtypes"}) public boolean intersects(Range<?> range) { final Class<? extends Number> type = getWidestClass(elementClass, getElementClass(range)); /* * The type bounds is actually <? extends Number & Comparable> but I'm unable to express * it it local variable as of Java 6. So we have to bypass the compiler check, but those * casts are actually safes, including Range because getElementClass(range) would have * throw an exception otherwise. */ range = convertAndCast((Range) range, (Class) type); return castTo((Class) type).intersectsNC((Range) range); }
/** Returns {@code true} if the supplied range is fully contained within this range. */ @Override @SuppressWarnings({"unchecked", "rawtypes"}) public boolean contains(Range<?> range) { final Class<? extends Number> type = getWidestClass(elementClass, getElementClass(range)); /* * The type bounds is actually <? extends Number & Comparable> but I'm unable to express * it it local variable as of Java 6. So we have to bypass the compiler check, but those * casts are actually safes, including Range because getElementClass(range) would have * throw an exception otherwise. */ range = convertAndCast((Range) range, (Class) type); return castTo((Class) type).containsNC((Range) range); }
/** * Returns the intersection of this range with the given range. Widening conversions will be * applied as needed. */ @Override @SuppressWarnings({"unchecked", "rawtypes"}) public NumberRange<?> intersect(Range<?> range) { final Class<? extends Number> rangeType = getElementClass(range); Class<? extends Number> type = getWidestClass(elementClass, rangeType); range = castTo((Class) type).intersectNC(convertAndCast((Range) range, (Class) type)); /* * Use a finer type capable to holds the result (since the intersection * may have reduced the range), but not finer than the finest type of * the ranges used in the intersection calculation. */ type = getFinestClass(elementClass, rangeType); if (range.minValue != null) { type = getWidestClass(type, getFinestClass(((Number) range.minValue).doubleValue())); } if (range.maxValue != null) { type = getWidestClass(type, getFinestClass(((Number) range.maxValue).doubleValue())); } return convertAndCast((Range) range, (Class) type); }
/** * Casts this range to the specified type. * * @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 this} if this range already uses * the specified type. */ public NumberRange castTo(final Class type) { return convertAndCast(this, type); }
/** * Casts this range to the specified type. * * @param <N> The class to cast to. * @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 this} if this range already uses the specified type. * @throws IllegalArgumentException if the values are not convertible to the specified class. */ public <N extends Number & Comparable<? super N>> NumberRange<N> castTo(final Class<N> type) throws IllegalArgumentException { return convertAndCast(this, type); }
/** * Work around a Java 5 compiler bug which is fixed in Java 6. * * @todo Delete when we will be allowed to target Java 6. */ @Deprecated final NumberRange damnJava5(final Range range, final Class type) { return convertAndCast(range, type); }
/** * 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. * * @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. */ //@Override NumberRange convertAndCast(final Range range, final Class type) { if (range instanceof MeasurementRange) { return ((MeasurementRange) range).convertAndCast(type, units); } else { return super.convertAndCast(range, type); } }
/** * Returns the union of this range with the given range. * Widening conversions will be applied as needed. */ @Override @SuppressWarnings("unchecked") public NumberRange<?> union(Range<?> range) { final Class<? extends Number> type = getWidestClass(elementClass, getElementClass(range)); range = convertAndCast((Range) range, (Class) type); return (NumberRange) castTo((Class) type).unionNC((Range)range); }
/** * Returns the range of values that are in this range but not in the given range. */ @Override @SuppressWarnings("unchecked") public NumberRange<?>[] subtract(Range<?> range) { Class<? extends Number> type = getWidestClass(elementClass, getElementClass(range)); return (NumberRange[]) castTo((Class) type) .subtractNC(convertAndCast((Range) range, (Class) type)); }
/** * Returns {@code true} if this range intersects the given range. */ @Override @SuppressWarnings("unchecked") public boolean intersects(Range<?> range) { final Class<? extends Number> type = getWidestClass(elementClass, getElementClass(range)); /* * The type bounds is actually <? extends Number & Comparable> but I'm unable to express * it it local variable as of Java 6. So we have to bypass the compiler check, but those * casts are actually safes, including Range because getElementClass(range) would have * throw an exception otherwise. */ range = convertAndCast((Range) range, (Class) type); return castTo((Class) type).intersectsNC((Range)range); }
/** * Returns true if the supplied range is fully contained within this range. */ //@Override public boolean contains(final Range range) { final Class type = ClassChanger.getWidestClass(getElementClass(), range.getElementClass()); return castTo(type)._contains(convertAndCast(range, type)); }
/** * Returns {@code true} if the supplied range is fully contained within this range. */ @Override @SuppressWarnings("unchecked") public boolean contains(Range<?> range) { final Class<? extends Number> type = getWidestClass(elementClass, getElementClass(range)); /* * The type bounds is actually <? extends Number & Comparable> but I'm unable to express * it it local variable as of Java 6. So we have to bypass the compiler check, but those * casts are actually safes, including Range because getElementClass(range) would have * throw an exception otherwise. */ range = convertAndCast((Range) range, (Class) type); return castTo((Class) type).containsNC((Range) range); }
/** * Returns true if this range intersects the given range. */ //@Override public boolean intersects(final Range range) { final Class type = ClassChanger.getWidestClass(getElementClass(), range.getElementClass()); return castTo(type)._intersects(convertAndCast(range, type)); }
/** * Returns the intersection of this range with the given range. Widening * conversions will be applied as needed. */ @Override @SuppressWarnings("unchecked") public NumberRange<?> intersect(Range<?> range) { final Class<? extends Number> rangeType = getElementClass(range); Class<? extends Number> type = getWidestClass(elementClass, rangeType); range = castTo((Class) type).intersectNC(convertAndCast((Range) range, (Class) type)); /* * Use a finer type capable to holds the result (since the intersection * may have reduced the range), but not finer than the finest type of * the ranges used in the intersection calculation. */ type = getFinestClass(elementClass, rangeType); if (range.minValue != null) { type = getWidestClass(type, getFinestClass(((Number) range.minValue).doubleValue())); } if (range.maxValue != null) { type = getWidestClass(type, getFinestClass(((Number) range.maxValue).doubleValue())); } return convertAndCast((Range) range, (Class) type); }
/** * Returns the union of this range with the given range. * Widening conversions will be applied as needed. * * @todo The return type will be changed to {@code NumberRange} when J2SE 1.5 * will be available. We should then search for NumberRange.warp(...) in all * client classes; some 'warp' may no longer be needed. */ //@Override public Range union(final Range range) { final Class type = ClassChanger.getWidestClass(getElementClass(), range.getElementClass()); return wrap(castTo(type)._union(convertAndCast(range, type))); }
/** * Returns the intersection of this range with the given range. * Widening conversions will be applied as needed. * * @todo The return type will be changed to {@code NumberRange} when J2SE 1.5 * will be available. We should then search for NumberRange.warp(...) in all * client classes; some 'warp' may no longer be needed. */ //@Override public Range intersect(final Range range) { Class type = ClassChanger.getWidestClass(getElementClass(), range.getElementClass()); final Range result = castTo(type)._intersect(convertAndCast(range, type)); /* * Use a finer type capable to holds the result (since the intersection may have * reduced the range), but not finer than the finest type of the ranges used in * the intersection calculation. */ type = ClassChanger.getFinestClass(getElementClass(), range.getElementClass()); return convertAndCast(result, ClassChanger.getWidestClass(type, ClassChanger.getWidestClass( ClassChanger.getFinestClass(((Number)result.getMinValue()).doubleValue()), ClassChanger.getFinestClass(((Number)result.getMaxValue()).doubleValue())))); }
/** * Returns the range of values that are in this range but not in the given range. * * @todo Consider changing the return type to {@code NumberRange} when we will be allowed * to compile for J2SE 1.5. */ //@Override public Range[] subtract(final Range range) { Class type = ClassChanger.getWidestClass(getElementClass(), range.getElementClass()); final Range[] result = castTo(type)._subtract(convertAndCast(range, type)); if (result != null) { for (int i=0; i<result.length; i++) { result[i] = wrap(result[i]); } } return result; }