killbill-memoizeit

catalog: abstract JDK to Joda conversion This relates to

2/8/2016 6:27:45 PM

Details

diff --git a/api/src/main/java/org/killbill/billing/callcontext/InternalTenantContext.java b/api/src/main/java/org/killbill/billing/callcontext/InternalTenantContext.java
index f35241d..8995fd1 100644
--- a/api/src/main/java/org/killbill/billing/callcontext/InternalTenantContext.java
+++ b/api/src/main/java/org/killbill/billing/callcontext/InternalTenantContext.java
@@ -25,7 +25,7 @@ import org.killbill.billing.util.callcontext.TenantContext;
 /**
  * Internal use only
  */
-public class InternalTenantContext {
+public class InternalTenantContext extends TimeAwareContext {
 
     protected final Long tenantRecordId;
     protected final Long accountRecordId;
diff --git a/api/src/main/java/org/killbill/billing/callcontext/TimeAwareContext.java b/api/src/main/java/org/killbill/billing/callcontext/TimeAwareContext.java
new file mode 100644
index 0000000..9a08ee2
--- /dev/null
+++ b/api/src/main/java/org/killbill/billing/callcontext/TimeAwareContext.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2016 Groupon, Inc
+ * Copyright 2016 The Billing Project, LLC
+ *
+ * The Billing Project licenses this file to you 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 org.killbill.billing.callcontext;
+
+import java.util.Date;
+
+import org.joda.time.DateTime;
+
+public class TimeAwareContext {
+
+    // From JDK to Joda (see http://www.joda.org/joda-time/userguide.html#JDK_Interoperability)
+    public DateTime toDateTime(final Date date) {
+        return new DateTime(date);
+    }
+}
diff --git a/catalog/src/main/java/org/killbill/billing/catalog/DefaultPlan.java b/catalog/src/main/java/org/killbill/billing/catalog/DefaultPlan.java
index 52e02e7..d0945db 100644
--- a/catalog/src/main/java/org/killbill/billing/catalog/DefaultPlan.java
+++ b/catalog/src/main/java/org/killbill/billing/catalog/DefaultPlan.java
@@ -1,7 +1,9 @@
 /*
  * Copyright 2010-2013 Ning, Inc.
+ * Copyright 2014-2016 Groupon, Inc
+ * Copyright 2014-2016 The Billing Project, LLC
  *
- * Ning licenses this file to you under the Apache License, version 2.0
+ * The Billing Project licenses this file to you 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:
  *
@@ -19,6 +21,8 @@ package org.killbill.billing.catalog;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Date;
 import java.util.Iterator;
 
@@ -73,7 +77,6 @@ public class DefaultPlan extends ValidatingConfig<StandaloneCatalog> implements 
     @XmlElement(required = false)
     private Integer plansAllowedInBundle = 1;
 
-
     public DefaultPlan() {
         initialPhases = new DefaultPlanPhase[0];
     }
@@ -83,49 +86,64 @@ public class DefaultPlan extends ValidatingConfig<StandaloneCatalog> implements 
         this.effectiveDateForExistingSubscriptons = in.getEffectiveDateForExistingSubscriptons();
         this.product = (DefaultProduct) in.getProduct();
         this.initialPhases = new DefaultPlanPhase[in.getInitialPhases().length];
-        for (int i = 0; i< overrides.length - 1; i++) {
+        for (int i = 0; i < overrides.length - 1; i++) {
             final DefaultPlanPhase newPhase = new DefaultPlanPhase(this, in.getInitialPhases()[i], overrides[i]);
             initialPhases[i] = newPhase;
         }
         this.finalPhase = new DefaultPlanPhase(this, in.getFinalPhase(), overrides[overrides.length - 1]);
 
     }
-    /* (non-Javadoc)
-      * @see org.killbill.billing.catalog.IPlan#getEffectiveDateForExistingSubscriptons()
-      */
+
     @Override
     public Date getEffectiveDateForExistingSubscriptons() {
         return effectiveDateForExistingSubscriptons;
-    }    /* (non-Javadoc)
-	 * @see org.killbill.billing.catalog.IPlan#getPhases()
-	 */
+    }
+
+    public void setEffectiveDateForExistingSubscriptons(
+            final Date effectiveDateForExistingSubscriptons) {
+        this.effectiveDateForExistingSubscriptons = effectiveDateForExistingSubscriptons;
+    }
 
     @Override
     public DefaultPlanPhase[] getInitialPhases() {
         return initialPhases;
     }
 
-    /* (non-Javadoc)
-	 * @see org.killbill.billing.catalog.IPlan#getProduct()
-	 */
+    public DefaultPlan setInitialPhases(final DefaultPlanPhase[] phases) {
+        this.initialPhases = phases;
+        return this;
+    }
+
     @Override
     public Product getProduct() {
         return product;
     }
 
-    /* (non-Javadoc)
-      * @see org.killbill.billing.catalog.IPlan#getName()
-      */
+    public DefaultPlan setProduct(final DefaultProduct product) {
+        this.product = product;
+        return this;
+    }
+
     @Override
     public String getName() {
         return name;
     }
 
+    public DefaultPlan setName(final String name) {
+        this.name = name;
+        return this;
+    }
+
     @Override
     public DefaultPlanPhase getFinalPhase() {
         return finalPhase;
     }
 
+    public DefaultPlan setFinalPhase(final DefaultPlanPhase finalPhase) {
+        this.finalPhase = finalPhase;
+        return this;
+    }
+
     @Override
     public PlanPhase[] getAllPhases() {
         final int length = (initialPhases == null || initialPhases.length == 0) ? 1 : (initialPhases.length + 1);
@@ -156,23 +174,23 @@ public class DefaultPlan extends ValidatingConfig<StandaloneCatalog> implements 
         return finalPhase.getRecurring() != null ? finalPhase.getRecurring().getBillingPeriod() : BillingPeriod.NO_BILLING_PERIOD;
     }
 
-    /* (non-Javadoc)
-      * @see org.killbill.billing.catalog.IPlan#getPlansAllowedInBundle()
-      */
     @Override
     public int getPlansAllowedInBundle() {
         return plansAllowedInBundle;
     }
 
+    public DefaultPlan setPlansAllowedInBundle(final Integer plansAllowedInBundle) {
+        this.plansAllowedInBundle = plansAllowedInBundle;
+        return this;
+    }
+
     /* (non-Javadoc)
       * @see org.killbill.billing.catalog.IPlan#getPhaseIterator()
       */
     @Override
     public Iterator<PlanPhase> getInitialPhaseIterator() {
-        final ArrayList<PlanPhase> list = new ArrayList<PlanPhase>();
-        for (final DefaultPlanPhase p : initialPhases) {
-            list.add(p);
-        }
+        final Collection<PlanPhase> list = new ArrayList<PlanPhase>();
+        Collections.addAll(list, initialPhases);
         return list.iterator();
     }
 
@@ -194,7 +212,7 @@ public class DefaultPlan extends ValidatingConfig<StandaloneCatalog> implements 
     @Override
     public ValidationErrors validate(final StandaloneCatalog catalog, final ValidationErrors errors) {
         if (effectiveDateForExistingSubscriptons != null &&
-                catalog.getEffectiveDate().getTime() > effectiveDateForExistingSubscriptons.getTime()) {
+            catalog.getEffectiveDate().getTime() > effectiveDateForExistingSubscriptons.getTime()) {
             errors.add(new ValidationError(String.format("Price effective date %s is before catalog effective date '%s'",
                                                          effectiveDateForExistingSubscriptons,
                                                          catalog.getEffectiveDate().getTime()),
@@ -206,40 +224,10 @@ public class DefaultPlan extends ValidatingConfig<StandaloneCatalog> implements 
         return errors;
     }
 
-    public void setEffectiveDateForExistingSubscriptons(
-            final Date effectiveDateForExistingSubscriptons) {
-        this.effectiveDateForExistingSubscriptons = effectiveDateForExistingSubscriptons;
-    }
-
-    public DefaultPlan setName(final String name) {
-        this.name = name;
-        return this;
-    }
-
-    public DefaultPlan setFinalPhase(final DefaultPlanPhase finalPhase) {
-        this.finalPhase = finalPhase;
-        return this;
-    }
-
-    public DefaultPlan setProduct(final DefaultProduct product) {
-        this.product = product;
-        return this;
-    }
-
-    public DefaultPlan setInitialPhases(final DefaultPlanPhase[] phases) {
-        this.initialPhases = phases;
-        return this;
-    }
-
-    public DefaultPlan setPlansAllowedInBundle(final Integer plansAllowedInBundle) {
-        this.plansAllowedInBundle = plansAllowedInBundle;
-        return this;
-    }
-
     @Override
     public DateTime dateOfFirstRecurringNonZeroCharge(final DateTime subscriptionStartDate, final PhaseType initialPhaseType) {
-        DateTime result = subscriptionStartDate.toDateTime();
-        boolean skipPhase = initialPhaseType == null ? false : true;
+        DateTime result = subscriptionStartDate;
+        boolean skipPhase = initialPhaseType != null;
         for (final PlanPhase phase : getAllPhases()) {
             if (skipPhase) {
                 if (phase.getPhaseType() != initialPhaseType) {
@@ -303,8 +291,8 @@ public class DefaultPlan extends ValidatingConfig<StandaloneCatalog> implements 
     @Override
     public String toString() {
         return "DefaultPlan [name=" + name + ", effectiveDateForExistingSubscriptons="
-                + effectiveDateForExistingSubscriptons + ", product=" + product + ", initialPhases="
-                + Arrays.toString(initialPhases) + ", finalPhase=" + finalPhase + ", plansAllowedInBundle="
-                + plansAllowedInBundle + "]";
+               + effectiveDateForExistingSubscriptons + ", product=" + product + ", initialPhases="
+               + Arrays.toString(initialPhases) + ", finalPhase=" + finalPhase + ", plansAllowedInBundle="
+               + plansAllowedInBundle + "]";
     }
 }
diff --git a/catalog/src/main/java/org/killbill/billing/catalog/io/VersionedCatalogLoader.java b/catalog/src/main/java/org/killbill/billing/catalog/io/VersionedCatalogLoader.java
index 3b5f7de..0d85afc 100644
--- a/catalog/src/main/java/org/killbill/billing/catalog/io/VersionedCatalogLoader.java
+++ b/catalog/src/main/java/org/killbill/billing/catalog/io/VersionedCatalogLoader.java
@@ -55,13 +55,10 @@ public class VersionedCatalogLoader implements CatalogLoader {
         this.internalCallContextFactory = internalCallContextFactory;
     }
 
-    /* (non-Javadoc)
-      * @see org.killbill.billing.catalog.io.ICatalogLoader#loadDefaultCatalog(java.lang.String)
-      */
     @Override
     public VersionedCatalog loadDefaultCatalog(final String uriString) throws CatalogApiException {
         try {
-            List<URI> xmlURIs;
+            final List<URI> xmlURIs;
             if (uriString.endsWith(XML_EXTENSION)) { // Assume its an xml file
                 xmlURIs = new ArrayList<URI>();
                 xmlURIs.add(new URI(uriString));
diff --git a/catalog/src/main/java/org/killbill/billing/catalog/StandaloneCatalogWithPriceOverride.java b/catalog/src/main/java/org/killbill/billing/catalog/StandaloneCatalogWithPriceOverride.java
index ac2326a..f7289fc 100644
--- a/catalog/src/main/java/org/killbill/billing/catalog/StandaloneCatalogWithPriceOverride.java
+++ b/catalog/src/main/java/org/killbill/billing/catalog/StandaloneCatalogWithPriceOverride.java
@@ -1,6 +1,6 @@
 /*
- * Copyright 2014-2015 Groupon, Inc
- * Copyright 2014-2015 The Billing Project, LLC
+ * Copyright 2014-2016 Groupon, Inc
+ * Copyright 2014-2016 The Billing Project, LLC
  *
  * The Billing Project licenses this file to you under the Apache License, version 2.0
  * (the "License"); you may not use this file except in compliance with the
@@ -24,7 +24,6 @@ import java.util.regex.Matcher;
 
 import javax.annotation.Nullable;
 
-import org.joda.time.DateTime;
 import org.killbill.billing.callcontext.InternalCallContext;
 import org.killbill.billing.callcontext.InternalTenantContext;
 import org.killbill.billing.catalog.api.BillingActionPolicy;
@@ -141,15 +140,14 @@ public class StandaloneCatalogWithPriceOverride extends ValidatingConfig<Standal
         }
 
         final InternalCallContext internalCallContext = internalCallContextFactory.createInternalCallContext(overrides.getCallContext());
-        return priceOverride.getOrCreateOverriddenPlan(defaultPlan, new DateTime(getEffectiveDate()), overrides.getOverrides(), internalCallContext);
+        return priceOverride.getOrCreateOverriddenPlan(defaultPlan, internalCallContext.toDateTime(getEffectiveDate()), overrides.getOverrides(), internalCallContext);
     }
 
     @Override
     public Plan findCurrentPlan(final String planName) throws CatalogApiException {
-
         final Matcher m = DefaultPriceOverride.CUSTOM_PLAN_NAME_PATTERN.matcher(planName);
         if (m.matches()) {
-            final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(tenantRecordId, null);
+            final InternalTenantContext internalTenantContext = createInternalTenantContext();
             return priceOverride.getOverriddenPlan(planName, standaloneCatalog, internalTenantContext);
         }
         return standaloneCatalog.findCurrentPlan(planName);
@@ -165,8 +163,8 @@ public class StandaloneCatalogWithPriceOverride extends ValidatingConfig<Standal
         final String planName = DefaultPlanPhase.planName(phaseName);
         final Matcher m = DefaultPriceOverride.CUSTOM_PLAN_NAME_PATTERN.matcher(planName);
         if (m.matches()) {
-            final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(tenantRecordId, null);
-            Plan plan = priceOverride.getOverriddenPlan(planName, standaloneCatalog, internalTenantContext);
+            final InternalTenantContext internalTenantContext = createInternalTenantContext();
+            final Plan plan = priceOverride.getOverriddenPlan(planName, standaloneCatalog, internalTenantContext);
             return plan.findPhase(phaseName);
         }
         return standaloneCatalog.findCurrentPhase(phaseName);
@@ -241,4 +239,7 @@ public class StandaloneCatalogWithPriceOverride extends ValidatingConfig<Standal
         return standaloneCatalog.findCurrentPriceList(priceListName);
     }
 
+    private InternalTenantContext createInternalTenantContext() {
+        return internalCallContextFactory.createInternalTenantContext(tenantRecordId, null);
+    }
 }
diff --git a/catalog/src/main/java/org/killbill/billing/catalog/VersionedCatalog.java b/catalog/src/main/java/org/killbill/billing/catalog/VersionedCatalog.java
index b0aa352..f30b9f0 100644
--- a/catalog/src/main/java/org/killbill/billing/catalog/VersionedCatalog.java
+++ b/catalog/src/main/java/org/killbill/billing/catalog/VersionedCatalog.java
@@ -1,7 +1,7 @@
 /*
  * Copyright 2010-2013 Ning, Inc.
- * Copyright 2014 Groupon, Inc
- * Copyright 2014 The Billing Project, LLC
+ * Copyright 2014-2016 Groupon, Inc
+ * Copyright 2014-2016 The Billing Project, LLC
  *
  * The Billing Project licenses this file to you under the Apache License, version 2.0
  * (the "License"); you may not use this file except in compliance with the
@@ -65,21 +65,23 @@ import org.killbill.xmlloader.ValidationErrors;
 public class VersionedCatalog extends ValidatingConfig<StandaloneCatalogWithPriceOverride> implements Catalog, StaticCatalog {
 
     private final Clock clock;
+    private final InternalTenantContext internalTenantContext;
+    @XmlElement(name = "catalogVersion", required = true)
+    private final List<StandaloneCatalogWithPriceOverride> versions;
     private String catalogName;
     private BillingMode recurringBillingMode;
 
-    @XmlElement(name = "catalogVersion", required = true)
-    private List<StandaloneCatalogWithPriceOverride> versions;
-
     // Required for JAXB deserialization
     public VersionedCatalog() {
         this.clock = null;
-        versions = new ArrayList<StandaloneCatalogWithPriceOverride>();
+        this.versions = new ArrayList<StandaloneCatalogWithPriceOverride>();
+        this.internalTenantContext = new InternalTenantContext(null, null);
     }
 
     public VersionedCatalog(final Clock clock) {
         this.clock = clock;
-        versions = new ArrayList<StandaloneCatalogWithPriceOverride>();
+        this.versions = new ArrayList<StandaloneCatalogWithPriceOverride>();
+        this.internalTenantContext = new InternalTenantContext(null, null);
     }
 
     public VersionedCatalog(final Clock clock, final String catalogName, final BillingMode recurringBillingMode, final List<StandaloneCatalogWithPriceOverride> versions, final InternalTenantContext tenantContext) {
@@ -91,9 +93,9 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalogWithPric
             final StandaloneCatalogWithPriceOverride catalogWithTenantInfo = new StandaloneCatalogWithPriceOverride(cur, tenantContext);
             this.versions.add(catalogWithTenantInfo);
         }
+        this.internalTenantContext = tenantContext;
     }
 
-
     //
     // Private methods
     //
@@ -154,16 +156,16 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalogWithPric
                           final DateTime subscriptionStartDate)
             throws CatalogApiException {
         final List<StandaloneCatalogWithPriceOverride> catalogs = versionsBeforeDate(requestedDate.toDate());
-        if (catalogs.size() == 0) {
+        if (catalogs.isEmpty()) {
             throw new CatalogApiException(ErrorCode.CAT_NO_CATALOG_FOR_GIVEN_DATE, requestedDate.toDate().toString());
         }
 
         for (int i = catalogs.size() - 1; i >= 0; i--) { // Working backwards to find the latest applicable plan
             final StandaloneCatalogWithPriceOverride c = catalogs.get(i);
-            Plan plan;
+            final Plan plan;
             try {
                 plan = wrapper.findPlan(c);
-            } catch (CatalogApiException e) {
+            } catch (final CatalogApiException e) {
                 if (e.getCode() != ErrorCode.CAT_NO_SUCH_PLAN.getCode()) {
                     throw e;
                 } else {
@@ -172,12 +174,12 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalogWithPric
                 }
             }
 
-            DateTime catalogEffectiveDate = new DateTime(c.getEffectiveDate());
+            final DateTime catalogEffectiveDate = internalTenantContext.toDateTime(c.getEffectiveDate());
             if (!subscriptionStartDate.isBefore(catalogEffectiveDate)) { // Its a new subscription this plan always applies
                 return plan;
             } else { //Its an existing subscription
                 if (plan.getEffectiveDateForExistingSubscriptons() != null) { //if it is null any change to this does not apply to existing subscriptions
-                    final DateTime existingSubscriptionDate = new DateTime(plan.getEffectiveDateForExistingSubscriptons());
+                    final DateTime existingSubscriptionDate = internalTenantContext.toDateTime(plan.getEffectiveDateForExistingSubscriptons());
                     if (requestedDate.isAfter(existingSubscriptionDate)) { // this plan is now applicable to existing subs
                         return plan;
                     }
@@ -271,10 +273,10 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalogWithPric
 
     @Override
     public Plan createOrFindPlan(final String productName,
-                         final BillingPeriod term,
-                         final String priceListName,
-                         final PlanPhasePriceOverridesWithCallContext overrides,
-                         final DateTime requestedDate)
+                                 final BillingPeriod term,
+                                 final String priceListName,
+                                 final PlanPhasePriceOverridesWithCallContext overrides,
+                                 final DateTime requestedDate)
             throws CatalogApiException {
         return versionForDate(requestedDate).createOrFindCurrentPlan(productName, term, priceListName, overrides);
     }
@@ -289,11 +291,11 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalogWithPric
 
     @Override
     public Plan createOrFindPlan(final String productName,
-                         final BillingPeriod term,
-                         final String priceListName,
-                         final PlanPhasePriceOverridesWithCallContext overrides,
-                         final DateTime requestedDate,
-                         final DateTime subscriptionStartDate)
+                                 final BillingPeriod term,
+                                 final String priceListName,
+                                 final PlanPhasePriceOverridesWithCallContext overrides,
+                                 final DateTime requestedDate,
+                                 final DateTime subscriptionStartDate)
             throws CatalogApiException {
         return findPlan(new PlanRequestWrapper(productName, term, priceListName, overrides), requestedDate, subscriptionStartDate);
     }
@@ -434,7 +436,7 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalogWithPric
 
     @Override
     public Plan createOrFindCurrentPlan(final String productName, final BillingPeriod term,
-                                final String priceList, PlanPhasePriceOverridesWithCallContext overrides) throws CatalogApiException {
+                                        final String priceList, final PlanPhasePriceOverridesWithCallContext overrides) throws CatalogApiException {
         return versionForDate(clock.getUTCNow()).createOrFindCurrentPlan(productName, term, priceList, overrides);
     }
 
@@ -512,7 +514,7 @@ public class VersionedCatalog extends ValidatingConfig<StandaloneCatalogWithPric
     }
 
     @Override
-    public boolean compliesWithLimits(String phaseName, String unit, double value) throws CatalogApiException {
+    public boolean compliesWithLimits(final String phaseName, final String unit, final double value) throws CatalogApiException {
         return versionForDate(clock.getUTCNow()).compliesWithLimits(phaseName, unit, value);
     }
 }