Search in sources :

Example 6 with FieldPosition

use of java.text.FieldPosition in project robovm by robovm.

the class NumberFormatTest method test_small_BigInteger_gets_longValue.

// NumberFormat.format(Object, StringBuffer, FieldPosition) guarantees it calls longValue for
// any BigInteger with a bitLength strictly less than 64.
public void test_small_BigInteger_gets_longValue() throws Exception {
    class MyNumberFormat extends NumberFormat {

        public StringBuffer format(double value, StringBuffer b, FieldPosition f) {
            b.append("double");
            return b;
        }

        public StringBuffer format(long value, StringBuffer b, FieldPosition f) {
            b.append("long");
            return b;
        }

        public Number parse(String string, ParsePosition p) {
            throw new UnsupportedOperationException();
        }
    }
    NumberFormat nf = new MyNumberFormat();
    assertEquals("long", nf.format(BigInteger.valueOf(Long.MAX_VALUE)));
    assertEquals("double", nf.format(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE)));
    assertEquals("long", nf.format(BigInteger.valueOf(Long.MIN_VALUE)));
    assertEquals("double", nf.format(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE)));
}
Also used : FieldPosition(java.text.FieldPosition) ParsePosition(java.text.ParsePosition) NumberFormat(java.text.NumberFormat)

Example 7 with FieldPosition

use of java.text.FieldPosition in project dbeaver by serge-rider.

the class NumberDataFormatter method init.

@Override
public void init(Locale locale, Map<Object, Object> properties) {
    numberFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);
    Object useGrouping = properties.get(NumberFormatSample.PROP_USE_GROUPING);
    if (useGrouping != null) {
        numberFormat.setGroupingUsed(CommonUtils.toBoolean(useGrouping));
    }
    Object maxIntDigits = properties.get(NumberFormatSample.PROP_MAX_INT_DIGITS);
    if (maxIntDigits != null) {
        numberFormat.setMaximumIntegerDigits(CommonUtils.toInt(maxIntDigits));
    }
    Object minIntDigits = properties.get(NumberFormatSample.PROP_MIN_INT_DIGITS);
    if (minIntDigits != null) {
        numberFormat.setMinimumIntegerDigits(CommonUtils.toInt(minIntDigits));
    }
    Object maxFractDigits = properties.get(NumberFormatSample.PROP_MAX_FRACT_DIGITS);
    if (maxFractDigits != null) {
        numberFormat.setMaximumFractionDigits(CommonUtils.toInt(maxFractDigits));
    }
    Object minFractDigits = properties.get(NumberFormatSample.PROP_MIN_FRACT_DIGITS);
    if (minFractDigits != null) {
        numberFormat.setMinimumFractionDigits(CommonUtils.toInt(minFractDigits));
    }
    String roundingMode = CommonUtils.toString(properties.get(NumberFormatSample.PROP_ROUNDING_MODE));
    if (!CommonUtils.isEmpty(roundingMode)) {
        try {
            numberFormat.setRoundingMode(RoundingMode.valueOf(roundingMode));
        } catch (Exception e) {
        // just skip it
        }
    }
    buffer = new StringBuffer();
    position = new FieldPosition(0);
}
Also used : FieldPosition(java.text.FieldPosition) ParseException(java.text.ParseException)

Example 8 with FieldPosition

use of java.text.FieldPosition in project j2objc by google.

the class NumberFormatTest method test_small_BigInteger_gets_longValue.

