// Copyright 2017 The Nomulus Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package google.registry.flows.domain; import static com.google.common.collect.ImmutableList.toImmutableList; import static google.registry.util.CollectionUtils.nullToEmpty; import static google.registry.util.CollectionUtils.nullToEmptyImmutableCopy; import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; import com.google.common.collect.ImmutableList; import com.google.common.collect.Streams; import google.registry.model.Buildable; import google.registry.model.ImmutableObject; import google.registry.model.domain.fee.BaseFee; import google.registry.model.domain.fee.BaseFee.FeeType; import google.registry.model.domain.fee.Credit; import google.registry.model.domain.fee.Fee; import org.joda.money.CurrencyUnit; import org.joda.money.Money; /** A collection of fees and credits for a specific EPP transform. */ public class FeesAndCredits extends ImmutableObject implements Buildable { private CurrencyUnit currency; private boolean feeExtensionRequired; private ImmutableList fees; private ImmutableList credits; private Money getTotalCostForType(FeeType type) { Money result = Money.zero(currency); checkArgumentNotNull(type); for (Fee fee : fees) { if (fee.getType() == type) { result = result.plus(fee.getCost()); } } return result; } /** Returns the total cost of all fees and credits for the event. */ public Money getTotalCost() { Money result = Money.zero(currency); for (Fee fee : fees) { result = result.plus(fee.getCost()); } for (Credit credit : credits) { result = result.plus(credit.getCost()); } return result; } /** Returns the create cost for the event. */ public Money getCreateCost() { return getTotalCostForType(FeeType.CREATE); } /** Returns the EAP cost for the event. */ public Money getEapCost() { return getTotalCostForType(FeeType.EAP); } /** Returns the renew cost for the event. */ public Money getRenewCost() { return getTotalCostForType(FeeType.RENEW); } /** Returns the restore cost for the event. */ public Money getRestoreCost() { return getTotalCostForType(FeeType.RESTORE); } /** Returns the list of fees for the event. */ public ImmutableList getFees() { return fees; } /** Returns whether a custom fee is present that requires fee extension acknowledgement. */ public boolean isFeeExtensionRequired() { return feeExtensionRequired; } /** Returns the list of credits for the event. */ public ImmutableList getCredits() { return ImmutableList.copyOf(nullToEmpty(credits)); } /** Returns the currency for all fees in the event. */ public final CurrencyUnit getCurrency() { return currency; } /** Returns all fees and credits for the event. */ public ImmutableList getFeesAndCredits() { return Streams.concat(getFees().stream(), getCredits().stream()).collect(toImmutableList()); } @Override public Builder asBuilder() { return new Builder(clone(this)); } /** A builder for constructing {@link FeesAndCredits} objects, since they are immutable. */ public static class Builder extends Buildable.Builder { public Builder() {} private Builder(FeesAndCredits instance) { super(instance); } public Builder addFeeOrCredit(BaseFee feeOrCredit) { if (feeOrCredit instanceof Credit) { getInstance().credits = new ImmutableList.Builder() .addAll(nullToEmptyImmutableCopy(getInstance().credits)) .add((Credit) feeOrCredit) .build(); } else { getInstance().fees = new ImmutableList.Builder() .addAll(nullToEmptyImmutableCopy(getInstance().fees)) .add((Fee) feeOrCredit) .build(); } return this; } public Builder setFeesAndCredits(ImmutableList feesAndCredits) { ImmutableList.Builder feeBuilder = new ImmutableList.Builder<>(); ImmutableList.Builder creditBuilder = new ImmutableList.Builder<>(); for (BaseFee feeOrCredit : feesAndCredits) { if (feeOrCredit instanceof Credit) { creditBuilder.add((Credit) feeOrCredit); } else { feeBuilder.add((Fee) feeOrCredit); } } getInstance().fees = feeBuilder.build(); getInstance().credits = creditBuilder.build(); return this; } public Builder setFeesAndCredits(BaseFee ... feesAndCredits) { return setFeesAndCredits(ImmutableList.copyOf(feesAndCredits)); } public Builder setCurrency(CurrencyUnit currency) { getInstance().currency = currency; return this; } public Builder setFeeExtensionRequired(boolean feeExtensionRequired) { getInstance().feeExtensionRequired = feeExtensionRequired; return this; } @Override public FeesAndCredits build() { checkArgumentNotNull(getInstance().currency, "Currency must be specified in FeesAndCredits"); getInstance().fees = nullToEmptyImmutableCopy(getInstance().fees); getInstance().credits = nullToEmptyImmutableCopy(getInstance().credits); return getInstance(); } } }