From a5d5c9e2fd82a9fff3075d8f7011227fb56faf41 Mon Sep 17 00:00:00 2001 From: mcilwain Date: Wed, 11 Jan 2017 09:49:07 -0800 Subject: [PATCH] Support creation of EPP fee objects using custom descriptions This is a reasonable thing that custom code might want to do (Donuts already has a use case). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=144216929 --- .../registry/model/domain/fee/BaseFee.java | 5 --- .../registry/model/domain/fee/Credit.java | 12 ++++++- .../google/registry/model/domain/fee/Fee.java | 31 +++++++++++++------ 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/java/google/registry/model/domain/fee/BaseFee.java b/java/google/registry/model/domain/fee/BaseFee.java index 825fc4338..906563e1c 100644 --- a/java/google/registry/model/domain/fee/BaseFee.java +++ b/java/google/registry/model/domain/fee/BaseFee.java @@ -133,11 +133,6 @@ public abstract class BaseFee extends ImmutableObject { return validDateRange; } - protected void generateDescription(Object... args) { - checkState(type != null); - description = type.renderDescription(args); - } - public boolean hasZeroCost() { return cost.signum() == 0; } diff --git a/java/google/registry/model/domain/fee/Credit.java b/java/google/registry/model/domain/fee/Credit.java index 6b844c0f4..29037dd18 100644 --- a/java/google/registry/model/domain/fee/Credit.java +++ b/java/google/registry/model/domain/fee/Credit.java @@ -16,17 +16,27 @@ package google.registry.model.domain.fee; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; import java.math.BigDecimal; /** A credit, in currency units specified elsewhere in the xml, and with an optional description. */ public class Credit extends BaseFee { + + /** Creates a Credit for the given amount and type with the default description. */ public static Credit create(BigDecimal cost, FeeType type, Object... descriptionArgs) { + checkArgumentNotNull(type, "Must specify the type of the credit"); + return createWithCustomDescription(cost, type, type.renderDescription(descriptionArgs)); + } + + /** Creates a Credit for the given amount and type with a custom description. */ + public static Credit createWithCustomDescription( + BigDecimal cost, FeeType type, String description) { Credit instance = new Credit(); instance.cost = checkNotNull(cost); checkArgument(instance.cost.signum() < 0); instance.type = checkNotNull(type); - instance.generateDescription(descriptionArgs); + instance.description = description; return instance; } } diff --git a/java/google/registry/model/domain/fee/Fee.java b/java/google/registry/model/domain/fee/Fee.java index a5742ab28..534f282c5 100644 --- a/java/google/registry/model/domain/fee/Fee.java +++ b/java/google/registry/model/domain/fee/Fee.java @@ -16,6 +16,7 @@ package google.registry.model.domain.fee; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; +import static google.registry.util.PreconditionsUtils.checkArgumentNotNull; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Range; @@ -28,15 +29,14 @@ import org.joda.time.DateTime; * description. */ public class Fee extends BaseFee { + + /** Creates a Fee for the given cost and type with the default description. */ public static Fee create(BigDecimal cost, FeeType type, Object... descriptionArgs) { - Fee instance = new Fee(); - instance.cost = checkNotNull(cost); - checkArgument(instance.cost.signum() >= 0); - instance.type = checkNotNull(type); - instance.generateDescription(descriptionArgs); - return instance; + checkArgumentNotNull(type, "Must specify the type of the fee"); + return createWithCustomDescription(cost, type, type.renderDescription(descriptionArgs)); } + /** Creates a Fee for the given cost, type, and valid date range with the default description. */ public static Fee create( BigDecimal cost, FeeType type, Range validDateRange, Object... descriptionArgs) { Fee instance = create(cost, type, descriptionArgs); @@ -44,8 +44,19 @@ public class Fee extends BaseFee { return instance; } - public static final ImmutableSet FEE_EXTENSION_URIS = ImmutableSet.of( - ServiceExtension.FEE_0_12.getUri(), - ServiceExtension.FEE_0_11.getUri(), - ServiceExtension.FEE_0_6.getUri()); + /** Creates a Fee for the given cost and type with a custom description. */ + public static Fee createWithCustomDescription(BigDecimal cost, FeeType type, String description) { + Fee instance = new Fee(); + instance.cost = checkNotNull(cost); + checkArgument(instance.cost.signum() >= 0); + instance.type = checkNotNull(type); + instance.description = description; + return instance; + } + + public static final ImmutableSet FEE_EXTENSION_URIS = + ImmutableSet.of( + ServiceExtension.FEE_0_12.getUri(), + ServiceExtension.FEE_0_11.getUri(), + ServiceExtension.FEE_0_6.getUri()); }