// NumberFormat.format(Object, StringBuffer, FieldPosition) guarantees it calls longValue for
// any BigInteger with a bitLength strictly less than 64.
public void test_small_BigInteger_gets_longValue() throws Exception {
    class MyNumberFormat extends NumberFormat {

        public StringBuffer format(double value, StringBuffer b, FieldPosition f) {
            b.append("double");
            return b;
        }

        public StringBuffer format(long value, StringBuffer b, FieldPosition f) {
            b.append("long");
            return b;
        }

        public Number parse(String string, ParsePosition p) {
            throw new UnsupportedOperationException();
        }
    }
    NumberFormat nf = new MyNumberFormat();
    assertEquals("long", nf.format(BigInteger.valueOf(Long.MAX_VALUE)));
    assertEquals("double", nf.format(BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE)));
    assertEquals("long", nf.format(BigInteger.valueOf(Long.MIN_VALUE)));
    assertEquals("double", nf.format(BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE)));
}
Also used : FieldPosition(java.text.FieldPosition) ParsePosition(java.text.ParsePosition) NumberFormat(java.text.NumberFormat)

Example 9 with FieldPosition

use of java.text.FieldPosition in project j2objc by google.

the class DecimalFormatTest method testBug15081434.

// Check we don't crash on null inputs.
public void testBug15081434() throws Exception {
    DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(Locale.US);
    try {
        df.parse(null);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        df.applyLocalizedPattern(null);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        df.applyPattern(null);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        df.applyPattern(null);
        fail();
    } catch (NullPointerException expected) {
    }
    try {
        df.format(null, new StringBuffer(), new FieldPosition(0));
        fail();
    // J2ObjC: Java docs actually specify that IAE is thrown for null input, not NPE.
    } catch (IllegalArgumentException expected) {
    }
    try {
        df.parse(null, new ParsePosition(0));
        fail();
    } catch (NullPointerException expected) {
    }
    // This just ignores null.
    df.setDecimalFormatSymbols(null);
    try {
        df.setCurrency(null);
        fail();
    } catch (NullPointerException expected) {
    }
    // These just ignore null.
    df.setNegativePrefix(null);
    df.setNegativeSuffix(null);
    df.setPositivePrefix(null);
    df.setPositiveSuffix(null);
    try {
        df.setRoundingMode(null);
        fail();
    } catch (NullPointerException expected) {
    }
}
Also used : DecimalFormat(java.text.DecimalFormat) FieldPosition(java.text.FieldPosition) ParsePosition(java.text.ParsePosition)

Example 10 with FieldPosition

use of java.text.FieldPosition in project j2objc by google.

the class Support_Format method t_FormatWithField.

protected void t_FormatWithField(int count, Format format, Object object, String text, Format.Field field, int begin, int end) {
    StringBuffer buffer = new StringBuffer();
    FieldPosition pos = new FieldPosition(field);
    format.format(object, buffer, pos);
    if (text == null) {
        assertEquals("Test " + count + ": incorrect formatted text", this.text, buffer.toString());
    } else {
        assertEquals(text, buffer.toString());
    }
    if (begin != pos.getBeginIndex() || end != pos.getEndIndex()) {
        assertEquals(field + " " + begin + ".." + end, pos.getFieldAttribute() + " " + pos.getBeginIndex() + ".." + pos.getEndIndex());
    }
}
Also used : FieldPosition(java.text.FieldPosition)

Aggregations

FieldPosition (java.text.FieldPosition)60 Date (java.util.Date)18 SimpleDateFormat (java.text.SimpleDateFormat)15 IsoDateFormat (alma.acs.util.IsoDateFormat)9 DecimalFormat (java.text.DecimalFormat)9 ILogEntry (com.cosylab.logging.engine.log.ILogEntry)8 ACSLogParser (alma.acs.logging.engine.parser.ACSLogParser)5 MessageFormat (java.text.MessageFormat)5 DateFormat (java.text.DateFormat)4 NumberFormat (java.text.NumberFormat)3 ParsePosition (java.text.ParsePosition)3 Vector (java.util.Vector)3 LogTypeHelper (com.cosylab.logging.engine.log.LogTypeHelper)2 ChoiceFormat (java.text.ChoiceFormat)2 ParseException (java.text.ParseException)2 Calendar (java.util.Calendar)2 GregorianCalendar (java.util.GregorianCalendar)2 List (java.util.List)2 Random (java.util.Random)2 EObject (org.eclipse.emf.ecore.EObject)2