示例#1
0
 /**
  * Gets the fraction as a <tt>float</tt>. This calculates the fraction as the numerator divided by
  * denominator.
  *
  * @return the fraction as a <tt>float</tt>.
  * @see java.lang.Number#floatValue()
  */
 @Override
 public float floatValue() {
   float result = numerator.floatValue() / denominator.floatValue();
   if (Double.isNaN(result)) {
     // Numerator and/or denominator must be out of range:
     // Calculate how far to shift them to put them in range.
     int shift = Math.max(numerator.bitLength(), denominator.bitLength()) - Float.MAX_EXPONENT;
     result =
         numerator.shiftRight(shift).floatValue() / denominator.shiftRight(shift).floatValue();
   }
   return result;
 }
  // 20. purpose: getFloat with Defined integer Property
  public void testGetFloatConversionFromDefinedIntegerProperty() {
    // dataObject's type add int property
    property_c = new SDOProperty(aHelperContext);
    property_c.setName(PROPERTY_NAME_C);
    property_c.setType(SDOConstants.SDO_INTEGER);
    type_c.addDeclaredProperty(property_c);
    dataObject_c._setType(type_c);

    BigInteger bi = new BigInteger("12");
    float delta = 0;
    dataObject_a.setBigInteger(propertyPath_a_b_c, bi); // add it to instance list

    this.assertEquals(bi.floatValue(), dataObject_a.getFloat(propertyPath_a_b_c), delta);
  }
示例#3
0
 public static TableCellBorder getTableCellBorder(CTBorder border, boolean fromTableCell) {
   if (border != null) {
     boolean noBorder = (STBorder.NONE == border.getVal() || STBorder.NIL == border.getVal());
     if (noBorder) {
       return new TableCellBorder(!noBorder, fromTableCell);
     }
     Float borderSize = null;
     BigInteger size = border.getSz();
     if (size != null) {
       // http://officeopenxml.com/WPtableBorders.php
       // if w:sz="4" => 1/4 points
       borderSize = size.floatValue() / 8f;
     }
     Color borderColor = ColorHelper.getBorderColor(border);
     return new TableCellBorder(borderSize, borderColor, fromTableCell);
   }
   return null;
 }
 /**
  * Increments value in each column. If the value is bounded type (e.g. short) it will cycle the
  * values.
  */
 private void generateRow() {
   List<Object> newRow = new ArrayList<Object>(types.size());
   String incrementedString = incrementString(staticStringValue, rowNumber);
   for (Class<?> type : types) {
     if (type.equals(Integer.class)) {
       newRow.add(rowNumber.intValue());
     } else if (type.equals(java.lang.Short.class)) {
       newRow.add(rowNumber.shortValue());
     } else if (type.equals(java.lang.Long.class)) {
       newRow.add(rowNumber.longValue());
     } else if (type.equals(java.lang.Float.class)) {
       newRow.add(rowNumber.floatValue() / 10);
     } else if (type.equals(java.lang.Double.class)) {
       newRow.add(rowNumber.doubleValue() / 10);
     } else if (type.equals(java.lang.Character.class)) {
       newRow.add((char) ((((rowNumber.byteValue() & 0xff) + 2) % 26) + 97));
     } else if (type.equals(java.lang.Byte.class)) {
       newRow.add(rowNumber.byteValue());
     } else if (type.equals(java.lang.Boolean.class)) {
       newRow.add(rowNumber.intValue() % 2 != 0);
     } else if (type.equals(java.math.BigInteger.class)) {
       newRow.add(rowNumber);
     } else if (type.equals(java.math.BigDecimal.class)) {
       newRow.add(new BigDecimal(rowNumber, 1));
     } else if (type.equals(java.sql.Date.class)) {
       newRow.add(new Date(DAY_SECONDS * 1000 * (rowNumber.intValue() % DATE_PERIOD)));
     } else if (type.equals(java.sql.Time.class)) {
       newRow.add(new Time((rowNumber.intValue() % DAY_SECONDS) * 1000));
     } else if (type.equals(java.sql.Timestamp.class)) {
       newRow.add(new Timestamp(rowNumber.longValue()));
     } else if (type.equals(TypeFacility.RUNTIME_TYPES.CLOB)) {
       newRow.add(
           this.config.getTypeFacility().convertToRuntimeType(incrementedString.toCharArray()));
     } else if (type.equals(TypeFacility.RUNTIME_TYPES.BLOB)
         || type.equals(TypeFacility.RUNTIME_TYPES.VARBINARY)) {
       newRow.add(
           this.config.getTypeFacility().convertToRuntimeType(incrementedString.getBytes()));
     } else {
       newRow.add(incrementedString);
     }
   }
   row = newRow;
 }
示例#5
0
 @Override
 public float floatValue() {
   return value.floatValue();
 }
示例#6
0
 /**
  * Gets the fraction as a <tt>float</tt>. This calculates the fraction as the numerator divided by
  * denominator.
  *
  * @return the fraction as a <tt>float</tt>.
  * @see java.lang.Number#floatValue()
  */
 @Override
 public float floatValue() {
   return numerator.floatValue() / denominator.floatValue();
 }
