Search in sources :

Example 6 with IllegalFieldValueException

use of org.joda.time.IllegalFieldValueException in project joda-time by JodaOrg.

the class BaseChronology method validate.

//-----------------------------------------------------------------------
/**
     * Validates whether the fields stored in a partial instant are valid.
     * <p>
     * This implementation uses {@link DateTimeField#getMinimumValue(ReadablePartial, int[])}
     * and {@link DateTimeField#getMaximumValue(ReadablePartial, int[])}.
     *
     * @param partial  the partial instant to validate
     * @param values  the values to validate, not null unless the partial is empty
     * @throws IllegalArgumentException if the instant is invalid
     */
public void validate(ReadablePartial partial, int[] values) {
    // check values in standard range, catching really stupid cases like -1
    // this means that the second check will not hit trouble
    int size = partial.size();
    for (int i = 0; i < size; i++) {
        int value = values[i];
        DateTimeField field = partial.getField(i);
        if (value < field.getMinimumValue()) {
            throw new IllegalFieldValueException(field.getType(), Integer.valueOf(value), Integer.valueOf(field.getMinimumValue()), null);
        }
        if (value > field.getMaximumValue()) {
            throw new IllegalFieldValueException(field.getType(), Integer.valueOf(value), null, Integer.valueOf(field.getMaximumValue()));
        }
    }
    // check values in specific range, catching really odd cases like 30th Feb
    for (int i = 0; i < size; i++) {
        int value = values[i];
        DateTimeField field = partial.getField(i);
        if (value < field.getMinimumValue(partial, values)) {
            throw new IllegalFieldValueException(field.getType(), Integer.valueOf(value), Integer.valueOf(field.getMinimumValue(partial, values)), null);
        }
        if (value > field.getMaximumValue(partial, values)) {
            throw new IllegalFieldValueException(field.getType(), Integer.valueOf(value), null, Integer.valueOf(field.getMaximumValue(partial, values)));
        }
    }
}
Also used : IllegalFieldValueException(org.joda.time.IllegalFieldValueException) DateTimeField(org.joda.time.DateTimeField) UnsupportedDateTimeField(org.joda.time.field.UnsupportedDateTimeField)

Aggregations

IllegalFieldValueException (org.joda.time.IllegalFieldValueException)6 DateTime (org.joda.time.DateTime)2 ArrayList (java.util.ArrayList)1 Chronology (org.joda.time.Chronology)1 DateTimeField (org.joda.time.DateTimeField)1 DurationField (org.joda.time.DurationField)1 IllegalInstantException (org.joda.time.IllegalInstantException)1 UnsupportedDateTimeField (org.joda.time.field.UnsupportedDateTimeField)1 Frame (water.fvec.Frame)1 Vec (water.fvec.Vec)1 Val (water.rapids.Val)1 ValFrame (water.rapids.vals.ValFrame)1