Exemplo n.º 1
1
 @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);
 }
  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);
  }
Exemplo n.º 3
0
 /**
  * Returns a new instance with each element in this period multiplied by the specified scalar.
  *
  * <p>This returns a period with each of the years, months and days units individually multiplied.
  * For example, a period of "2 years, -3 months and 4 days" multiplied by 3 will return "6 years,
  * -9 months and 12 days". No normalization is performed.
  *
  * @param scalar the scalar to multiply by, not null
  * @return a {@code Period} based on this period with the amounts multiplied by the scalar, not
  *     null
  * @throws ArithmeticException if numeric overflow occurs
  */
 public Period multipliedBy(int scalar) {
   if (this == ZERO || scalar == 1) {
     return this;
   }
   return create(
       Math.multiplyExact(years, scalar),
       Math.multiplyExact(months, scalar),
       Math.multiplyExact(days, scalar));
 }
Exemplo n.º 4
0
 @Override
 boolean applyAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
   Attachment.MonetarySystemExchangeBuy attachment =
       (Attachment.MonetarySystemExchangeBuy) transaction.getAttachment();
   if (senderAccount.getUnconfirmedBalanceNQT()
       >= Math.multiplyExact(attachment.getUnits(), attachment.getRateNQT())) {
     senderAccount.addToUnconfirmedBalanceNQT(
         -Math.multiplyExact(attachment.getUnits(), attachment.getRateNQT()));
     return true;
   }
   return false;
 }
Exemplo n.º 5
0
 @Override
 boolean applyAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
   Attachment.MonetarySystemReserveIncrease attachment =
       (Attachment.MonetarySystemReserveIncrease) transaction.getAttachment();
   Currency currency = Currency.getCurrency(attachment.getCurrencyId());
   if (senderAccount.getUnconfirmedBalanceNQT()
       >= Math.multiplyExact(
           currency.getReserveSupply(), attachment.getAmountPerUnitNQT())) {
     senderAccount.addToUnconfirmedBalanceNQT(
         -Math.multiplyExact(currency.getReserveSupply(), attachment.getAmountPerUnitNQT()));
     return true;
   }
   return false;
 }
Exemplo n.º 6
0
 @Override
 void undoAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
   Attachment.MonetarySystemExchangeBuy attachment =
       (Attachment.MonetarySystemExchangeBuy) transaction.getAttachment();
   senderAccount.addToUnconfirmedBalanceNQT(
       Math.multiplyExact(attachment.getUnits(), attachment.getRateNQT()));
 }
Exemplo n.º 7
0
 @Override
 boolean applyAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
   Attachment.MonetarySystemPublishExchangeOffer attachment =
       (Attachment.MonetarySystemPublishExchangeOffer) transaction.getAttachment();
   if (senderAccount.getUnconfirmedBalanceNQT()
           >= Math.multiplyExact(
               attachment.getInitialBuySupply(), attachment.getBuyRateNQT())
       && senderAccount.getUnconfirmedCurrencyUnits(attachment.getCurrencyId())
           >= attachment.getInitialSellSupply()) {
     senderAccount.addToUnconfirmedBalanceNQT(
         -Math.multiplyExact(attachment.getInitialBuySupply(), attachment.getBuyRateNQT()));
     senderAccount.addToUnconfirmedCurrencyUnits(
         attachment.getCurrencyId(), -attachment.getInitialSellSupply());
     return true;
   }
   return false;
 }
Exemplo n.º 8
0
 @Override
 public Integer multiply(Integer other) {
   try {
     return new ScalableInt32(Math.multiplyExact(this.value, ((ScalableInt32) other).value));
   } catch (ClassCastException e) {
     return promoteNext().multiply(other);
   } catch (ArithmeticException e) {
     return promoteNext().multiply(other);
   }
 }
  static void removeOffer(CurrencyBuyOffer buyOffer) {
    CurrencySellOffer sellOffer = buyOffer.getCounterOffer();

    CurrencyBuyOffer.remove(buyOffer);
    CurrencySellOffer.remove(sellOffer);

    Account account = Account.getAccount(buyOffer.getAccountId());
    account.addToUnconfirmedBalanceNQT(
        Math.multiplyExact(buyOffer.getSupply(), buyOffer.getRateNQT()));
    account.addToUnconfirmedCurrencyUnits(buyOffer.getCurrencyId(), sellOffer.getSupply());
  }
Exemplo n.º 10
0
 @Override
 void undoAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
   Attachment.MonetarySystemPublishExchangeOffer attachment =
       (Attachment.MonetarySystemPublishExchangeOffer) transaction.getAttachment();
   senderAccount.addToUnconfirmedBalanceNQT(
       Math.multiplyExact(attachment.getInitialBuySupply(), attachment.getBuyRateNQT()));
   Currency currency = Currency.getCurrency(attachment.getCurrencyId());
   if (currency != null) {
     senderAccount.addToUnconfirmedCurrencyUnits(
         attachment.getCurrencyId(), attachment.getInitialSellSupply());
   }
 }
Exemplo n.º 11
0
 private static int parseNumber(CharSequence text, String str, int negate) {
   if (str == null) {
     return 0;
   }
   int val = Integer.parseInt(str);
   try {
     return Math.multiplyExact(val, negate);
   } catch (ArithmeticException ex) {
     throw (DateTimeParseException)
         new DateTimeParseException("Text cannot be parsed to a Period", text, 0).initCause(ex);
   }
 }
 private static long parseNumber(
     CharSequence text, String parsed, int multiplier, String errorText) {
   // regex limits to [-+]?[0-9]+
   if (parsed == null) {
     return 0;
   }
   try {
     long val = Long.parseLong(parsed);
     return Math.multiplyExact(val, multiplier);
   } catch (NumberFormatException | ArithmeticException ex) {
     throw (DateTimeParseException)
         new DateTimeParseException("Text cannot be parsed to a Duration: " + errorText, text, 0)
             .initCause(ex);
   }
 }
Exemplo n.º 13
0
 @Override
 void undoAttachmentUnconfirmed(Transaction transaction, Account senderAccount) {
   Attachment.MonetarySystemReserveIncrease attachment =
       (Attachment.MonetarySystemReserveIncrease) transaction.getAttachment();
   long reserveSupply;
   Currency currency = Currency.getCurrency(attachment.getCurrencyId());
   if (currency != null) {
     reserveSupply = currency.getReserveSupply();
   } else { // currency must have been deleted, get reserve supply from the original issuance
            // transaction
     Transaction currencyIssuance =
         Nxt.getBlockchain().getTransaction(attachment.getCurrencyId());
     Attachment.MonetarySystemCurrencyIssuance currencyIssuanceAttachment =
         (Attachment.MonetarySystemCurrencyIssuance) currencyIssuance.getAttachment();
     reserveSupply = currencyIssuanceAttachment.getReserveSupply();
   }
   senderAccount.addToUnconfirmedBalanceNQT(
       Math.multiplyExact(reserveSupply, attachment.getAmountPerUnitNQT()));
 }
Exemplo n.º 14
0
 AbstractDate plusWeeks(long amountToAdd) {
   return plusDays(Math.multiplyExact(amountToAdd, lengthOfWeek()));
 }