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

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

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

Numbers.narrowestNumber介绍

[英]Returns the given number wrapped in the smallest class capable to hold the specified value. This method is equivalent to the following code, in a slightly more efficient way: java
[中]返回包装在能够保存指定值的最小类中的给定数字。这个方法相当于下面的代码,效率略高一些:java

代码示例

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

/**
   * 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

@Override java.lang.Number doConvert(String source) throws NumberFormatException {
    return Numbers.narrowestNumber(source);
  }
}

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

@Override java.lang.Number doConvert(String source) throws NumberFormatException {
    return Numbers.narrowestNumber(source);
  }
}

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

/**
 * Returns the smallest number capable to hold the specified value.
 *
 * @param  value  the value to be wrapped in a {@link Number}.
 * @return the narrowest type capable to hold the given value.
 * @throws NumberFormatException if the given value can not be parsed as a number.
 *
 * @see #narrowestNumber(Number)
 */
public static Number narrowestNumber(final String value) throws NumberFormatException {
  // Do not trim whitespaces. It is up to the caller to do that if he wants.
  // For such low level function, we are better to avoid hidden initiative.
  final int length = value.length();
  for (int i=0; i<length; i++) {
    final char c = value.charAt(i);
    if (c == '.' || c == 'e' || c == 'E') {
      return narrowestNumber(Double.parseDouble(value));
    }
  }
  return narrowestNumber(Long.parseLong(value));
}

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

/**
 * Returns the smallest number capable to hold the specified value.
 *
 * @param  value  the value to be wrapped in a {@link Number}.
 * @return the narrowest type capable to hold the given value.
 * @throws NumberFormatException if the given value can not be parsed as a number.
 *
 * @see #narrowestNumber(Number)
 */
public static Number narrowestNumber(final String value) throws NumberFormatException {
  // Do not trim whitespaces. It is up to the caller to do that if he wants.
  // For such low level function, we are better to avoid hidden initiative.
  final int length = value.length();
  for (int i=0; i<length; i++) {
    final char c = value.charAt(i);
    if (c == '.' || c == 'e' || c == 'E') {
      return narrowestNumber(Double.parseDouble(value));
    }
  }
  return narrowestNumber(Long.parseLong(value));
}

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

/**
 * 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

/**
 * 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: apache/sis

/**
 * Tests {@link Numbers#narrowestNumber(Number)}.
 */
@Test
@SuppressWarnings("UnnecessaryBoxing")
public void testNarrowestNumber() {
  assertEquals(Byte   .valueOf((byte)   127),  narrowestNumber(    127.0));
  assertEquals(Short  .valueOf((short)  128),  narrowestNumber(    128.0));
  assertEquals(Integer.valueOf(      100000),  narrowestNumber( 100000.0));
  assertEquals(Float  .valueOf(       10.5f),  narrowestNumber(     10.5));
  assertEquals(Byte   .valueOf((byte)  -128),  narrowestNumber(   -128  ));
  assertEquals(Short  .valueOf((short) -129),  narrowestNumber(   -129  ));
  assertEquals(Integer.valueOf(     -100000),  narrowestNumber(-100000  ));
  assertEquals(Integer.valueOf(1  << 30),      narrowestNumber((double) (1L << 30)));
  assertEquals(Float  .valueOf(1L << 40),      narrowestNumber((double) (1L << 40)));
  assertEquals(Double .valueOf(StrictMath.PI), narrowestNumber(StrictMath.PI));
}

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

/**
 * Returns the string representation of the given parameter default value,
 * or an empty string (never {@code null}) if none.
 */
private String getDefaultValue(final ParameterDescriptor<?> param, final String unit) {
  Object defaultValue = param.getDefaultValue();
  if (defaultValue != null) {
    if (defaultValue instanceof Number) {
      // Trim the fractional part if unnecessary (e.g. "0.0" to "0").
      defaultValue = Numbers.narrowestNumber((Number) defaultValue);
    } else if (defaultValue instanceof String) {
      return (String) defaultValue;
    }
  } else {
    if (ArraysExt.contains(defaultToLatitudeOfOrigin, param)) {
      return "Latitude of origin";
    } else if (ArraysExt.contains(defaultToStandardParallel1, param)) {
      return "Standard parallel 1";
    } else if (ArraysExt.contains(defaultToAzimuth, param)) {
      return "Azimuth of initial line";
    } else if (param.getValueClass() == Boolean.class) {
      defaultValue = Boolean.FALSE;
    }
  }
  return (defaultValue != null) ? defaultValue + unit : "";
}

相关文章