Example usage for org.joda.time MutablePeriod getValue

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

Introduction

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

Prototype

public int getValue(int index) 

Source Link

Document

Gets the value at the specified index.

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  w w .  j a v a  2  s .  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;
}