private static Duration create(
     boolean negate, long daysAsSecs, long hoursAsSecs, long minsAsSecs, long secs, int nanos) {
   long seconds =
       Math.addExact(daysAsSecs, Math.addExact(hoursAsSecs, Math.addExact(minsAsSecs, secs)));
   if (negate) {
     return Duration.ofSeconds(seconds, nanos).negated();
   }
   return Duration.ofSeconds(seconds, nanos);
 }
  static void exchangeNXTForCurrency(
      Transaction transaction,
      Account account,
      final long currencyId,
      final long rateNQT,
      long units) {
    long extraUnits = 0;
    long remainingAmountNQT = Math.multiplyExact(units, rateNQT);

    List<CurrencySellOffer> currencySellOffers = new ArrayList<>();
    try (DbIterator<CurrencySellOffer> offers =
        CurrencySellOffer.getOffers(
            new ValidOffersDbClause(currencyId, rateNQT, false),
            0,
            -1,
            " ORDER BY rate ASC, creation_height ASC, transaction_height ASC, transaction_index ASC ")) {
      for (CurrencySellOffer offer : offers) {
        currencySellOffers.add(offer);
      }
    }

    for (CurrencySellOffer offer : currencySellOffers) {
      if (remainingAmountNQT == 0) {
        break;
      }
      long curUnits =
          Math.min(
              Math.min(remainingAmountNQT / offer.getRateNQT(), offer.getSupply()),
              offer.getLimit());
      if (curUnits == 0) {
        continue;
      }
      long curAmountNQT = Math.multiplyExact(curUnits, offer.getRateNQT());

      extraUnits = Math.addExact(extraUnits, curUnits);
      remainingAmountNQT = Math.subtractExact(remainingAmountNQT, curAmountNQT);

      offer.decreaseLimitAndSupply(curUnits);
      long excess = offer.getCounterOffer().increaseSupply(curUnits);

      Account counterAccount = Account.getAccount(offer.getAccountId());
      counterAccount.addToBalanceNQT(curAmountNQT);
      counterAccount.addToUnconfirmedBalanceNQT(
          Math.addExact(
              Math.multiplyExact(
                  curUnits - excess, offer.getRateNQT() - offer.getCounterOffer().getRateNQT()),
              Math.multiplyExact(excess, offer.getRateNQT())));
      counterAccount.addToCurrencyUnits(currencyId, -curUnits);
      Exchange.addExchange(
          transaction, currencyId, offer, offer.getAccountId(), account.getId(), curUnits);
    }

    account.addToCurrencyAndUnconfirmedCurrencyUnits(currencyId, extraUnits);
    account.addToBalanceNQT(-(Math.multiplyExact(units, rateNQT) - remainingAmountNQT));
    account.addToUnconfirmedBalanceNQT(remainingAmountNQT);
  }
 AbstractDate plusYears(long yearsToAdd) {
   if (yearsToAdd == 0) {
     return this;
   }
   int newYear = YEAR.checkValidIntValue(Math.addExact(getProlepticYear(), yearsToAdd));
   return resolvePrevious(newYear, getMonth(), getDayOfMonth());
 }
 @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);
 }
 AbstractDate plusMonths(long months) {
   if (months == 0) {
     return this;
   }
   long curEm = getProlepticMonth();
   long calcEm = Math.addExact(curEm, months);
   int newYear = Math.toIntExact(Math.floorDiv(calcEm, lengthOfYearInMonths()));
   int newMonth = (int) (Math.floorMod(calcEm, lengthOfYearInMonths()) + 1);
   return resolvePrevious(newYear, newMonth, getDayOfMonth());
 }
 @Override
 public Integer plus(Integer other) {
   try {
     return new ScalableInt32(Math.addExact(this.value, ((ScalableInt32) other).value));
   } catch (ClassCastException e) {
     return promoteNext().plus(other);
   } catch (ArithmeticException e) {
     return promoteNext().plus(other);
   }
 }
 AbstractDate plusDays(long days) {
   if (days == 0) {
     return this;
   }
   return resolveEpochDay(Math.addExact(toEpochDay(), days));
 }
 long increaseSupply(long delta) {
   long excess = Math.max(Math.addExact(supply, Math.subtractExact(delta, limit)), 0);
   supply += delta - excess;
   return excess;
 }
Beispiel #9
1
 /**
  * Returns a copy of this period with the specified days added.
  *
  * <p>This adds the amount to the days unit in a copy of this period. The years and months units
  * are unaffected. For example, "1 year, 6 months and 3 days" plus 2 days returns "1 year, 6
  * months and 5 days".
  *
  * <p>This instance is immutable and unaffected by this method call.
  *
  * @param daysToAdd the days to add, positive or negative
  * @return a {@code Period} based on this period with the specified days added, not null
  * @throws ArithmeticException if numeric overflow occurs
  */
 public Period plusDays(long daysToAdd) {
   if (daysToAdd == 0) {
     return this;
   }
   return create(years, months, Math.toIntExact(Math.addExact(days, daysToAdd)));
 }
Beispiel #10
1
 /**
  * Returns a copy of this period with the specified months added.
  *
  * <p>This adds the amount to the months unit in a copy of this period. The years and days units
  * are unaffected. For example, "1 year, 6 months and 3 days" plus 2 months returns "1 year, 8
  * months and 3 days".
  *
  * <p>This instance is immutable and unaffected by this method call.
  *
  * @param monthsToAdd the months to add, positive or negative
  * @return a {@code Period} based on this period with the specified months added, not null
  * @throws ArithmeticException if numeric overflow occurs
  */
 public Period plusMonths(long monthsToAdd) {
   if (monthsToAdd == 0) {
     return this;
   }
   return create(years, Math.toIntExact(Math.addExact(months, monthsToAdd)), days);
 }
Beispiel #11
1
 /**
  * Returns a copy of this period with the specified years added.
  *
  * <p>This adds the amount to the years unit in a copy of this period. The months and days units
  * are unaffected. For example, "1 year, 6 months and 3 days" plus 2 years returns "3 years, 6
  * months and 3 days".
  *
  * <p>This instance is immutable and unaffected by this method call.
  *
  * @param yearsToAdd the years to add, positive or negative
  * @return a {@code Period} based on this period with the specified years added, not null
  * @throws ArithmeticException if numeric overflow occurs
  */
 public Period plusYears(long yearsToAdd) {
   if (yearsToAdd == 0) {
     return this;
   }
   return create(Math.toIntExact(Math.addExact(years, yearsToAdd)), months, days);
 }
Beispiel #12
1
 /**
  * Returns a copy of this period with the specified period added.
  *
  * <p>This operates separately on the years, months, days and the normalized time. There is no
  * further normalization beyond the normalized time.
  *
  * <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>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 ArithmeticException if numeric overflow occurs
  */
 public Period plus(Period amountToAdd) {
   return create(
       Math.addExact(years, amountToAdd.years),
       Math.addExact(months, amountToAdd.months),
       Math.addExact(days, amountToAdd.days));
 }