java.text.FieldPosition Java Examples

The following examples show how to use java.text.FieldPosition. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BigFractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats an object and appends the result to a StringBuffer.
 * <code>obj</code> must be either a  {@link BigFraction} object or a
 * {@link BigInteger} object or a {@link Number} object. Any other type of
 * object will result in an {@link IllegalArgumentException} being thrown.
 *
 * @param obj the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
 * @throws MathIllegalArgumentException if <code>obj</code> is not a valid type.
 */
@Override
public StringBuffer format(final Object obj,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    final StringBuffer ret;
    if (obj instanceof BigFraction) {
        ret = format((BigFraction) obj, toAppendTo, pos);
    } else if (obj instanceof BigInteger) {
        ret = format(new BigFraction((BigInteger) obj), toAppendTo, pos);
    } else if (obj instanceof Number) {
        ret = format(new BigFraction(((Number) obj).doubleValue()),
                     toAppendTo, pos);
    } else {
        throw new MathIllegalArgumentException(LocalizedFormats.CANNOT_FORMAT_OBJECT_TO_FRACTION);
    }

    return ret;
}
 
Example #2
Source File: BigFractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats an object and appends the result to a StringBuffer.
 * <code>obj</code> must be either a  {@link BigFraction} object or a
 * {@link BigInteger} object or a {@link Number} object. Any other type of
 * object will result in an {@link IllegalArgumentException} being thrown.
 *
 * @param obj the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
 * @throws IllegalArgumentException is <code>obj</code> is not a valid type.
 */
@Override
public StringBuffer format(final Object obj,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    final StringBuffer ret;
    if (obj instanceof BigFraction) {
        ret = format((BigFraction) obj, toAppendTo, pos);
    } else if (obj instanceof BigInteger) {
        ret = format(new BigFraction((BigInteger) obj), toAppendTo, pos);
    } else if (obj instanceof Number) {
        ret = format(new BigFraction(((Number) obj).doubleValue()),
                     toAppendTo, pos);
    } else {
        throw MathRuntimeException.createIllegalArgumentException(
            "cannot format given object as a fraction number");
    }

    return ret;
}
 
Example #3
Source File: DateTimeFormatter.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
    Objects.requireNonNull(obj, "obj");
    Objects.requireNonNull(toAppendTo, "toAppendTo");
    Objects.requireNonNull(pos, "pos");
    if (obj instanceof TemporalAccessor == false) {
        throw new IllegalArgumentException("Format target must implement TemporalAccessor");
    }
    pos.setBeginIndex(0);
    pos.setEndIndex(0);
    try {
        formatter.formatTo((TemporalAccessor) obj, toAppendTo);
    } catch (RuntimeException ex) {
        throw new IllegalArgumentException(ex.getMessage(), ex);
    }
    return toAppendTo;
}
 
Example #4
Source File: ProperBigFractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats a {@link BigFraction} object to produce a string.  The BigFraction
 * is output in proper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
