Example usage for org.joda.time MutablePeriod setValue

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

Introduction

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

Prototype

public void setValue(int index, int value) 

Source Link

Document

Sets the value of one of the fields by 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);
            }/*w ww . ja  v  a  2s . com*/

            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;
}