@Test(dataProvider="calendars") public void test_badPlusAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); TemporalAmount adjuster = new FixedAdjuster(czdt2); if (chrono != chrono2) { try { czdt.plus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + czdt + ", supplied: " + czdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.plus(adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date time"); } } }
@Test(dataProvider="calendars") public void test_badPlusAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDate date = chrono.date(refDate); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDate date2 = chrono2.date(refDate); TemporalAmount adjuster = new FixedAdjuster(date2); if (chrono != chrono2) { try { date.plus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException"); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDate result = date.plus(adjuster); assertEquals(result, date2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badMinusAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoZonedDateTime<?> czdt = chrono.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoZonedDateTime<?> czdt2 = chrono2.date(refDate).atTime(LocalTime.NOON).atZone(ZoneOffset.UTC); TemporalAmount adjuster = new FixedAdjuster(czdt2); if (chrono != chrono2) { try { czdt.minus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + czdt + ", supplied: " + czdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoZonedDateTime<?> result = czdt.minus(adjuster); assertEquals(result, czdt2, "WithAdjuster failed to replace date"); } } }
@Test(dataProvider="calendars") public void test_badPlusAdjusterChrono(Chronology chrono) { LocalDate refDate = LocalDate.of(2013, 1, 1); ChronoLocalDateTime<?> cdt = chrono.date(refDate).atTime(LocalTime.NOON); for (Chronology[] clist : data_of_calendars()) { Chronology chrono2 = clist[0]; ChronoLocalDateTime<?> cdt2 = chrono2.date(refDate).atTime(LocalTime.NOON); TemporalAmount adjuster = new FixedAdjuster(cdt2); if (chrono != chrono2) { try { cdt.plus(adjuster); Assert.fail("WithAdjuster should have thrown a ClassCastException, " + "required: " + cdt + ", supplied: " + cdt2); } catch (ClassCastException cce) { // Expected exception; not an error } } else { // Same chronology, ChronoLocalDateTime<?> result = cdt.plus(adjuster); assertEquals(result, cdt2, "WithAdjuster failed to replace date time"); } } }
@Test(dataProvider="plus_TemporalAmount") public void test_plus_TemporalAmount(YearMonth base, TemporalAmount temporalAmount, YearMonth expectedYearMonth, Class<?> expectedEx) { if (expectedEx == null) { assertEquals(base.plus(temporalAmount), expectedYearMonth); } else { try { YearMonth result = base.plus(temporalAmount); fail(); } catch (Exception ex) { assertTrue(expectedEx.isInstance(ex)); } } }
/** * Obtains an instance of {@code ChronoPeriodImpl} from a temporal amount. * * @param amount the temporal amount to convert, not null * @return the period, not null */ private ChronoPeriodImpl validateAmount(TemporalAmount amount) { Objects.requireNonNull(amount, "amount"); if (amount instanceof ChronoPeriodImpl == false) { throw new DateTimeException("Unable to obtain ChronoPeriod from TemporalAmount: " + amount.getClass()); } ChronoPeriodImpl period = (ChronoPeriodImpl) amount; if (chrono.equals(period.getChronology()) == false) { throw new ClassCastException("Chronology mismatch, expected: " + chrono.getId() + ", actual: " + period.getChronology().getId()); } return period; }
/** * Obtains an instance of {@code Period} from a temporal amount. * <p> * This obtains a period based on the specified amount. * A {@code TemporalAmount} represents an amount of time, which may be * date-based or time-based, which this factory extracts to a {@code Period}. * <p> * The conversion loops around the set of units from the amount and uses * the {@link ChronoUnit#YEARS YEARS}, {@link ChronoUnit#MONTHS MONTHS} * and {@link ChronoUnit#DAYS DAYS} units to create a period. * If any other units are found then an exception is thrown. * <p> * If the amount is a {@code ChronoPeriod} then it must use the ISO chronology. * * @param amount the temporal amount to convert, not null * @return the equivalent period, not null * @throws DateTimeException if unable to convert to a {@code Period} * @throws ArithmeticException if the amount of years, months or days exceeds an int */ public static Period from(TemporalAmount amount) { if (amount instanceof Period) { return (Period) amount; } if (amount instanceof ChronoPeriod) { if (IsoChronology.INSTANCE.equals(((ChronoPeriod) amount).getChronology()) == false) { throw new DateTimeException("Period requires ISO chronology: " + amount); } } Objects.requireNonNull(amount, "amount"); int years = 0; int months = 0; int days = 0; for (TemporalUnit unit : amount.getUnits()) { long unitAmount = amount.get(unit); if (unit == ChronoUnit.YEARS) { years = Math.toIntExact(unitAmount); } else if (unit == ChronoUnit.MONTHS) { months = Math.toIntExact(unitAmount); } else if (unit == ChronoUnit.DAYS) { days = Math.toIntExact(unitAmount); } else { throw new DateTimeException("Unit must be Years, Months or Days, but was " + unit); } } return create(years, months, days); }
@Test(dataProvider="plusTemporalAmount") public void test_plusTemporalAmount(TemporalUnit unit, TemporalAmount amount, int seconds, int nanos) { Instant inst = Instant.ofEpochMilli(1000); Instant actual = inst.plus(amount); Instant expected = Instant.ofEpochSecond(seconds, nanos); assertEquals(actual, expected, "plus(TemporalAmount) failed"); }
@Override public ChronoPeriod plus(TemporalAmount amountToAdd) { ChronoPeriodImpl amount = validateAmount(amountToAdd); return new ChronoPeriodImpl( chrono, Math.addExact(years, amount.years), Math.addExact(months, amount.months), Math.addExact(days, amount.days)); }
@Override public ChronoPeriod minus(TemporalAmount amountToSubtract) { ChronoPeriodImpl amount = validateAmount(amountToSubtract); return new ChronoPeriodImpl( chrono, Math.subtractExact(years, amount.years), Math.subtractExact(months, amount.months), Math.subtractExact(days, amount.days)); }
public TemporalAmount getTimeToLive() { return Duration.ofMinutes(5); }
@Test public void test_plus_TemporalAmount_wrap() { TemporalAmount p = MockSimplePeriod.of(1, HOURS); LocalTime t = LocalTime.of(23, 30).plus(p); assertEquals(t, LocalTime.of(0, 30)); }
public static long getUnitOrDefault(TemporalAmount period, TemporalUnit unit, long def) { return period.getUnits().contains(unit) ? period.get(unit) : def; }
@Test(expectedExceptions=NullPointerException.class) public void test_minus_TemporalAmount_null() { TEST_DATE_TIME.minus((TemporalAmount) null); }
@Test public void test_minus_TemporalAmount_zero() { TemporalAmount period = Period.ZERO; LocalTime t = TEST_12_30_40_987654321.minus(period); assertEquals(t, TEST_12_30_40_987654321); }
@Test(expectedExceptions=NullPointerException.class) public void test_minus_TemporalAmount_null() { TEST_12_30_40_987654321.minus((TemporalAmount) null); }
@Test(dataProvider="minusValid") public void test_minusValid(int year, TemporalAmount amount, int expected) { assertEquals(Year.of(year).minus(amount), Year.of(expected)); }
@Test(expectedExceptions=DateTimeException.class) public void test_plus_TemporalAmount_dateNotAllowed() { TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.MONTHS); TEST_12_30_40_987654321.plus(period); }
@Test public void test_minus_TemporalAmount_positiveHours() { TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.HOURS); LocalTime t = TEST_12_30_40_987654321.minus(period); assertEquals(t, LocalTime.of(5, 30, 40, 987654321)); }
@Test public void test_minus_TemporalAmount_wrap() { TemporalAmount p = MockSimplePeriod.of(1, HOURS); LocalTime t = LocalTime.of(0, 30).minus(p); assertEquals(t, LocalTime.of(23, 30)); }
@Test(expectedExceptions=NullPointerException.class) public void test_plus_TemporalAmount_null() { TEST_12_30_40_987654321.plus((TemporalAmount) null); }
@Override @SuppressWarnings("unchecked") public D minus(TemporalAmount amount) { return (D) ChronoLocalDate.super.minus(amount); }
@Test public void test_plus_TemporalAmount_negativeMinutes() { TemporalAmount period = MockSimplePeriod.of(-25, ChronoUnit.MINUTES); LocalTime t = TEST_12_30_40_987654321.plus(period); assertEquals(t, LocalTime.of(12, 5, 40, 987654321)); }
@Test(expectedExceptions=NullPointerException.class) public void test_minus_MinusAdjuster_null() { TEST_11_30_59_500_PONE.minus((TemporalAmount) null); }
@Test public void factory_from_TemporalAmount_Period() { TemporalAmount amount = Period.of(1, 2, 3); assertPeriod(Period.from(amount), 1, 2, 3); }
@Test(dataProvider="minusInvalidUnit", expectedExceptions=UnsupportedTemporalTypeException.class) public void test_minusInvalidUnit(TemporalAmount amount) { TEST_2008.minus(amount); }
@Test(expectedExceptions=NullPointerException.class) public void test_minus_TemporalAmount_null() { TEST_2007_07_15_12_30_40_987654321.minus((TemporalAmount) null); }
@Test(expectedExceptions=NullPointerException.class) public void test_plus_PlusAdjuster_null() { TEST_11_30_59_500_PONE.plus((TemporalAmount) null); }
@Test(expectedExceptions=NullPointerException.class) public void test_plus_TemporalAmount_null() { TEST_2007_07_15_12_30_40_987654321.plus((TemporalAmount) null); }
@Test(expectedExceptions=NullPointerException.class) public void test_plus_TemporalAmount_null() { TEST_DATE_TIME.plus((TemporalAmount) null); }
@Test public void test_plus_TemporalAmount_positiveHours() { TemporalAmount period = MockSimplePeriod.of(7, ChronoUnit.HOURS); LocalTime t = TEST_12_30_40_987654321.plus(period); assertEquals(t, LocalTime.of(19, 30, 40, 987654321)); }
/** * Returns a copy of this period with the specified period added. * <p> * This operates separately on the years, months and days. * No normalization is performed. * <p> * For example, "1 year, 6 months and 3 days" plus "2 years, 2 months and 2 days" * returns "3 years, 8 months and 5 days". * <p> * The specified amount is typically an instance of {@code Period}. * Other types are interpreted using {@link Period#from(TemporalAmount)}. * <p> * This instance is immutable and unaffected by this method call. * * @param amountToAdd the period to add, not null * @return a {@code Period} based on this period with the requested period added, not null * @throws DateTimeException if the specified amount has a non-ISO chronology or * contains an invalid unit * @throws ArithmeticException if numeric overflow occurs */ public Period plus(TemporalAmount amountToAdd) { Period isoAmount = Period.from(amountToAdd); return create( Math.addExact(years, isoAmount.years), Math.addExact(months, isoAmount.months), Math.addExact(days, isoAmount.days)); }