@Override
public StringBuffer format(final BigFraction fraction,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    BigInteger num = fraction.getNumerator();
    BigInteger den = fraction.getDenominator();
    BigInteger whole = num.divide(den);
    num = num.remainder(den);

    if (!BigInteger.ZERO.equals(whole)) {
        getWholeFormat().format(whole, toAppendTo, pos);
        toAppendTo.append(' ');
        if (num.compareTo(BigInteger.ZERO) < 0) {
            num = num.negate();
        }
    }
    getNumeratorFormat().format(num, toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(den, toAppendTo, pos);

    return toAppendTo;
}
 
Example #5
Source File: HexNumberFormat.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Formats the specified number as a hexadecimal string.  The decimal
 * fraction is ignored.
 *
 * @param number  the number to format.
 * @param toAppendTo  the buffer to append to (ignored here).
 * @param pos  the field position (ignored here).
 *
 * @return The string buffer.
 */
@Override
public StringBuffer format(long number, StringBuffer toAppendTo,
        FieldPosition pos) {
    String l_hex = Long.toHexString(number).toUpperCase();

    int l_pad = this.m_numDigits - l_hex.length();
    l_pad = (0 < l_pad) ? l_pad : 0;

    StringBuffer l_extended = new StringBuffer("0x");
    for (int i = 0; i < l_pad; i++) {
        l_extended.append(0);
    }
    l_extended.append(l_hex);

    return l_extended;
}
 
Example #6
Source File: SimpleDateFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Formats a date or time, which is the standard millis
 * since January 1, 1970, 00:00:00 GMT.
 * <p>Example: using the US locale:
 * "yyyy.MM.dd G 'at' HH:mm:ss zzz" -&gt;&gt; 1996.07.10 AD at 15:08:56 PDT
 * @param cal the calendar whose date-time value is to be formatted into a date-time string
 * @param toAppendTo where the new date-time text is to be appended
 * @param pos the formatting position. On input: an alignment field,
 * if desired. On output: the offsets of the alignment field.
 * @return the formatted date-time string.
 * @see DateFormat
 */
@Override
public StringBuffer format(Calendar cal, StringBuffer toAppendTo,
                           FieldPosition pos) {
    TimeZone backupTZ = null;
    if (cal != calendar && !cal.getType().equals(calendar.getType())) {
        // Different calendar type
        // We use the time and time zone from the input calendar, but
        // do not use the input calendar for field calculation.
        calendar.setTimeInMillis(cal.getTimeInMillis());
        backupTZ = calendar.getTimeZone();
        calendar.setTimeZone(cal.getTimeZone());
        cal = calendar;
    }
    StringBuffer result = format(cal, getContext(DisplayContext.Type.CAPITALIZATION), toAppendTo, pos, null);
    if (backupTZ != null) {
        // Restore the original time zone
        calendar.setTimeZone(backupTZ);
    }
    return result;
}
 
Example #7
Source File: ProperBigFractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats a {@link BigFraction} object to produce a string.  The BigFraction
 * is output in proper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
@Override
public StringBuffer format(final BigFraction fraction,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    BigInteger num = fraction.getNumerator();
    BigInteger den = fraction.getDenominator();
    BigInteger whole = num.divide(den);
    num = num.remainder(den);

    if (!BigInteger.ZERO.equals(whole)) {
        getWholeFormat().format(whole, toAppendTo, pos);
        toAppendTo.append(' ');
        if (num.compareTo(BigInteger.ZERO) < 0) {
            num = num.negate();
        }
    }
    getNumeratorFormat().format(num, toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(den, toAppendTo, pos);

    return toAppendTo;
}
 
Example #8
Source File: AngleFormatTest.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Tests the field position while formatting an angle.
 */
@Test
public void testFieldPosition() {
    final Latitude latitude = new Latitude(FormattedCharacterIteratorTest.LATITUDE_VALUE);
    final AngleFormat f = new AngleFormat("DD°MM′SS.s″", Locale.CANADA);
    final StringBuffer buffer = new StringBuffer();
    for (int i=AngleFormat.DEGREES_FIELD; i<=AngleFormat.HEMISPHERE_FIELD; i++) {
        final AngleFormat.Field field;
        final int start, limit;
        switch (i) {
            case AngleFormat.DEGREES_FIELD:    field = AngleFormat.Field.DEGREES;    start= 0; limit= 3;  break;
            case AngleFormat.MINUTES_FIELD:    field = AngleFormat.Field.MINUTES;    start= 3; limit= 6;  break;
            case AngleFormat.SECONDS_FIELD:    field = AngleFormat.Field.SECONDS;    start= 6; limit=11; break;
            case AngleFormat.HEMISPHERE_FIELD: field = AngleFormat.Field.HEMISPHERE; start=11; limit=12; break;
            default: continue; // Skip the fraction field.
        }
        final FieldPosition pos = new FieldPosition(field);
        assertEquals(FormattedCharacterIteratorTest.LATITUDE_STRING, f.format(latitude, buffer, pos).toString());
        assertSame  ("getFieldAttribute", field, pos.getFieldAttribute());
        assertEquals("getBeginIndex",     start, pos.getBeginIndex());
        assertEquals("getEndIndex",       limit, pos.getEndIndex());
        buffer.setLength(0);
    }
}
 
Example #9
Source File: ExcelGeneralNumberFormat.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public StringBuffer format(Object number, StringBuffer toAppendTo, FieldPosition pos) {
    final double value;
    if (number instanceof Number) {
        value = ((Number)number).doubleValue();
        if (Double.isInfinite(value) || Double.isNaN(value)) {
            return integerFormat.format(number, toAppendTo, pos);
        }
    } else {
        // testBug54786 gets here with a date, so retain previous behaviour
        return integerFormat.format(number, toAppendTo, pos);
    }

    final double abs = Math.abs(value);
    if (abs >= 1E11 || (abs <= 1E-10 && abs > 0)) {
        return scientificFormat.format(number, toAppendTo, pos);
    } else if (Math.floor(value) == value || abs >= 1E10) {
        // integer, or integer portion uses all 11 allowed digits
        return integerFormat.format(number, toAppendTo, pos);
    }
    // Non-integers of non-scientific magnitude are formatted as "up to 11
    // numeric characters, with the decimal point counting as a numeric
    // character". We know there is a decimal point, so limit to 10 digits.
    // https://support.microsoft.com/en-us/kb/65903
    final double rounded = new BigDecimal(value).round(TO_10_SF).doubleValue();
    return decimalFormat.format(rounded, toAppendTo, pos);
}
 
Example #10
Source File: HexNumberFormat.java    From buffer_bci with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Formats the specified number as a hexadecimal string.  The decimal
 * fraction is ignored.
 *
 * @param number  the number to format.
 * @param toAppendTo  the buffer to append to (ignored here).
 * @param pos  the field position (ignored here).
 *
 * @return The string buffer.
 */
@Override
public StringBuffer format(long number, StringBuffer toAppendTo,
        FieldPosition pos) {
    String l_hex = Long.toHexString(number).toUpperCase();

    int l_pad = this.m_numDigits - l_hex.length();
    l_pad = (0 < l_pad) ? l_pad : 0;

    StringBuffer l_extended = new StringBuffer("0x");
    for (int i = 0; i < l_pad; i++) {
        l_extended.append(0);
    }
    l_extended.append(l_hex);

    return l_extended;
}
 
Example #11
Source File: ProperFractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats a {@link Fraction} object to produce a string.  The fraction
 * is output in proper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
@Override
public StringBuffer format(Fraction fraction, StringBuffer toAppendTo,
        FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    int num = fraction.getNumerator();
    int den = fraction.getDenominator();
    int whole = num / den;
    num = num % den;

    if (whole != 0) {
        getWholeFormat().format(whole, toAppendTo, pos);
        toAppendTo.append(' ');
        num = Math.abs(num);
    }
    getNumeratorFormat().format(num, toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(den, toAppendTo,
        pos);

    return toAppendTo;
}
 
Example #12
Source File: FractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats an object and appends the result to a StringBuffer. <code>obj</code> must be either a 
 * {@link Fraction} object or a {@link Number} object.  Any other type of
 * object will result in an {@link IllegalArgumentException} being thrown.
 *
 * @param obj the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
 * @throws IllegalArgumentException is <code>obj</code> is not a valid type.
 */
@Override
public StringBuffer format(final Object obj,
                           final StringBuffer toAppendTo, final FieldPosition pos) {
    StringBuffer ret = null;
    
    if (obj instanceof Fraction) {
        ret = format((Fraction) obj, toAppendTo, pos);
    } else if (obj instanceof Number) {
        try {
            ret = format(new Fraction(((Number) obj).doubleValue()),
                         toAppendTo, pos);
        } catch (ConvergenceException ex) {
            throw MathRuntimeException.createIllegalArgumentException(
                "cannot convert given object to a fraction number: {0}",
                ex.getLocalizedMessage());
        }
    } else { 
        throw MathRuntimeException.createIllegalArgumentException(
            "cannot format given object as a fraction number");
    }
    
    return ret;
}
 
Example #13
Source File: DateTimeDateFormat.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
   Appends to <code>sbuf</code> the date in the format "dd MMM yyyy
   HH:mm:ss,SSS" for example, "06 Nov 1994 08:49:37,459".

   @param sbuf the string buffer to write to
*/
public
StringBuffer format(Date date, StringBuffer sbuf,
      FieldPosition fieldPosition) {

  calendar.setTime(date);

  int day = calendar.get(Calendar.DAY_OF_MONTH);
  if(day < 10)
    sbuf.append('0');
  sbuf.append(day);
  sbuf.append(' ');
  sbuf.append(shortMonths[calendar.get(Calendar.MONTH)]);
  sbuf.append(' ');

  int year =  calendar.get(Calendar.YEAR);
  sbuf.append(year);
  sbuf.append(' ');

  return super.format(date, sbuf, fieldPosition);
}
 
Example #14
Source File: DateTimeFormatter.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
    Objects.requireNonNull(obj, "obj");
    Objects.requireNonNull(toAppendTo, "toAppendTo");
    Objects.requireNonNull(pos, "pos");
    if (obj instanceof TemporalAccessor == false) {
        throw new IllegalArgumentException("Format target must implement TemporalAccessor");
    }
    pos.setBeginIndex(0);
    pos.setEndIndex(0);
    try {
        formatter.formatTo((TemporalAccessor) obj, toAppendTo);
    } catch (RuntimeException ex) {
        throw new IllegalArgumentException(ex.getMessage(), ex);
    }
    return toAppendTo;
}
 
Example #15
Source File: ComplexFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats a object to produce a string.  <code>obj</code> must be either a
 * {@link Complex} object or a {@link Number} object.  Any other type of
 * object will result in an {@link IllegalArgumentException} being thrown.
 *
 * @param obj the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
 * @throws IllegalArgumentException is <code>obj</code> is not a valid type.
 */
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo,
        FieldPosition pos) {

    StringBuffer ret = null;

    if (obj instanceof Complex) {
        ret = format( (Complex)obj, toAppendTo, pos);
    } else if (obj instanceof Number) {
        ret = format( new Complex(((Number)obj).doubleValue(), 0.0),
            toAppendTo, pos);
    } else {
        throw MathRuntimeException.createIllegalArgumentException(
              "cannot format a {0} instance as a complex number",
              obj.getClass().getName());
    }

    return ret;
}
 
Example #16
Source File: ProperFractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats a {@link Fraction} object to produce a string.  The fraction
 * is output in proper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
@Override
public StringBuffer format(Fraction fraction, StringBuffer toAppendTo,
        FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    int num = fraction.getNumerator();
    int den = fraction.getDenominator();
    int whole = num / den;
    num = num % den;

    if (whole != 0) {
        getWholeFormat().format(whole, toAppendTo, pos);
        toAppendTo.append(' ');
        num = Math.abs(num);
    }
    getNumeratorFormat().format(num, toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(den, toAppendTo,
        pos);

    return toAppendTo;
}
 
Example #17
Source File: MeasureFormat.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private StringBuilder formatMeasure(
        Measure measure,
        ImmutableNumberFormat nf,
        StringBuilder appendTo,
        FieldPosition fieldPosition) {
    Number n = measure.getNumber();
    MeasureUnit unit = measure.getUnit();
    if (unit instanceof Currency) {
        return appendTo.append(
                currencyFormat.format(
                        new CurrencyAmount(n, (Currency) unit),
                        new StringBuffer(),
                        fieldPosition));

    }
    StringBuffer formattedNumber = new StringBuffer();
    StandardPlural pluralForm = QuantityFormatter.selectPlural(
            n, nf.nf, rules, formattedNumber, fieldPosition);
    String formatter = getPluralFormatter(unit, formatWidth, pluralForm.ordinal());
    return QuantityFormatter.format(formatter, formattedNumber, appendTo, fieldPosition);
}
 
Example #18
Source File: ProperBigFractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats a {@link BigFraction} object to produce a string.  The BigFraction
 * is output in proper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
@Override
public StringBuffer format(final BigFraction fraction,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    BigInteger num = fraction.getNumerator();
    BigInteger den = fraction.getDenominator();
    BigInteger whole = num.divide(den);
    num = num.remainder(den);

    if (!BigInteger.ZERO.equals(whole)) {
        getWholeFormat().format(whole, toAppendTo, pos);
        toAppendTo.append(' ');
        if (num.compareTo(BigInteger.ZERO) < 0) {
            num = num.negate();
        }
    }
    getNumeratorFormat().format(num, toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(den, toAppendTo, pos);

    return toAppendTo;
}
 
Example #19
Source File: ProperBigFractionFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats a {@link BigFraction} object to produce a string.  The BigFraction
 * is output in proper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
@Override
public StringBuffer format(final BigFraction fraction,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    BigInteger num = fraction.getNumerator();
    BigInteger den = fraction.getDenominator();
    BigInteger whole = num.divide(den);
    num = num.remainder(den);

    if (!BigInteger.ZERO.equals(whole)) {
        getWholeFormat().format(whole, toAppendTo, pos);
        toAppendTo.append(' ');
        if (num.compareTo(BigInteger.ZERO) < 0) {
            num = num.negate();
        }
    }
    getNumeratorFormat().format(num, toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(den, toAppendTo, pos);

    return toAppendTo;
}
 
Example #20
Source File: RealVectorFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats a {@link RealVector} object to produce a string.
 * @param vector the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
public StringBuffer format(RealVector vector, StringBuffer toAppendTo,
                           FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    // format prefix
    toAppendTo.append(prefix);

    // format components
    for (int i = 0; i < vector.getDimension(); ++i) {
        if (i > 0) {
            toAppendTo.append(separator);
        }
        formatDouble(vector.getEntry(i), format, toAppendTo, pos);
    }

    // format suffix
    toAppendTo.append(suffix);

    return toAppendTo;

}
 
Example #21
Source File: ComplexFormat.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Formats a object to produce a string.  <code>obj</code> must be either a
 * {@link Complex} object or a {@link Number} object.  Any other type of
 * object will result in an {@link IllegalArgumentException} being thrown.
 *
 * @param obj the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
 * @throws IllegalArgumentException is <code>obj</code> is not a valid type.
 */
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo,
        FieldPosition pos) {

    StringBuffer ret = null;

    if (obj instanceof Complex) {
        ret = format( (Complex)obj, toAppendTo, pos);
    } else if (obj instanceof Number) {
        ret = format( new Complex(((Number)obj).doubleValue(), 0.0),
            toAppendTo, pos);
    } else {
        throw MathRuntimeException.createIllegalArgumentException(
              "cannot format a {0} instance as a complex number",
              obj.getClass().getName());
    }

    return ret;
}
 
Example #22
Source File: FractionFormat.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Formats a {@link Fraction} object to produce a string.  The fraction is
 * output in improper format.
 *
 * @param fraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
public StringBuffer format(final Fraction fraction,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    getNumeratorFormat().format(fraction.getNumerator(), toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(fraction.getDenominator(), toAppendTo,
        pos);

    return toAppendTo;
}
 
Example #23
Source File: ComplexFormat.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Format the absolute value of the imaginary part.
 *
 * @param absIm Absolute value of the imaginary part of a complex number.
 * @param toAppendTo where the text is to be appended.
 * @param pos On input: an alignment field, if desired. On output: the
 * offsets of the alignment field.
 * @return the value passed in as toAppendTo.
 */
private StringBuffer formatImaginary(double absIm,
                                     StringBuffer toAppendTo,
                                     FieldPosition pos) {
    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    CompositeFormat.formatDouble(absIm, getImaginaryFormat(), toAppendTo, pos);
    if (toAppendTo.toString().equals("1")) {
        // Remove the character "1" if it is the only one.
        toAppendTo.setLength(0);
    }

    return toAppendTo;
}
 
Example #24
Source File: 1_FastDateFormat.java    From SimFix with GNU General Public License v2.0 5 votes vote down vote up
/**
 * <p>Formats a <code>Date</code>, <code>Calendar</code> or
 * <code>Long</code> (milliseconds) object.</p>
 * 
 * @param obj  the object to format
 * @param toAppendTo  the buffer to append to
 * @param pos  the position - ignored
 * @return the buffer passed in
 */
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
    if (obj instanceof Date) {
        return format((Date) obj, toAppendTo);
    } else if (obj instanceof Calendar) {
        return format((Calendar) obj, toAppendTo);
    } else if (obj instanceof Long) {
        return format(((Long) obj).longValue(), toAppendTo);
    } else {
        throw new IllegalArgumentException("Unknown class: " +
            (obj == null ? "<null>" : obj.getClass().getName()));
    }
}
 
Example #25
Source File: DisplayableSupport.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
@Override
public StringBuffer format(Object o, StringBuffer b, FieldPosition p) {
    if (ValuesChecker.isNAValue(o)) return b.append(VALUE_NA);
    
    return o instanceof Number ? b.append(NUMBER_FORMAT.format(((Number)o).longValue())).append(dataSuffix) :
           o == null ? b : b.append("<unknown>");
}
 
Example #26
Source File: DataFormatter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
    if (emulateCSV) {
        return toAppendTo.append(result.text);
    } else {
        return toAppendTo.append(result.text.trim());
    }
}
 
Example #27
Source File: MeasureFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * Able to format Collection&lt;? extends Measure&gt;, Measure[], and Measure
 * by delegating to formatMeasures.
 * If the pos argument identifies a NumberFormat field,
 * then its indices are set to the beginning and end of the first such field
 * encountered. MeasureFormat itself does not supply any fields.
 * 
 * Calling a
 * <code>formatMeasures</code> method is preferred over calling
 * this method as they give better performance.
 * 
 * @param obj must be a Collection&lt;? extends Measure&gt;, Measure[], or Measure object.
 * @param toAppendTo Formatted string appended here.
 * @param pos Identifies a field in the formatted text.
 * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
 * 
 * @stable ICU53
 */
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
    int prevLength = toAppendTo.length();
    FieldPosition fpos =
            new FieldPosition(pos.getFieldAttribute(), pos.getField());
    if (obj instanceof Collection) {
        Collection<?> coll = (Collection<?>) obj;
        Measure[] measures = new Measure[coll.size()];
        int idx = 0;
        for (Object o : coll) {
            if (!(o instanceof Measure)) {
                throw new IllegalArgumentException(obj.toString());
            }
            measures[idx++] = (Measure) o;
        }
        toAppendTo.append(formatMeasures(new StringBuilder(), fpos, measures));
    } else if (obj instanceof Measure[]) {
        toAppendTo.append(formatMeasures(new StringBuilder(), fpos, (Measure[]) obj));
    } else if (obj instanceof Measure){
        toAppendTo.append(formatMeasure((Measure) obj, numberFormat, new StringBuilder(), fpos));
    } else {
        throw new IllegalArgumentException(obj.toString());            
    }
    if (fpos.getBeginIndex() != 0 || fpos.getEndIndex() != 0) {
        pos.setBeginIndex(fpos.getBeginIndex() + prevLength);
        pos.setEndIndex(fpos.getEndIndex() + prevLength);
    }
    return toAppendTo;
}
 
Example #28
Source File: BigFractionFormat.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Formats a {@link BigFraction} object to produce a string.  The BigFraction is
 * output in improper format.
 *
 * @param BigFraction the object to format.
 * @param toAppendTo where the text is to be appended
 * @param pos On input: an alignment field, if desired. On output: the
 *            offsets of the alignment field
 * @return the value passed in as toAppendTo.
 */
public StringBuffer format(final BigFraction BigFraction,
                           final StringBuffer toAppendTo, final FieldPosition pos) {

    pos.setBeginIndex(0);
    pos.setEndIndex(0);

    getNumeratorFormat().format(BigFraction.getNumerator(), toAppendTo, pos);
    toAppendTo.append(" / ");
    getDenominatorFormat().format(BigFraction.getDenominator(), toAppendTo, pos);

    return toAppendTo;
}
 
Example #29
Source File: LocalizedResource.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public String getTimeAsString(Date t){
	if (!enableLocalized){
		return t.toString();
	}
	return formatTime.format(t,	new StringBuffer(),
								  new java.text.FieldPosition(0)).toString();
}
 
Example #30
Source File: ISO8601DateFormatWithMilliSeconds.java    From botanic-ng with Apache License 2.0 5 votes vote down vote up
@Override
public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition)
{
	final String value = ISO8601Utils.format(date, true);
	toAppendTo.append(value);
	return toAppendTo;
}