org.apache.sis.util.Numbers.cast()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(13.8k)|赞(0)|评价(0)|浏览(160)

本文整理了Java中org.apache.sis.util.Numbers.cast()方法的一些代码示例,展示了Numbers.cast()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Numbers.cast()方法的具体详情如下:
包路径:org.apache.sis.util.Numbers
类名称:Numbers
方法名:cast

Numbers.cast介绍

[英]Casts a number to the specified type. The target type can be one of Byte, Short, Integer, Long, Float, Double, BigInteger or BigDecimal. This method makes the following choice:

  • If the given value is null or an instance of the given type, then it is returned unchanged.
  • Otherwise if the given type is Double.class, then this method returns Double#valueOf(double)(number.doubleValue());
  • Otherwise if the given type is Float.class, then this method returns Float#valueOf(float)(number.floatValue());
  • And likewise for all remaining known types.
    This method does not verify if the given type is wide enough for the given value, because the type has typically been calculated by #widestClass(Class,Class)or #narrowestClass(Number). If nevertheless the given type is not wide enough, then the behavior depends on the implementation of the corresponding Number.fooValue() method - typically, the value is just rounded or truncated.
    [中]将数字强制转换为指定的类型。目标类型可以是Byte、Short、Integer、Long、Float、Double、BigInteger或BigDecimal。此方法可进行以下选择:
    *如果给定的值为null或给定类型的实例,则返回时将保持不变。
    *否则,如果给定的类型是Double。类,然后该方法返回[$0$];
    *否则,如果给定类型为Float。类,然后该方法返回Float#valueOf(float)(number.floatValue())
    *同样,对于所有已知的类型。
    此方法不会验证给定类型是否足够宽,以满足给定值的要求,因为该类型通常是由#WidetClass(类,类)或#SlowestClass(数)计算的。然而,如果给定的类型不够宽,那么行为取决于相应数字的实现。fooValue()方法-通常,该值只是四舍五入或截断。

代码示例

代码示例来源:origin: org.apache.sis.core/sis-utility

/**
 * Constructs a range with the same values than the specified range, casted to the specified type.
 *
 * @param  type   the element type, usually one of {@link Byte}, {@link Short},
 *                {@link Integer}, {@link Long}, {@link Float} or {@link Double}.
 * @param  range  the range to copy. The elements must be {@link Number} instances.
 * @throws IllegalArgumentException if the given type is not one of the primitive wrappers for numeric types.
 */
NumberRange(final Class<E> type, final Range<? extends Number> range)
    throws IllegalArgumentException
{
  super(type, Numbers.cast(range.minValue, type), range.isMinIncluded,
        Numbers.cast(range.maxValue, type), range.isMaxIncluded);
}

代码示例来源:origin: apache/sis

/**
 * Constructs a range with the same values than the specified range, casted to the specified type.
 *
 * @param  type   the element type, usually one of {@link Byte}, {@link Short},
 *                {@link Integer}, {@link Long}, {@link Float} or {@link Double}.
 * @param  range  the range to copy. The elements must be {@link Number} instances.
 * @throws IllegalArgumentException if the given type is not one of the primitive wrappers for numeric types.
 */
NumberRange(final Class<E> type, final Range<? extends Number> range)
    throws IllegalArgumentException
{
  super(type, Numbers.cast(range.minValue, type), range.isMinIncluded,
        Numbers.cast(range.maxValue, type), range.isMaxIncluded);
}

代码示例来源:origin: org.apache.sis.core/sis-utility

