org.apache.sis.util.Numbers类的使用及代码示例

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

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

Numbers介绍

[英]Static methods working with Number objects, and a few primitive types by extension.
[中]静态方法处理数字对象,以及扩展的一些基本类型。

代码示例

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

/**
 * Returns the kind of value provided in the extended element.
 * This is a generic code that describe the element type.
 * For more accurate information, see {@link #getElementType()}.
 */
@Override
public Datatype getDataType() {
  if (CharSequence.class.isAssignableFrom(elementType)) return Datatype.CHARACTER_STRING;
  if (CodeList    .class.isAssignableFrom(elementType)) return Datatype.CODE_LIST;
  if (Enum        .class.isAssignableFrom(elementType)) return Datatype.ENUMERATION;
  if (Numbers.isInteger(elementType)) {
    return Datatype.INTEGER;
  }
  // TODO: check the org.opengis.annotation.Classifier annotation here.
  return Datatype.TYPE_CLASS;
}

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

/**
 * Returns the elements of the given collection as an array. This method can be used when the {@code valueClass}
 * argument is not known at compile-time. If the {@code valueClass} is known at compile-time, then callers should
 * use {@link Collection#toArray(Object[])} instead.
 *
 * @param  <T>         the compile-time value of {@code valueClass}.
 * @param  collection  the collection from which to get the elements.
 * @param  valueClass  the runtime type of collection elements.
 * @return the collection elements as an array, or {@code null} if {@code collection} is null.
 *
 * @since 0.6
 */
@SuppressWarnings("unchecked")
public static <T> T[] toArray(final Collection<T> collection, final Class<T> valueClass) {
  assert Numbers.primitiveToWrapper(valueClass) == valueClass : valueClass;
  if (collection != null) {
    return collection.toArray((T[]) Array.newInstance(valueClass, collection.size()));
  }
  return null;
}

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

return false;
final Class<? extends Number> type = Numbers.widestClass(elementType, value.getClass());
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

/**
 * Parses the given string. Callers shall catch the {@link NumberFormatException}
 * and handle the error according the caller's method contract.
 *
 * @throws NumberFormatException if the parsing failed.
 */
private Object valueOf(final String source) throws NumberFormatException {
  return (type != Number.class) ? Numbers.valueOf(source, type) : Numbers.narrowestNumber(source);
}

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

/**
 * Verifies that a value of the given type can be casted to the expected type.
 * The expected type must be one of the {@link Numbers} constants.
 */
final void verifyType(final Class<? extends Number> type, final byte expected) {
  final byte t = Numbers.getEnumConstant(type);
  if (t < Numbers.BYTE || t > expected) {
    throw new ClassCastException(Errors.format(Errors.Keys.CanNotConvertFromType_2,
      type, Numbers.wrapperToPrimitive(getElementType())));
  }
}

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

if (value instanceof Number) {
  final Number n = (Number) value;
  if (Numbers.isInteger(n.getClass())) {
    final int xmlValue = n.intValue();
    if (xmlValue >= 0 && xmlValue == n.doubleValue()) {
  return ObjectConverters.convert(value, URI.class);
final Class<?> type = Numbers.primitiveToWrapper(value.getClass().getComponentType());
if (type != null && Number.class.isAssignableFrom(type)) {
  if (Numbers.isInteger(type)) {
    return new IntegerList(value);

代码示例来源: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 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

/** Returns the increment between all consecutive values */
@Override public Number increment(final double tolerance) {
  return Numbers.wrap(increment, type);
}

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

/**
 * Declares this converter as a injective or surjective function,
 * depending on whether conversions loose information or not.
 */
@Override
public Set<FunctionProperty> properties() {
  return EnumSet.of(Numbers.widestClass(sourceClass, targetClass) == targetClass
             ? FunctionProperty.INJECTIVE : FunctionProperty.SURJECTIVE,
           FunctionProperty.ORDER_PRESERVING, FunctionProperty.INVERTIBLE);
}

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

final boolean tb = minInc;  minInc  = maxInc;  maxInc  = tb;
if (Numbers.isInteger(type)) {
  minInc &= (minimum == (minimum = Math.floor(minimum)));
  maxInc &= (maximum == (maximum = Math.ceil (maximum)));
    Numbers.cast(minimum, type), minInc,
    Numbers.cast(maximum, type), maxInc, targetUnit);

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

newValues[0] = Numbers.valueOfNil(targetType);
  targetType = Numbers.primitiveToWrapper(targetType);
} else {

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

/**
   * Returns {@code true} if the given type is one of the types supported
   * by {@link NumberConverter}.
   */
  private static boolean isSupportedNumber(final Class<?> type) {
    final int code = Numbers.getEnumConstant(type);
    return (code >= Numbers.BYTE && code <= Numbers.BIG_DECIMAL);
  }
}

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

baseValueClass = (baseValueClass != null) ? Numbers.wrapperToPrimitive(baseValueClass) : Object.class;
return types;

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

switch (getEnumConstant(type)) {
  case BYTE:        number = (N) Byte      .valueOf((byte)  value); break;
  case SHORT:       number = (N) Short     .valueOf((short) value); break;
  case BIG_INTEGER: number = (N) BigInteger.valueOf((long) value); break;
  case BIG_DECIMAL: return   (N) BigDecimal.valueOf(value); // No need to verify.
  default: throw unknownType(type);

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

/**
   * Converts the given number to a {@code Comparable} if its type is different.
   */
  @Override
  public java.lang.Comparable<?> apply(final Number source) {
    if (source == null || source instanceof java.lang.Comparable<?>) {
      return (java.lang.Comparable<?>) source;
    }
    return (java.lang.Comparable<?>) Numbers.narrowestNumber(source);
  }
}

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

/**
 * Returns a "nil" value. This is used for creating empty ranges.
 */
private Object valueOfNil() {
  Object value = Numbers.valueOfNil(elementType);
  if (value == null) {
    if (Date.class.isAssignableFrom(elementType)) {
      value = new Date();
    } else {
      value = 0;
    }
  }
  return convert(value);
}

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

/**
 * Tests {@link Numbers#isFloat(Class)}.
 */
@Test
public void testIsFloat() {
  assertFalse(isFloat(Byte     .TYPE));
  assertFalse(isFloat(Short    .TYPE));
  assertFalse(isFloat(Integer  .TYPE));
  assertFalse(isFloat(Long     .TYPE));
  assertTrue (isFloat(Float    .TYPE));
  assertTrue (isFloat(Double   .TYPE));
  assertFalse(isFloat(Byte     .class));
  assertFalse(isFloat(Short    .class));
  assertFalse(isFloat(Integer  .class));
  assertFalse(isFloat(Long     .class));
  assertTrue (isFloat(Float    .class));
  assertTrue (isFloat(Double   .class));
  assertFalse(isFloat(String   .class));
  assertFalse(isFloat(Character.class));
}

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

if (value instanceof Number) {
  final Number n = (Number) value;
  if (Numbers.isInteger(n.getClass())) {
    final int xmlValue = n.intValue();
    if (xmlValue >= 0 && xmlValue == n.doubleValue()) {
  return ObjectConverters.convert(value, URI.class);
final Class<?> type = Numbers.primitiveToWrapper(value.getClass().getComponentType());
if (type != null && Number.class.isAssignableFrom(type)) {
  if (Numbers.isInteger(type)) {
    return new IntegerList(value);

代码示例来源: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);
}

相关文章