public SqlIntervalYearMonth(int year, int months) { this.months = toMonths(year, months); }
public static int parseMonths(String value) { if (value.equals(INT_MIN_VALUE)) { return Integer.MIN_VALUE; } int signum = 1; if (value.startsWith("-")) { signum = -1; value = value.substring(1); } Matcher matcher = FORMAT.matcher(value); if (!matcher.matches()) { throw new IllegalArgumentException("Invalid year-month interval: " + value); } int years = parseInt(matcher.group(1)); int months = parseInt(matcher.group(2)); return toMonths(years, months) * signum; } }
@Test(expectedExceptions = IllegalArgumentException.class) public void testOverflow() { int days = (Integer.MAX_VALUE / 12) + 1; toMonths(days, 0); } }
@Test public void testMaxYears() { int years = Integer.MAX_VALUE / 12; assertEquals(toMonths(years, 0), years * 12); }
private static long parsePeriodMonths(String value, PeriodFormatter periodFormatter, IntervalField startField, IntervalField endField) { try { Period period = parsePeriod(periodFormatter, value); return IntervalYearMonth.toMonths( period.getValue(YEAR_FIELD), period.getValue(MONTH_FIELD)); } catch (IllegalArgumentException e) { throw invalidInterval(e, value, startField, endField); } }
@Test public void testFormat() { assertMonths(0, "0-0"); assertMonths(toMonths(0, 0), "0-0"); assertMonths(3, "0-3"); assertMonths(-3, "-0-3"); assertMonths(toMonths(0, 3), "0-3"); assertMonths(toMonths(0, -3), "-0-3"); assertMonths(28, "2-4"); assertMonths(-28, "-2-4"); assertMonths(toMonths(2, 4), "2-4"); assertMonths(toMonths(-2, -4), "-2-4"); assertMonths(Integer.MAX_VALUE, "178956970-7"); assertMonths(Integer.MIN_VALUE + 1, "-178956970-7"); assertMonths(Integer.MIN_VALUE, "-178956970-8"); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testOverflow() { int days = (Integer.MAX_VALUE / 12) + 1; toMonths(days, 0); } }
@Test public void testMaxYears() { int years = Integer.MAX_VALUE / 12; assertEquals(toMonths(years, 0), years * 12); }
@Test public void testFormat() { assertMonths(0, "0-0"); assertMonths(toMonths(0, 0), "0-0"); assertMonths(3, "0-3"); assertMonths(-3, "-0-3"); assertMonths(toMonths(0, 3), "0-3"); assertMonths(toMonths(0, -3), "-0-3"); assertMonths(28, "2-4"); assertMonths(-28, "-2-4"); assertMonths(toMonths(2, 4), "2-4"); assertMonths(toMonths(-2, -4), "-2-4"); assertMonths(Integer.MAX_VALUE, "178956970-7"); assertMonths(Integer.MIN_VALUE + 1, "-178956970-7"); assertMonths(Integer.MIN_VALUE, "-178956970-8"); }