Java Code Examples for java.time.temporal.ValueRange#of()

The following examples show how to use java.time.temporal.ValueRange#of() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HijrahChronology.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
@Override
public ValueRange range(ChronoField field) {
    checkCalendarInit();
    if (field instanceof ChronoField) {
        ChronoField f = field;
        switch (f) {
            case DAY_OF_MONTH:
                return ValueRange.of(1, 1, getMinimumMonthLength(), getMaximumMonthLength());
            case DAY_OF_YEAR:
                return ValueRange.of(1, getMaximumDayOfYear());
            case ALIGNED_WEEK_OF_MONTH:
                return ValueRange.of(1, 5);
            case YEAR:
            case YEAR_OF_ERA:
                return ValueRange.of(getMinimumYear(), getMaximumYear());
            case ERA:
                return ValueRange.of(1, 1);
            default:
                return field.range();
        }
    }
    return field.range();
}
 
Example 2
Source File: JapaneseDate.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
public ValueRange range(TemporalField field) {
    if (field instanceof ChronoField) {
        if (isSupported(field)) {
            ChronoField f = (ChronoField) field;
            switch (f) {
                case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
                case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
                case YEAR_OF_ERA: {
                    Calendar jcal = Calendar.getInstance(JapaneseChronology.LOCALE);
                    jcal.set(Calendar.ERA, era.getValue() + JapaneseEra.ERA_OFFSET);
                    jcal.set(yearOfEra, isoDate.getMonthValue() - 1, isoDate.getDayOfMonth());
                    return ValueRange.of(1, jcal.getActualMaximum(Calendar.YEAR));
                }
            }
            return getChronology().range(f);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.rangeRefinedBy(this);
}
 
Example 3
Source File: HijrahDate.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
@Override
public ValueRange range(TemporalField field) {
    if (field instanceof ChronoField) {
        if (isSupported(field)) {
            ChronoField f = (ChronoField) field;
            switch (f) {
                case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
                case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
                case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, 5);  // TODO
                // TODO does the limited range of valid years cause years to
                // start/end part way through? that would affect range
            }
            return getChronology().range(f);
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.rangeRefinedBy(this);
}
 
Example 4
Source File: TestDateTimeValueRange.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void test_isValidValue_long_int() {
    ValueRange test = ValueRange.of(1, 28, 31);
    assertEquals(test.isValidValue(0), false);
    assertEquals(test.isValidValue(1), true);
    assertEquals(test.isValidValue(31), true);
    assertEquals(test.isValidValue(32), false);
}
 
Example 5
Source File: TestDateTimeValueRange.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void test_isValidValue_long_long() {
    ValueRange test = ValueRange.of(1, 28, Integer.MAX_VALUE + 1L);
    assertEquals(test.isValidIntValue(0), false);
    assertEquals(test.isValidIntValue(1), false);
    assertEquals(test.isValidIntValue(31), false);
    assertEquals(test.isValidIntValue(32), false);
}
 
Example 6
Source File: TestDateTimeValueRange.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public void test_equals2() {
    ValueRange a = ValueRange.of(1, 2, 3, 4);
    assertEquals(a.equals(ValueRange.of(0, 2, 3, 4)), false);
    assertEquals(a.equals(ValueRange.of(1, 3, 3, 4)), false);
    assertEquals(a.equals(ValueRange.of(1, 2, 4, 4)), false);
    assertEquals(a.equals(ValueRange.of(1, 2, 3, 5)), false);
}
 
Example 7
Source File: TestDateTimeValueRange.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Test
@UseDataProvider("data_valid")
public void test_of_longlonglonglong(long sMin, long lMin, long sMax, long lMax) {
    ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax);
    assertEquals(test.getMinimum(), sMin);
    assertEquals(test.getLargestMinimum(), lMin);
    assertEquals(test.getSmallestMaximum(), sMax);
    assertEquals(test.getMaximum(), lMax);
    assertEquals(test.isFixed(), sMin == lMin && sMax == lMax);
    assertEquals(test.isIntValue(), true);
}
 
Example 8
Source File: TestDateTimeValueRange.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test_of_longlong() {
    ValueRange test = ValueRange.of(1, 12);
    assertEquals(test.getMinimum(), 1);
    assertEquals(test.getLargestMinimum(), 1);
    assertEquals(test.getSmallestMaximum(), 12);
    assertEquals(test.getMaximum(), 12);
    assertEquals(test.isFixed(), true);
    assertEquals(test.isIntValue(), true);
}
 
Example 9
Source File: TestDateTimeValueRange.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider="valid")
public void test_of_checkValidValue(long sMin, long lMin, long sMax, long lMax) {
    ValueRange test = ValueRange.of(sMin, lMin, sMax, lMax);
    assertEquals(test.checkValidIntValue(sMin, null), sMin);
    assertEquals(test.checkValidIntValue(lMin, null), lMin);
    assertEquals(test.checkValidIntValue(sMax, null), sMax);
    assertEquals(test.checkValidIntValue(lMax, null), lMax);
}
 
Example 10
Source File: TestDateTimeValueRange.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = IllegalArgumentException.class)
public void test_of_longlong_minGtMax() {
    ValueRange.of(12, 1);
}
 
Example 11
Source File: TestDateTimeValueRange.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = DateTimeException.class)
public void test_checkValidValueInvalid_long_long() {
    ValueRange test = ValueRange.of(1, 28, Integer.MAX_VALUE + 1L);
    test.checkValidIntValue(Integer.MAX_VALUE + 2L, (ChronoField)null);
}
 