value = Numbers.cast(value, type);
if (minValue != null) {
  @SuppressWarnings("unchecked")
  final int c = ((Comparable) Numbers.cast(minValue, type)).compareTo(value);
  if (isMinIncluded ? (c > 0) : (c >= 0)) {
    return false;
  final int c = ((Comparable) Numbers.cast(maxValue, type)).compareTo(value);
  if (isMaxIncluded ? (c < 0) : (c <= 0)) {
    return false;

代码示例来源:origin: apache/sis

value = Numbers.cast(value, type);
if (minValue != null) {
  @SuppressWarnings("unchecked")
  final int c = ((Comparable) Numbers.cast(minValue, type)).compareTo(value);
  if (isMinIncluded ? (c > 0) : (c >= 0)) {
    return false;
  final int c = ((Comparable) Numbers.cast(maxValue, type)).compareTo(value);
  if (isMaxIncluded ? (c < 0) : (c <= 0)) {
    return false;

代码示例来源:origin: org.apache.sis.core/sis-utility

/**
 * Converts the given value to the a {@link #elementType} type.
 */
@SuppressWarnings("unchecked")
private Object convert(final Object value) throws UnconvertibleObjectException {
  if (value == null || elementType.isInstance(value)) {
    return value;
  }
  if (value instanceof Number && Number.class.isAssignableFrom(elementType)) {
    return Numbers.cast((Number) value, (Class<? extends Number>) elementType);
  }
  throw new UnconvertibleObjectException(Errors.format(
      Errors.Keys.IllegalClass_2, elementType, value.getClass()));
}

代码示例来源:origin: apache/sis

/**
 * Converts the given value to the a {@link #elementType} type.
 */
@SuppressWarnings("unchecked")
private Object convert(final Object value) throws UnconvertibleObjectException {
  if (value == null || elementType.isInstance(value)) {
    return value;
  }
  if (value instanceof Number && Number.class.isAssignableFrom(elementType)) {
    return Numbers.cast((Number) value, (Class<? extends Number>) elementType);
  }
  throw new UnconvertibleObjectException(Errors.format(
      Errors.Keys.IllegalClass_2, elementType, value.getClass()));
}

代码示例来源:origin: org.apache.sis.core/sis-utility

/**
 * Returns the increment between all consecutive values if this increment is constant, or {@code null} otherwise.
 */
@Override
public Number increment(final double tolerance) {
  Number inc = first.increment(tolerance);
  if (inc != null) {
    Number check = second.increment(tolerance);
    if (check != null) {
      final Class<? extends Number> type = Numbers.widestClass(inc.getClass(), check.getClass());
      inc   = Numbers.cast(inc,   type);
      check = Numbers.cast(check, type);
      if (inc.equals(check)) {
        return inc;
      }
    }
  }
  return null;
}

代码示例来源:origin: apache/sis

/**
 * Returns the increment between all consecutive values if this increment is constant, or {@code null} otherwise.
 */
@Override
public Number increment(final double tolerance) {
  Number inc = first.increment(tolerance);
  if (inc != null) {
    Number check = second.increment(tolerance);
    if (check != null) {
      final Class<? extends Number> type = Numbers.widestClass(inc.getClass(), check.getClass());
      inc   = Numbers.cast(inc,   type);
      check = Numbers.cast(check, type);
      if (inc.equals(check)) {
        return inc;
      }
    }
  }
  return null;
}

代码示例来源:origin: org.apache.sis.core/sis-utility

public T apply(final S source) {
  final double sourceValue = source.doubleValue();
  T target = Numbers.cast(source, targetClass);
  final double targetValue = target.doubleValue();
  if (Double.doubleToLongBits(targetValue) != Double.doubleToLongBits(sourceValue)) {
    if (!(delta < 0.5)) { // Use '!' for catching NaN.
      if (delta < 1) {
        target = Numbers.cast(Math.round(sourceValue), targetClass);
      } else {

代码示例来源:origin: apache/sis

public T apply(final S source) {
  final double sourceValue = source.doubleValue();
  T target = Numbers.cast(source, targetClass);
  final double targetValue = target.doubleValue();
  if (Double.doubleToLongBits(targetValue) != Double.doubleToLongBits(sourceValue)) {
    if (!(delta < 0.5)) { // Use '!' for catching NaN.
      if (delta < 1) {
        target = Numbers.cast(Math.round(sourceValue), targetClass);
      } else {

代码示例来源:origin: org.apache.sis.core/sis-referencing

/**
 * Sets the parameter value as an integer.
 *
 * <p>The default implementation wraps the given integer in an object of the type specified by the
 * {@linkplain #getDescriptor() descriptor}, then delegates to {@link #setValue(Object, Unit)}.</p>
 *
 * @param  newValue  the parameter value.
 * @throws InvalidParameterValueException if the integer type is inappropriate for this parameter,
 *         or if the value is illegal for some other reason (for example a value out of range).
 *
 * @see #intValue()
 */
@Override
public void setValue(final int newValue) throws InvalidParameterValueException {
  Number n = newValue;
  final Class<T> valueClass = descriptor.getValueClass();
  if (Number.class.isAssignableFrom(valueClass)) {
    @SuppressWarnings("unchecked")
    final Number c = Numbers.cast(newValue, (Class<? extends Number>) valueClass);
    if (c.intValue() == newValue) {
      n = c;
    }
  }
  setValue(n, unit);
  // Use 'unit' instead than 'getUnit()' despite class Javadoc claims because units are not expected
  // to be involved in this method. We just want the current unit setting to be unchanged.
}

代码示例来源:origin: apache/sis

/**
 * Sets the parameter value as an integer.
 *
 * <p>The default implementation wraps the given integer in an object of the type specified by the
 * {@linkplain #getDescriptor() descriptor}, then delegates to {@link #setValue(Object, Unit)}.</p>
 *
 * @param  newValue  the parameter value.
 * @throws InvalidParameterValueException if the integer type is inappropriate for this parameter,
 *         or if the value is illegal for some other reason (for example a value out of range).
 *
 * @see #intValue()
 */
@Override
public void setValue(final int newValue) throws InvalidParameterValueException {
  Number n = newValue;
  final Class<T> valueClass = descriptor.getValueClass();
  if (Number.class.isAssignableFrom(valueClass)) {
    @SuppressWarnings("unchecked")
    final Number c = Numbers.cast(newValue, (Class<? extends Number>) valueClass);
    if (c.intValue() == newValue) {
      n = c;
    }
  }
  setValue(n, unit);
  // Use 'unit' instead than 'getUnit()' despite class Javadoc claims because units are not expected
  // to be involved in this method. We just want the current unit setting to be unchanged.
}

代码示例来源:origin: org.apache.sis.core/sis-utility

/**
 * Constructs a range of the given type with values from the given annotation.
 * This constructor does not verify if the given type is wide enough for the values of
 * the given annotation, because those information are usually static. If nevertheless
 * the given type is not wide enough, then the values are truncated in the same way
 * than the Java language casts primitive types.
 *
 * @param  type   the element type, restricted to one of {@link Byte}, {@link Short},
 *                {@link Integer}, {@link Long}, {@link Float} or {@link Double}.
 * @param  range  the range of values.
 * @throws IllegalArgumentException if the given type is not one of the primitive wrappers for numeric types.
 */
public NumberRange(final Class<E> type, final ValueRange range) throws IllegalArgumentException {
  super(type, Numbers.cast(valueOf("minimum", range.minimum(), Double.NEGATIVE_INFINITY), type), range.isMinIncluded(),
        Numbers.cast(valueOf("maximum", range.maximum(), Double.POSITIVE_INFINITY), type), range.isMaxIncluded());
}

代码示例来源:origin: apache/sis

/**
 * Tests {@link Numbers#cast(Number, Class)}.
 */
@Test
public void testCast() {
  @SuppressWarnings("UnnecessaryBoxing")
  final Integer value = new Integer(10); // Intentionally a new instance.
  assertEquals(Byte   .valueOf((byte)   10), cast(value, Byte   .class));
  assertEquals(Short  .valueOf((short)  10), cast(value, Short  .class));
  assertSame  (value,                        cast(value, Integer.class));
  assertEquals(Long   .valueOf((long)   10), cast(value, Long   .class));
  assertEquals(Float  .valueOf((float)  10), cast(value, Float  .class));
  assertEquals(Double .valueOf((double) 10), cast(value, Double .class));
}

代码示例来源:origin: apache/sis

/**
 * Constructs a range of the given type with values from the given annotation.
 * This constructor does not verify if the given type is wide enough for the values of
 * the given annotation, because those information are usually static. If nevertheless
 * the given type is not wide enough, then the values are truncated in the same way
 * than the Java language casts primitive types.
 *
 * @param  type   the element type, restricted to one of {@link Byte}, {@link Short},
 *                {@link Integer}, {@link Long}, {@link Float} or {@link Double}.
 * @param  range  the range of values.
 * @throws IllegalArgumentException if the given type is not one of the primitive wrappers for numeric types.
 */
public NumberRange(final Class<E> type, final ValueRange range) throws IllegalArgumentException {
  super(type, Numbers.cast(valueOf("minimum", range.minimum(), Double.NEGATIVE_INFINITY), type), range.isMinIncluded(),
        Numbers.cast(valueOf("maximum", range.maximum(), Double.POSITIVE_INFINITY), type), range.isMaxIncluded());
}

代码示例来源:origin: org.apache.sis.core/sis-utility

/**
 * Constructs a range using the smallest type of {@link Number} that can hold the given values.
 * This method performs the same work than {@link NumberRange#createBestFit
 * NumberRange.createBestFit(…)} with an additional {@code unit} argument.
 *
 * <p>This method may return a shared instance, at implementation choice.</p>
 *
 * @param  minValue       the minimal value, or {@code null} if none.
 * @param  isMinIncluded  {@code true} if the minimal value is inclusive, or {@code false} if exclusive.
 * @param  maxValue       the maximal value, or {@code null} if none.
 * @param  isMaxIncluded  {@code true} if the maximal value is inclusive, or {@code false} if exclusive.
 * @param  unit           the unit of measurement, or {@code null} if unknown.
 * @return the new range, or {@code null} if both {@code minValue} and {@code maxValue} are {@code null}.
 *
 * @see NumberRange#createBestFit(Number, boolean, Number, boolean)
 */
@SuppressWarnings({"rawtypes","unchecked"})
public static MeasurementRange<?> createBestFit(final Number minValue, final boolean isMinIncluded,
    final Number maxValue, final boolean isMaxIncluded, final Unit<?> unit)
{
  final Class<? extends Number> type = Numbers.widestClass(
      Numbers.narrowestClass(minValue), Numbers.narrowestClass(maxValue));
  if (type == null) {
    return null;
  }
  return unique(new MeasurementRange(type,
      Numbers.cast(minValue, type), isMinIncluded,
      Numbers.cast(maxValue, type), isMaxIncluded, unit));
}

代码示例来源:origin: apache/sis

/**
 * Constructs a range using the smallest type of {@link Number} that can hold the given values.
 * This method performs the same work than {@link NumberRange#createBestFit
 * NumberRange.createBestFit(…)} with an additional {@code unit} argument.
 *
 * <p>This method may return a shared instance, at implementation choice.</p>
 *
 * @param  minValue       the minimal value, or {@code null} if none.
 * @param  isMinIncluded  {@code true} if the minimal value is inclusive, or {@code false} if exclusive.
 * @param  maxValue       the maximal value, or {@code null} if none.
 * @param  isMaxIncluded  {@code true} if the maximal value is inclusive, or {@code false} if exclusive.
 * @param  unit           the unit of measurement, or {@code null} if unknown.
 * @return the new range, or {@code null} if both {@code minValue} and {@code maxValue} are {@code null}.
 *
 * @see NumberRange#createBestFit(Number, boolean, Number, boolean)
 */
@SuppressWarnings({"rawtypes","unchecked"})
public static MeasurementRange<?> createBestFit(final Number minValue, final boolean isMinIncluded,
    final Number maxValue, final boolean isMaxIncluded, final Unit<?> unit)
{
  final Class<? extends Number> type = Numbers.widestClass(
      Numbers.narrowestClass(minValue), Numbers.narrowestClass(maxValue));
  if (type == null) {
    return null;
  }
  return unique(new MeasurementRange(type,
      Numbers.cast(minValue, type), isMinIncluded,
      Numbers.cast(maxValue, type), isMaxIncluded, unit));
}

代码示例来源:origin: apache/sis

return null;
final Number min = Numbers.cast(minValue, type);
final Number max = Objects.equals(minValue, maxValue) ? min : Numbers.cast(maxValue, type);
return unique(new NumberRange(type, min, isMinIncluded, max, isMaxIncluded));

代码示例来源:origin: org.apache.sis.core/sis-utility

Numbers.narrowestClass(minValue), Numbers.narrowestClass(maxValue));
return (type == null) ? null : unique(new NumberRange(type,
    Numbers.cast(minValue, type), isMinIncluded,
    Numbers.cast(maxValue, type), isMaxIncluded));

代码示例来源:origin: org.apache.sis.core/sis-utility

Numbers.cast(minimum, type), minInc,
Numbers.cast(maximum, type), maxInc, targetUnit);

相关文章