Example usage for java.text NumberFormat formatToCharacterIterator

List of usage examples for java.text NumberFormat formatToCharacterIterator

Introduction

In this page you can find the example usage for java.text NumberFormat formatToCharacterIterator.

Prototype

public AttributedCharacterIterator formatToCharacterIterator(Object obj) 

Source Link

Document

Formats an Object producing an AttributedCharacterIterator.

Usage

From source file:Main.java

private static Format createFormat() {
    NumberFormat format = NumberFormat.getInstance();
    format.setParseIntegerOnly(true);//from   w w  w  .  j a v  a  2 s.co m
    return new Format() {
        @Override
        public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
            return format.format(obj, toAppendTo, pos);
        }

        @Override
        public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
            return format.formatToCharacterIterator(obj);
        }

        @Override
        public Object parseObject(String source, ParsePosition pos) {
            int initialIndex = pos.getIndex();
            Object result = format.parseObject(source, pos);
            if (result != null && pos.getIndex() > initialIndex + 1) {
                int errorIndex = initialIndex + 1;
                pos.setIndex(initialIndex);
                pos.setErrorIndex(errorIndex);
                return null;
            }
            return result;
        }
    };
}