Example 12
Source File: TCKValueRangeSerialization.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public void test_serialization() throws Exception {
    ValueRange range = ValueRange.of(1, 2, 3, 4);
    assertSerializable(range);
}
 
Example 13
Source File: TestDateTimeValueRange.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void test_of_longlonglong_minGtMax() {
    ValueRange.of(12, 1, 2);
}
 
Example 14
Source File: TestDateTimeValueRange.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void test_of_longlonglong_smallestmaxminGtMax() {
    ValueRange.of(1, 31, 28);
}
 
Example 15
Source File: TestDateTimeValueRange.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Test(expected = DateTimeException.class)
public void test_checkValidValueUnsupported_long_long() {
    ValueRange test = ValueRange.of(1, 28, Integer.MAX_VALUE + 1L);
    test.checkValidIntValue(0, (ChronoField)null);
}
 
Example 16
Source File: TestDateTimeValueRange.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = DateTimeException.class)
public void test_checkValidValueUnsupported_long_long() {
    ValueRange test = ValueRange.of(1, 28, Integer.MAX_VALUE + 1L);
    test.checkValidIntValue(0, (ChronoField)null);
}
 
Example 17
Source File: LocalDate.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Gets the range of valid values for the specified field.
 * <p>
 * The range object expresses the minimum and maximum valid values for a field.
 * This date is used to enhance the accuracy of the returned range.
 * If it is not possible to return the range, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return
 * appropriate range instances.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 * passing {@code this} as the argument.
 * Whether the range can be obtained is determined by the field.
 *
 * @param field  the field to query the range for, not null
 * @return the range of valid values for the field, not null
 * @throws DateTimeException if the range for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 */
@Override
public ValueRange range(TemporalField field) {
    if (field instanceof ChronoField) {
        ChronoField f = (ChronoField) field;
        if (f.isDateBased()) {
            switch (f) {
                case DAY_OF_MONTH: return ValueRange.of(1, lengthOfMonth());
                case DAY_OF_YEAR: return ValueRange.of(1, lengthOfYear());
                case ALIGNED_WEEK_OF_MONTH: return ValueRange.of(1, getMonth() == Month.FEBRUARY && isLeapYear() == false ? 4 : 5);
                case YEAR_OF_ERA:
                    return (getYear() <= 0 ? ValueRange.of(1, Year.MAX_VALUE + 1) : ValueRange.of(1, Year.MAX_VALUE));
            }
            return field.range();
        }
        throw new UnsupportedTemporalTypeException("Unsupported field: " + field);
    }
    return field.rangeRefinedBy(this);
}
 
Example 18
Source File: Year.java    From JDKSourceCode1.8 with MIT License 3 votes vote down vote up
/**
 * Gets the range of valid values for the specified field.
 * <p>
 * The range object expresses the minimum and maximum valid values for a field.
 * This year is used to enhance the accuracy of the returned range.
 * If it is not possible to return the range, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return
 * appropriate range instances.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 * passing {@code this} as the argument.
 * Whether the range can be obtained is determined by the field.
 *
 * @param field  the field to query the range for, not null
 * @return the range of valid values for the field, not null
 * @throws DateTimeException if the range for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 */
@Override
public ValueRange range(TemporalField field) {
    if (field == YEAR_OF_ERA) {
        return (year <= 0 ? ValueRange.of(1, MAX_VALUE + 1) : ValueRange.of(1, MAX_VALUE));
    }
    return Temporal.super.range(field);
}
 
Example 19
Source File: Year.java    From jdk1.8-source-analysis with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the range of valid values for the specified field.
 * <p>
 * The range object expresses the minimum and maximum valid values for a field.
 * This year is used to enhance the accuracy of the returned range.
 * If it is not possible to return the range, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return
 * appropriate range instances.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 * passing {@code this} as the argument.
 * Whether the range can be obtained is determined by the field.
 *
 * @param field  the field to query the range for, not null
 * @return the range of valid values for the field, not null
 * @throws DateTimeException if the range for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 */
@Override
public ValueRange range(TemporalField field) {
    if (field == YEAR_OF_ERA) {
        return (year <= 0 ? ValueRange.of(1, MAX_VALUE + 1) : ValueRange.of(1, MAX_VALUE));
    }
    return Temporal.super.range(field);
}
 
Example 20
Source File: Year.java    From openjdk-8-source with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Gets the range of valid values for the specified field.
 * <p>
 * The range object expresses the minimum and maximum valid values for a field.
 * This year is used to enhance the accuracy of the returned range.
 * If it is not possible to return the range, because the field is not supported
 * or for some other reason, an exception is thrown.
 * <p>
 * If the field is a {@link ChronoField} then the query is implemented here.
 * The {@link #isSupported(TemporalField) supported fields} will return
 * appropriate range instances.
 * All other {@code ChronoField} instances will throw an {@code UnsupportedTemporalTypeException}.
 * <p>
 * If the field is not a {@code ChronoField}, then the result of this method
 * is obtained by invoking {@code TemporalField.rangeRefinedBy(TemporalAccessor)}
 * passing {@code this} as the argument.
 * Whether the range can be obtained is determined by the field.
 *
 * @param field  the field to query the range for, not null
 * @return the range of valid values for the field, not null
 * @throws DateTimeException if the range for the field cannot be obtained
 * @throws UnsupportedTemporalTypeException if the field is not supported
 */
@Override
public ValueRange range(TemporalField field) {
    if (field == YEAR_OF_ERA) {
        return (year <= 0 ? ValueRange.of(1, MAX_VALUE + 1) : ValueRange.of(1, MAX_VALUE));
    }
    return Temporal.super.range(field);
}