Exemplo n.º 1
0
  @Override
  public AttributedCharacterIterator formatToCharacterIterator(Object object) {
    if (!(object instanceof Number)) {
      throw new IllegalArgumentException();
    }
    Number number = (Number) object;
    String text = null;
    StringBuffer attributes = new StringBuffer();

    if (number instanceof BigInteger) {
      BigInteger valBigInteger = (BigInteger) number;
      if (valBigInteger.compareTo(new BigInteger(String.valueOf(Long.MAX_VALUE))) > 0) {
        throw (new UnsupportedOperationException(
            "Number too big. BigInteger > Long.MAX_VALUE not yet supported."));
      }
      text =
          NativeDecimalFormat.format(this.addr, valBigInteger.longValue(), null, null, attributes);
    } else if (number instanceof BigDecimal) {
      BigDecimal valBigDecimal = (BigDecimal) number;
      if (valBigDecimal.compareTo(new BigDecimal(String.valueOf(Double.MAX_VALUE))) > 0) {
        throw (new UnsupportedOperationException(
            "Number too big. BigDecimal > Double.MAX_VALUE not yet supported."));
      }
      text =
          NativeDecimalFormat.format(
              this.addr, valBigDecimal.doubleValue(), null, null, attributes);
    } else {
      double dv = number.doubleValue();
      long lv = number.longValue();
      if (dv == lv) {
        text = NativeDecimalFormat.format(this.addr, lv, null, null, attributes);
      } else {
        text = NativeDecimalFormat.format(this.addr, dv, null, null, attributes);
      }
    }

    AttributedString as = new AttributedString(text.toString());

    String[] attrs = attributes.toString().split(";");
    // add NumberFormat field attributes to the AttributedString
    int size = attrs.length / 3;
    if (size * 3 != attrs.length) {
      return as.getIterator();
    }
    for (int i = 0; i < size; i++) {
      Format.Field attribute = getField(attrs[3 * i]);
      as.addAttribute(
          attribute,
          attribute,
          Integer.parseInt(attrs[3 * i + 1]),
          Integer.parseInt(attrs[3 * i + 2]));
    }

    // return the CharacterIterator from AttributedString
    return as.getIterator();
  }
Exemplo n.º 2
0
  @Override
  public StringBuffer format(Object value, StringBuffer buffer, FieldPosition field) {

    if (!(value instanceof Number)) {
      throw new IllegalArgumentException();
    }
    if (buffer == null || field == null) {
      throw new NullPointerException();
    }

    String fieldType = null;
    if (field != null) {
      fieldType = getFieldType(field.getFieldAttribute());
    }

    Number number = (Number) value;

    if (number instanceof BigInteger) {
      BigInteger valBigInteger = (BigInteger) number;
      String result =
          NativeDecimalFormat.format(
              this.addr, valBigInteger.toString(10), field, fieldType, null, 0);
      return buffer.append(result);
    } else if (number instanceof BigDecimal) {
      BigDecimal valBigDecimal = (BigDecimal) number;
      String result =
          NativeDecimalFormat.format(
              this.addr,
              valBigDecimal.unscaledValue().toString(10),
              field,
              fieldType,
              null,
              valBigDecimal.scale());
      return buffer.append(result);
    } else {
      double dv = number.doubleValue();
      long lv = number.longValue();
      if (dv == lv) {
        String result = NativeDecimalFormat.format(this.addr, lv, field, fieldType, null);
        return buffer.append(result);
      }
      String result = NativeDecimalFormat.format(this.addr, dv, field, fieldType, null);
      return buffer.append(result);
    }
  }
Exemplo n.º 3
0
  @Override
  public StringBuffer format(double value, StringBuffer buffer, FieldPosition field) {

    if (buffer == null) {
      throw new NullPointerException();
    }

    String fieldType = null;

    if (field != null) {
      fieldType = getFieldType(field.getFieldAttribute());
    }

    String result = NativeDecimalFormat.format(this.addr, value, field, fieldType, null);

    buffer.append(result.toCharArray(), 0, result.length());

    return buffer;
  }