Example #1
0
 public Duration plus(long amountToAdd, TemporalUnit unit) {
   Objects.requireNonNull(unit, "unit");
   if (unit == DAYS) {
     return plus(Math.multiplyExact(amountToAdd, SECONDS_PER_DAY), 0);
   }
   if (unit.isDurationEstimated()) {
     throw new UnsupportedTemporalTypeException("Unit must not have an estimated duration");
   }
   if (amountToAdd == 0) {
     return this;
   }
   if (unit instanceof ChronoUnit) {
     switch ((ChronoUnit) unit) {
       case NANOS:
         return plusNanos(amountToAdd);
       case MICROS:
         return plusSeconds((amountToAdd / (1000_000L * 1000)) * 1000)
             .plusNanos((amountToAdd % (1000_000L * 1000)) * 1000);
       case MILLIS:
         return plusMillis(amountToAdd);
       case SECONDS:
         return plusSeconds(amountToAdd);
     }
     return plusSeconds(Math.multiplyExact(unit.getDuration().seconds, amountToAdd));
   }
   Duration duration = unit.getDuration().multipliedBy(amountToAdd);
   return plusSeconds(duration.getSeconds()).plusNanos(duration.getNano());
 }
Example #2
0
 public Duration minus(Duration duration) {
   long secsToSubtract = duration.getSeconds();
   int nanosToSubtract = duration.getNano();
   if (secsToSubtract == Long.MIN_VALUE) {
     return plus(Long.MAX_VALUE, -nanosToSubtract).plus(1, 0);
   }
   return plus(-secsToSubtract, -nanosToSubtract);
 }
Example #3
0
 public static Duration from(TemporalAmount amount) {
   Objects.requireNonNull(amount, "amount");
   Duration duration = ZERO;
   for (TemporalUnit unit : amount.getUnits()) {
     duration = duration.plus(amount.get(unit), unit);
   }
   return duration;
 }
Example #4
0
 public Duration plus(Duration duration) {
   return plus(duration.getSeconds(), duration.getNano());
 }
Example #5
0
 static Duration readExternal(DataInput in) throws IOException {
   long seconds = in.readLong();
   int nanos = in.readInt();
   return Duration.ofSeconds(seconds, nanos);
 }
Example #6
0
 public static Duration of(long amount, TemporalUnit unit) {
   return ZERO.plus(amount, unit);
 }