示例#7
0
文件: DataType.java 项目: inexas/oak
  /**
   * Type converts values to other classes. For example an Integer can be converted to a Long.
   *
   * @param <T> Data Type.
   * @param value The value to be converted.
   * @param to The class to be converted to. Must be one of the Java types corresponding to the
   *     DataTypes.
   * @return The converted value.
   * @throws TypeMismatchException Thrown desired class is incompatible with the source class.
   * @throws OverflowException Thrown only on narrowing value conversions, e.g from a BigInteger to
   *     an Integer.
   */
  @Nullable
  public static <T> T convert(Object value, Class<?> to)
      throws TypeMismatchException, OverflowException {
    final Object result;

    if (value == null || value.getClass() == to) {
      result = value;
    } else {
      final Class<?> from = value.getClass();
      try {
        if (from == Integer.class) { // Integer -> ...
          if (to == Long.class) {
            result = new Long(((Number) value).longValue());
          } else if (to == BigInteger.class) {
            result = BigInteger.valueOf(((Number) value).intValue());
          } else if (to == Float.class) {
            result = new Float(((Number) value).floatValue());
          } else if (to == Double.class) {
            result = new Double(((Number) value).doubleValue());
          } else if (to == BigDecimal.class) {
            // Use intValue() to avoid precision errors
            result = new BigDecimal(((Number) value).intValue());
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else if (from == Long.class) { // Long -> ...
          if (to == Integer.class) {
            final long l = ((Long) value).longValue();
            if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
              throw new OverflowException((Number) value, to);
            }
            result = new Integer((int) l);
          } else if (to == BigInteger.class) {
            result = BigInteger.valueOf(((Number) value).longValue());
          } else if (to == Float.class) {
            result = new Float(((Number) value).floatValue());
          } else if (to == Double.class) {
            result = new Double(((Number) value).doubleValue());
          } else if (to == BigDecimal.class) {
            // Use longValue() to avoid precision errors
            result = new BigDecimal(((Number) value).longValue());
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else if (from == BigInteger.class) { // BigInteger -> ...
          final BigInteger bi = (BigInteger) value;
          if (to == Integer.class) {
            result = new Integer(bi.intValueExact());
          } else if (to == Long.class) {
            result = new Long(bi.longValueExact());
          } else if (to == Float.class) {
            final float f1 = bi.floatValue();
            if (f1 == Float.NEGATIVE_INFINITY || f1 == Float.POSITIVE_INFINITY) {
              throw new OverflowException(bi, to);
            }
            result = new Float(f1);
          } else if (to == Double.class) {
            final double d = bi.doubleValue();
            if (d == Double.NEGATIVE_INFINITY || d == Double.POSITIVE_INFINITY) {
              throw new OverflowException(bi, to);
            }
            result = new Double(d);
          } else if (to == BigDecimal.class) {
            result = new BigDecimal(bi);
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else if (from == Float.class) { // Float -> ...
          final float fl = ((Float) value).floatValue();
          if (to == Integer.class) {
            if (fl < Integer.MIN_VALUE || fl > Integer.MAX_VALUE | fl % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            result = new Integer((int) fl);
          } else if (to == Long.class) {
            if (fl < Long.MIN_VALUE || fl > Long.MAX_VALUE | fl % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            result = new Long((long) fl);
          } else if (to == BigInteger.class) {
            if (fl % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            final BigDecimal bd = BigDecimal.valueOf(fl);
            result = bd.toBigInteger();
          } else if (to == Double.class) {
            result = new Double(((Number) value).doubleValue());
          } else if (to == BigDecimal.class) {
            result = BigDecimal.valueOf(fl);
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else if (from == Double.class) { // Double -> ...
          final double d = ((Double) value).doubleValue();
          if (to == Integer.class) {
            if (d < Integer.MIN_VALUE || d > Integer.MAX_VALUE || d % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            result = new Integer((int) d);
          } else if (to == Long.class) { // OK
            if (d < Long.MIN_VALUE || d > Long.MAX_VALUE || d % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            result = new Long((int) d);
          } else if (to == BigInteger.class) { // OK
            if (d % 1 > 0) {
              throw new OverflowException((Number) value, to);
            }
            final BigDecimal bd = BigDecimal.valueOf(d);
            result = bd.toBigInteger();
          } else if (to == Float.class) { // OK
            if (d < -Float.MAX_VALUE || d > Float.MAX_VALUE) {
              throw new OverflowException((Number) value, to);
            }
            result = new Float((float) d);
          } else if (to == BigDecimal.class) { // OK
            result = BigDecimal.valueOf(d);
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else if (from == BigDecimal.class) { // BigDecimal -> ...
          final BigDecimal bd = (BigDecimal) value;
          if (to == Integer.class) { // OK
            result = new Integer(bd.intValueExact());
          } else if (to == Long.class) { // OK
            result = new Long(bd.longValueExact());
          } else if (to == BigInteger.class) { // OK
            // BigDecimal modulus
            final BigDecimal remainder = bd.remainder(BigDecimal.ONE);
            if (!remainder.equals(BigDecimal.ZERO)) {
              throw new OverflowException(bd, to);
            }
            result = bd.toBigInteger();
          } else if (to == Float.class) { // OK
            if (bd.compareTo(BigDecimal_MIN_FLOAT) < 0 || bd.compareTo(BigDecimal_MAX_FLOAT) > 0) {
              throw new OverflowException(bd, to);
            }
            result = new Float(bd.floatValue());
          } else if (to == Double.class) { // OK
            if (bd.compareTo(BigDecimal_MIN_DOUBLE) < 0
                || bd.compareTo(BigDecimal_MAX_DOUBLE) > 0) {
              throw new OverflowException(bd, to);
            }
            result = new Double(bd.doubleValue());
          } else {
            throw new TypeMismatchException(value.getClass(), to);
          }

        } else {
          throw new UnexpectedException("convert: " + from.getName());
        }
      } catch (final ArithmeticException e) {
        // Thrown by intValueExact() etc.
        throw new OverflowException((Number) value, to);
      }
    }

    @SuppressWarnings("unchecked")
    final T t = (T) result;
    return t;
  }