@Override public AbstractDate plus(long amountToAdd, TemporalUnit unit) { if (unit instanceof ChronoUnit) { ChronoUnit f = (ChronoUnit) unit; switch (f) { case DAYS: return plusDays(amountToAdd); case WEEKS: return plusWeeks(amountToAdd); case MONTHS: return plusMonths(amountToAdd); case YEARS: return plusYears(amountToAdd); case DECADES: return plusYears(Math.multiplyExact(amountToAdd, 10)); case CENTURIES: return plusYears(Math.multiplyExact(amountToAdd, 100)); case MILLENNIA: return plusYears(Math.multiplyExact(amountToAdd, 1000)); case ERAS: return with(ERA, Math.addExact(getLong(ERA), amountToAdd)); default: break; } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return unit.addTo(this, amountToAdd); }
// ------------------------------------------------------------------------- long until(AbstractDate end, TemporalUnit unit) { if (unit instanceof ChronoUnit) { switch ((ChronoUnit) unit) { case DAYS: return daysUntil(end); case WEEKS: return weeksUntil(end); case MONTHS: return monthsUntil(end); case YEARS: return monthsUntil(end) / lengthOfYearInMonths(); case DECADES: return monthsUntil(end) / (lengthOfYearInMonths() * 10); case CENTURIES: return monthsUntil(end) / (lengthOfYearInMonths() * 100); case MILLENNIA: return monthsUntil(end) / (lengthOfYearInMonths() * 1000); case ERAS: return end.getLong(ERA) - getLong(ERA); default: break; } throw new UnsupportedTemporalTypeException("Unsupported unit: " + unit); } return unit.between(this, end); }
@Override public boolean equals(Object o) { if (!(o instanceof EveryAST)) { return false; } EveryAST oe = (EveryAST) o; return value.equals(oe.value) && unit.equals(oe.unit); }
/** * Gets the value of the requested unit. * * <p>This returns a value for each of the three supported units, {@link ChronoUnit#YEARS YEARS}, * {@link ChronoUnit#MONTHS MONTHS} and {@link ChronoUnit#DAYS DAYS}. All other units throw an * exception. * * @param unit the {@code TemporalUnit} for which to return the value * @return the long value of the unit * @throws DateTimeException if the unit is not supported */ @Override public long get(TemporalUnit unit) { if (unit == ChronoUnit.YEARS) { return getYears(); } else if (unit == ChronoUnit.MONTHS) { return getMonths(); } else if (unit == ChronoUnit.DAYS) { return getDays(); } else { throw new DateTimeException("Unsupported unit: " + unit.getName()); } }
public ReceivedResponse request(URI uri, Duration duration, Action<? super RequestSpec> action) throws Throwable { CountDownLatch latch = new CountDownLatch(1); AtomicReference<ExecResult<ReceivedResponse>> result = new AtomicReference<>(); try (ExecController execController = new DefaultExecController(2)) { execController .fork() .onComplete(e -> {}) .start( e -> { HttpClient.httpClient(UnpooledByteBufAllocator.DEFAULT, Integer.MAX_VALUE) .request(uri, action.prepend(s -> s.readTimeout(Duration.ofHours(1)))) .map( response -> { TypedData responseBody = response.getBody(); ByteBuf responseBodyBuffer = responseBody.getBuffer(); responseBodyBuffer = Unpooled.unreleasableBuffer(responseBodyBuffer.retain()); return new DefaultReceivedResponse( response.getStatus(), response.getHeaders(), new ByteBufBackedTypedData( responseBodyBuffer, responseBody.getContentType())); }) .connect( new Downstream<DefaultReceivedResponse>() { @Override public void success(DefaultReceivedResponse value) { result.set(ExecResult.of(Result.success(value))); latch.countDown(); } @Override public void error(Throwable throwable) { result.set(ExecResult.of(Result.error(throwable))); latch.countDown(); } @Override public void complete() { result.set(ExecResult.complete()); latch.countDown(); } }); }); try { if (!latch.await(duration.toNanos(), TimeUnit.NANOSECONDS)) { TemporalUnit unit = duration.getUnits().get(0); throw new IllegalStateException( "Request to " + uri + " took more than " + duration.get(unit) + " " + unit.toString() + " to complete"); } } catch (InterruptedException e) { throw Exceptions.uncheck(e); } return result.get().getValueOrThrow(); } }