Example usage for org.joda.time MutablePeriod size

List of usage examples for org.joda.time MutablePeriod size

Introduction

In this page you can find the example usage for org.joda.time MutablePeriod size.

Prototype

public int size() 

Source Link

Document

Gets the number of fields that this period supports.

Usage

From source file:org.whole.lang.xsd.parsers.SchemaDataTypeParsers.java

License:Open Source License

public static IDataTypeParser duration() {
    if (durationDataTypeParser == null) {
        durationDataTypeParser = new FailureDataTypeParser() {
            public Object parseObject(EntityDescriptor<?> ed, String value) {
                if (value.startsWith("-")) {
                    MutablePeriod period = durationFormatter().parseMutablePeriod(value.substring(1));
                    for (int i = 0, size = period.size(); i < size; i++)
                        period.setValue(i, -period.getValue(i));
                    return period;
                } else
                    return durationFormatter().parseMutablePeriod(value);
            }/*from  w  ww . j a v  a2s .co  m*/

            public String unparseObject(EntityDescriptor<?> ed, Object value) {
                if (value instanceof MutablePeriod) {
                    MutablePeriod period = (MutablePeriod) value;
                    int i = 0;
                    boolean isNegative = false;
                    for (int size = period.size(); i < size; i++) {
                        int n = period.getValue(i);
                        if (n != 0) {
                            isNegative = n < 0;
                            break;
                        }
                    }

                    if (isNegative) {
                        for (int size = period.size(); i < size; i++) {
                            int n = period.getValue(i);
                            if (n > 0)
                                throw new IllegalArgumentException("bad duration format");
                            period.setValue(i, -n);
                        }
                    }
                    return (isNegative ? "-" : "") + durationFormatter().print(period);
                } else
                    return String.valueOf(value);
            }
        };
    }
    return durationDataTypeParser;
}