Skip to content

Commit

Permalink
catalog: Modify catalog api to return Collection instead of array for…
Browse files Browse the repository at this point in the history
… large cardinality Objects (Plan, Product). See #621

This change build upon 3695ec7 where the new CatalogEntityCollection was intrdoduced to allow lookup of entiry in log(N).
With this change, there is no more copy of objects to cross worlds between Collection and arrays, which considerably improves perf.
  • Loading branch information
sbrossie committed Oct 4, 2016
1 parent 5fb1ca8 commit f29457a
Show file tree
Hide file tree
Showing 31 changed files with 381 additions and 343 deletions.
Expand Up @@ -101,7 +101,7 @@ public void testBasic() throws Exception {
final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("foo-monthly", "Foo", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of()); final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("foo-monthly", "Foo", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of());
catalogUserApi.addSimplePlan(desc1, init, testCallContext); catalogUserApi.addSimplePlan(desc1, init, testCallContext);
StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
assertEquals(catalog.getCurrentPlans().length, 1); assertEquals(catalog.getCurrentPlans().size(), 1);


final Entitlement baseEntitlement = createEntitlement("foo-monthly", true); final Entitlement baseEntitlement = createEntitlement("foo-monthly", true);


Expand All @@ -111,7 +111,7 @@ public void testBasic() throws Exception {
final SimplePlanDescriptor desc2 = new DefaultSimplePlanDescriptor("superfoo-monthly", "SuperFoo", ProductCategory.BASE, account.getCurrency(), new BigDecimal("20.00"), BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of()); final SimplePlanDescriptor desc2 = new DefaultSimplePlanDescriptor("superfoo-monthly", "SuperFoo", ProductCategory.BASE, account.getCurrency(), new BigDecimal("20.00"), BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of());
catalogUserApi.addSimplePlan(desc2, init, testCallContext); catalogUserApi.addSimplePlan(desc2, init, testCallContext);
catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
assertEquals(catalog.getCurrentPlans().length, 2); assertEquals(catalog.getCurrentPlans().size(), 2);


// Change Plan to the newly added Plan and verify correct default rules behavior (IMMEDIATE change) // Change Plan to the newly added Plan and verify correct default rules behavior (IMMEDIATE change)
busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT); busHandler.pushExpectedEvents(NextEvent.CHANGE, NextEvent.INVOICE, NextEvent.INVOICE_PAYMENT, NextEvent.PAYMENT);
Expand All @@ -131,26 +131,26 @@ public void testWithMultiplePlansForOneProduct() throws CatalogApiException, Ent
final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("xxx-monthly", "XXX", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of()); final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("xxx-monthly", "XXX", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of());
catalogUserApi.addSimplePlan(desc1, init, testCallContext); catalogUserApi.addSimplePlan(desc1, init, testCallContext);
StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
assertEquals(catalog.getCurrentProducts().length, 1); assertEquals(catalog.getCurrentProducts().size(), 1);
assertEquals(catalog.getCurrentPlans().length, 1); assertEquals(catalog.getCurrentPlans().size(), 1);


final Entitlement baseEntitlement1 = createEntitlement("xxx-monthly", true); final Entitlement baseEntitlement1 = createEntitlement("xxx-monthly", true);


// Add a second plan for same product but with a 14 days trial // Add a second plan for same product but with a 14 days trial
final SimplePlanDescriptor desc2 = new DefaultSimplePlanDescriptor("xxx-14-monthly", "XXX", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 14, TimeUnit.DAYS, ImmutableList.<String>of()); final SimplePlanDescriptor desc2 = new DefaultSimplePlanDescriptor("xxx-14-monthly", "XXX", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 14, TimeUnit.DAYS, ImmutableList.<String>of());
catalogUserApi.addSimplePlan(desc2, init, testCallContext); catalogUserApi.addSimplePlan(desc2, init, testCallContext);
catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
assertEquals(catalog.getCurrentProducts().length, 1); assertEquals(catalog.getCurrentProducts().size(), 1);
assertEquals(catalog.getCurrentPlans().length, 2); assertEquals(catalog.getCurrentPlans().size(), 2);


final Entitlement baseEntitlement2 = createEntitlement("xxx-14-monthly", false); final Entitlement baseEntitlement2 = createEntitlement("xxx-14-monthly", false);


// Add a second plan for same product but with a 30 days trial // Add a second plan for same product but with a 30 days trial
final SimplePlanDescriptor desc3 = new DefaultSimplePlanDescriptor("xxx-30-monthly", "XXX", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 30, TimeUnit.DAYS, ImmutableList.<String>of()); final SimplePlanDescriptor desc3 = new DefaultSimplePlanDescriptor("xxx-30-monthly", "XXX", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 30, TimeUnit.DAYS, ImmutableList.<String>of());
catalogUserApi.addSimplePlan(desc3, init, testCallContext); catalogUserApi.addSimplePlan(desc3, init, testCallContext);
catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
assertEquals(catalog.getCurrentProducts().length, 1); assertEquals(catalog.getCurrentProducts().size(), 1);
assertEquals(catalog.getCurrentPlans().length, 3); assertEquals(catalog.getCurrentPlans().size(), 3);


final Entitlement baseEntitlement3 = createEntitlement("xxx-30-monthly", false); final Entitlement baseEntitlement3 = createEntitlement("xxx-30-monthly", false);


Expand All @@ -172,12 +172,12 @@ public void testError_CAT_MULTIPLE_MATCHING_PLANS_FOR_PRICELIST() throws Excepti
final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("zoe-monthly", "Zoe", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of()); final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("zoe-monthly", "Zoe", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of());
catalogUserApi.addSimplePlan(desc1, init, testCallContext); catalogUserApi.addSimplePlan(desc1, init, testCallContext);
StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
assertEquals(catalog.getCurrentPlans().length, 1); assertEquals(catalog.getCurrentPlans().size(), 1);


final SimplePlanDescriptor desc2 = new DefaultSimplePlanDescriptor("zoe-14-monthly", "Zoe", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 14, TimeUnit.DAYS, ImmutableList.<String>of()); final SimplePlanDescriptor desc2 = new DefaultSimplePlanDescriptor("zoe-14-monthly", "Zoe", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 14, TimeUnit.DAYS, ImmutableList.<String>of());
catalogUserApi.addSimplePlan(desc2, init, testCallContext); catalogUserApi.addSimplePlan(desc2, init, testCallContext);
catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
assertEquals(catalog.getCurrentPlans().length, 2); assertEquals(catalog.getCurrentPlans().size(), 2);


try { try {
final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Zoe", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Zoe", BillingPeriod.MONTHLY, PriceListSet.DEFAULT_PRICELIST_NAME, null);
Expand All @@ -195,9 +195,9 @@ public void testWithPriceOverride() throws Exception {
final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("bar-monthly", "Bar", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of()); final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("bar-monthly", "Bar", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.MONTHLY, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of());
catalogUserApi.addSimplePlan(desc1, init, testCallContext); catalogUserApi.addSimplePlan(desc1, init, testCallContext);
StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
assertEquals(catalog.getCurrentPlans().length, 1); assertEquals(catalog.getCurrentPlans().size(), 1);


final Plan plan = catalog.getCurrentPlans()[0]; final Plan plan = catalog.getCurrentPlans().iterator().next();
final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("bar-monthly", null); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("bar-monthly", null);


final List<PlanPhasePriceOverride> overrides = new ArrayList<PlanPhasePriceOverride>(); final List<PlanPhasePriceOverride> overrides = new ArrayList<PlanPhasePriceOverride>();
Expand Down Expand Up @@ -236,7 +236,7 @@ public void testWithThirtyDaysPlan() throws Exception {
final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("thirty-monthly", "Thirty", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.THIRTY_DAYS, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of()); final SimplePlanDescriptor desc1 = new DefaultSimplePlanDescriptor("thirty-monthly", "Thirty", ProductCategory.BASE, account.getCurrency(), BigDecimal.TEN, BillingPeriod.THIRTY_DAYS, 0, TimeUnit.UNLIMITED, ImmutableList.<String>of());
catalogUserApi.addSimplePlan(desc1, init, testCallContext); catalogUserApi.addSimplePlan(desc1, init, testCallContext);
StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext); StaticCatalog catalog = catalogUserApi.getCurrentCatalog("dummy", testCallContext);
assertEquals(catalog.getCurrentPlans().length, 1); assertEquals(catalog.getCurrentPlans().size(), 1);




final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("thirty-monthly", null); final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("thirty-monthly", null);
Expand Down
Expand Up @@ -41,6 +41,14 @@ public CatalogEntityCollection(final T[] entities) {
} }
} }



public CatalogEntityCollection(final Collection<T> entities) {
this.data = new TreeMap<String, T>(Ordering.<String>natural());
for (final T cur : entities) {
addEntry(cur);
}
}

// //
// Returning such entries will be log(N) // Returning such entries will be log(N)
// //
Expand Down
Expand Up @@ -29,9 +29,11 @@
import org.killbill.billing.catalog.api.CatalogApiException; import org.killbill.billing.catalog.api.CatalogApiException;
import org.killbill.billing.catalog.api.Currency; import org.killbill.billing.catalog.api.Currency;
import org.killbill.billing.catalog.api.PhaseType; import org.killbill.billing.catalog.api.PhaseType;
import org.killbill.billing.catalog.api.Plan;
import org.killbill.billing.catalog.api.PlanAlignmentChange; import org.killbill.billing.catalog.api.PlanAlignmentChange;
import org.killbill.billing.catalog.api.PlanAlignmentCreate; import org.killbill.billing.catalog.api.PlanAlignmentCreate;
import org.killbill.billing.catalog.api.PriceListSet; import org.killbill.billing.catalog.api.PriceListSet;
import org.killbill.billing.catalog.api.Product;
import org.killbill.billing.catalog.api.ProductCategory; import org.killbill.billing.catalog.api.ProductCategory;
import org.killbill.billing.catalog.api.SimplePlanDescriptor; import org.killbill.billing.catalog.api.SimplePlanDescriptor;
import org.killbill.billing.catalog.api.TimeUnit; import org.killbill.billing.catalog.api.TimeUnit;
Expand All @@ -44,6 +46,8 @@
import org.killbill.billing.catalog.rules.DefaultPlanRules; import org.killbill.billing.catalog.rules.DefaultPlanRules;
import org.killbill.xmlloader.XMLWriter; import org.killbill.xmlloader.XMLWriter;


import com.google.common.collect.ImmutableList;

public class CatalogUpdater { public class CatalogUpdater {


private static URI DUMMY_URI; private static URI DUMMY_URI;
Expand All @@ -53,7 +57,9 @@ public class CatalogUpdater {
DUMMY_URI = new URI("dummy"); DUMMY_URI = new URI("dummy");
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
} }
}; }

;


private final DefaultMutableStaticCatalog catalog; private final DefaultMutableStaticCatalog catalog;


Expand All @@ -68,8 +74,8 @@ public CatalogUpdater(final String catalogName, final BillingMode billingMode, f
.setCatalogName(catalogName) .setCatalogName(catalogName)
.setEffectiveDate(effectiveDate.toDate()) .setEffectiveDate(effectiveDate.toDate())
.setRecurringBillingMode(billingMode) .setRecurringBillingMode(billingMode)
.setProducts(new DefaultProduct[0]) .setProducts(ImmutableList.<Product>of())
.setPlans(new DefaultPlan[0]) .setPlans(ImmutableList.<Plan>of())
.setPriceLists(new DefaultPriceListSet(defaultPriceList, new DefaultPriceList[0])) .setPriceLists(new DefaultPriceListSet(defaultPriceList, new DefaultPriceList[0]))
.setPlanRules(getSaneDefaultPlanRules(defaultPriceList)); .setPlanRules(getSaneDefaultPlanRules(defaultPriceList));
if (currencies != null && currencies.length > 0) { if (currencies != null && currencies.length > 0) {
Expand All @@ -92,20 +98,19 @@ public String getCatalogXML() {
} }
} }



public void addSimplePlanDescriptor(final SimplePlanDescriptor desc) throws CatalogApiException { public void addSimplePlanDescriptor(final SimplePlanDescriptor desc) throws CatalogApiException {


// We need at least a planId // We need at least a planId
if (desc == null || desc.getPlanId() == null) { if (desc == null || desc.getPlanId() == null) {
throw new CatalogApiException(ErrorCode.CAT_INVALID_SIMPLE_PLAN_DESCRIPTOR, desc); throw new CatalogApiException(ErrorCode.CAT_INVALID_SIMPLE_PLAN_DESCRIPTOR, desc);
} }


DefaultPlan plan = getExistingPlan(desc.getPlanId()); DefaultPlan plan = (DefaultPlan) getExistingPlan(desc.getPlanId());
if (plan == null && desc.getProductName() == null) { if (plan == null && desc.getProductName() == null) {
throw new CatalogApiException(ErrorCode.CAT_INVALID_SIMPLE_PLAN_DESCRIPTOR, desc); throw new CatalogApiException(ErrorCode.CAT_INVALID_SIMPLE_PLAN_DESCRIPTOR, desc);
} }


DefaultProduct product = plan != null ? (DefaultProduct) plan.getProduct() : getExistingProduct(desc.getProductName()); DefaultProduct product = plan != null ? (DefaultProduct) plan.getProduct() : (DefaultProduct) getExistingProduct(desc.getProductName());
if (product == null) { if (product == null) {
product = new DefaultProduct(); product = new DefaultProduct();
product.setName(desc.getProductName()); product.setName(desc.getProductName());
Expand Down Expand Up @@ -171,9 +176,9 @@ public void addSimplePlanDescriptor(final SimplePlanDescriptor desc) throws Cata


if (desc.getProductCategory() == ProductCategory.ADD_ON) { if (desc.getProductCategory() == ProductCategory.ADD_ON) {
for (final String bp : desc.getAvailableBaseProducts()) { for (final String bp : desc.getAvailableBaseProducts()) {
final DefaultProduct targetBasePlan = getExistingProduct(bp); final Product targetBasePlan = getExistingProduct(bp);
boolean found = false; boolean found = false;
for (DefaultProduct cur : targetBasePlan.getAvailable()) { for (Product cur : targetBasePlan.getAvailable()) {
if (cur.getName().equals(product.getName())) { if (cur.getName().equals(product.getName())) {
found = true; found = true;
break; break;
Expand Down Expand Up @@ -278,17 +283,17 @@ private void validateNewPlanDescriptor(final SimplePlanDescriptor desc) throws C
} }
} }


private DefaultProduct getExistingProduct(final String productName) { private Product getExistingProduct(final String productName) {
for (final DefaultProduct input : catalog.getCurrentProducts()) { for (final Product input : catalog.getCurrentProducts()) {
if (input.getName().equals(productName)) { if (input.getName().equals(productName)) {
return input; return input;
} }
} }
return null; return null;
} }


private DefaultPlan getExistingPlan(final String planName) { private Plan getExistingPlan(final String planName) {
for (final DefaultPlan input : catalog.getCurrentPlans()) { for (final Plan input : catalog.getCurrentPlans()) {
if (input.getName().equals(planName)) { if (input.getName().equals(planName)) {
return input; return input;
} }
Expand Down
Expand Up @@ -61,35 +61,16 @@ public void addCurrency(final Currency currency) throws CatalogApiException {


@Override @Override
public void addProduct(final Product product) throws CatalogApiException { public void addProduct(final Product product) throws CatalogApiException {
final Product[] newEntries = allocateNewEntries(getCurrentProducts(), product); getCatalogEntityCollectionProduct().add(product);
setProducts((DefaultProduct[]) newEntries);
} }


@Override @Override
public void addPlan(final Plan plan) throws CatalogApiException { public void addPlan(final Plan plan) throws CatalogApiException {
final Plan[] newEntries = allocateNewEntries(getCurrentPlans(), plan);
setPlans((DefaultPlan[]) newEntries);


final DefaultPriceList priceList = getPriceLists().findPriceListFrom(plan.getPriceListName()); getCatalogEntityCollectionPlan().add(plan);
final List<DefaultPlan> newPriceListPlan = new ArrayList<DefaultPlan>();
for (Plan input : newEntries) {
if (plan.getName().equals(input.getName())) {
newPriceListPlan.add((DefaultPlan) plan);
continue;
}
if (priceList.getPlans() != null) {
for (final Plan priceListPlan : priceList.getPlans()) {
if (priceListPlan.getName().equals(input.getName())) {
newPriceListPlan.add((DefaultPlan) priceListPlan);
break;
}
}
}
}


final Plan[] newPriceListEntries = new DefaultPlan[newPriceListPlan.size()]; final DefaultPriceList priceList = getPriceLists().findPriceListFrom(plan.getPriceListName());
final Plan[] bar = newPriceListPlan.toArray(newPriceListEntries); priceList.getCatalogEntityCollectionPlan().add(plan);
priceList.setPlans((DefaultPlan[]) bar);
} }


@Override @Override
Expand All @@ -104,9 +85,8 @@ public void addRecurringPriceToPlan(final DefaultInternationalPrice currentPrice
currentPrices.setPrices((DefaultPrice[]) newEntries); currentPrices.setPrices((DefaultPrice[]) newEntries);
} }


public void addProductAvailableAO(final DefaultProduct targetBasePlan, final DefaultProduct aoProduct) throws CatalogApiException { public void addProductAvailableAO(final Product targetBasePlan, final DefaultProduct aoProduct) throws CatalogApiException {
final Product[] newEntries = allocateNewEntries(targetBasePlan.getAvailable(), aoProduct); ((DefaultProduct) targetBasePlan).getCatalogEntityCollectionAvailable().add(aoProduct);
targetBasePlan.setAvailable((DefaultProduct[]) newEntries);
} }


private <T> T[] allocateNewEntries(final T[] existingEntries, final T newEntry) throws CatalogApiException { private <T> T[] allocateNewEntries(final T[] existingEntries, final T newEntry) throws CatalogApiException {
Expand Down
Expand Up @@ -218,8 +218,8 @@ public DefaultPlan setFinalPhase(final DefaultPlanPhase finalPhase) {
return this; return this;
} }


public DefaultPlan setProduct(final DefaultProduct product) { public DefaultPlan setProduct(final Product product) {
this.product = product; this.product = (DefaultProduct) product;
return this; return this;
} }


Expand Down
Expand Up @@ -17,6 +17,7 @@
package org.killbill.billing.catalog; package org.killbill.billing.catalog;


import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;


import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
Expand All @@ -43,10 +44,11 @@ public class DefaultPriceList extends ValidatingConfig<StandaloneCatalog> implem


@XmlElementWrapper(name = "plans", required = false) @XmlElementWrapper(name = "plans", required = false)
@XmlIDREF @XmlIDREF
@XmlElement(name = "plan", required = false) @XmlElement(type=DefaultPlan.class, name = "plan", required = false)
private CatalogEntityCollection<DefaultPlan> plans; private CatalogEntityCollection<Plan> plans;


public DefaultPriceList() { public DefaultPriceList() {
this.plans = new CatalogEntityCollection();
} }


public DefaultPriceList(final DefaultPlan[] plans, final String name) { public DefaultPriceList(final DefaultPlan[] plans, final String name) {
Expand All @@ -55,10 +57,14 @@ public DefaultPriceList(final DefaultPlan[] plans, final String name) {
} }


@Override @Override
public Plan[] getPlans() { public Collection<Plan> getPlans() {
return (Plan[]) plans.toArray(new DefaultPlan[plans.size()]); return plans;
} }



public CatalogEntityCollection<Plan> getCatalogEntityCollectionPlan() {
return plans;
}
/* (non-Javadoc) /* (non-Javadoc)
* @see org.killbill.billing.catalog.IPriceList#getName() * @see org.killbill.billing.catalog.IPriceList#getName()
*/ */
Expand All @@ -71,18 +77,18 @@ public String getName() {
* @see org.killbill.billing.catalog.IPriceList#findPlan(org.killbill.billing.catalog.api.IProduct, org.killbill.billing.catalog.api.BillingPeriod) * @see org.killbill.billing.catalog.IPriceList#findPlan(org.killbill.billing.catalog.api.IProduct, org.killbill.billing.catalog.api.BillingPeriod)
*/ */
@Override @Override
public DefaultPlan[] findPlans(final Product product, final BillingPeriod period) { public Collection<Plan> findPlans(final Product product, final BillingPeriod period) {
final List<DefaultPlan> result = new ArrayList<DefaultPlan>(plans.size()); final List<Plan> result = new ArrayList<Plan>(plans.size());
for (final Plan cur : getPlans()) { for (final Plan cur : getPlans()) {
if (cur.getProduct().equals(product) && if (cur.getProduct().equals(product) &&
(cur.getRecurringBillingPeriod() != null && cur.getRecurringBillingPeriod().equals(period))) { (cur.getRecurringBillingPeriod() != null && cur.getRecurringBillingPeriod().equals(period))) {
result.add((DefaultPlan) cur); result.add(cur);
} }
} }
return result.toArray(new DefaultPlan[result.size()]); return result;
} }


public DefaultPlan findPlan(final String planName) { public Plan findPlan(final String planName) {
return plans.findByName(planName); return plans.findByName(planName);
} }


Expand All @@ -96,7 +102,7 @@ public DefaultPriceList setName(final String name) {
return this; return this;
} }


public DefaultPriceList setPlans(final DefaultPlan[] plans) { public DefaultPriceList setPlans(final Collection<Plan> plans) {
this.plans = new CatalogEntityCollection(plans); this.plans = new CatalogEntityCollection(plans);
return this; return this;
} }
Expand Down
Expand Up @@ -21,11 +21,13 @@
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;


import org.killbill.billing.ErrorCode; import org.killbill.billing.ErrorCode;
import org.killbill.billing.catalog.api.BillingPeriod; import org.killbill.billing.catalog.api.BillingPeriod;
import org.killbill.billing.catalog.api.CatalogApiException; import org.killbill.billing.catalog.api.CatalogApiException;
import org.killbill.billing.catalog.api.Plan;
import org.killbill.billing.catalog.api.PriceList; import org.killbill.billing.catalog.api.PriceList;
import org.killbill.billing.catalog.api.PriceListSet; import org.killbill.billing.catalog.api.PriceListSet;
import org.killbill.billing.catalog.api.Product; import org.killbill.billing.catalog.api.Product;
Expand All @@ -52,21 +54,21 @@ public DefaultPriceListSet(final DefaultPriceList defaultPricelist, final Defaul
this.childPriceLists = childPriceLists != null ? childPriceLists : new DefaultPriceList[0]; this.childPriceLists = childPriceLists != null ? childPriceLists : new DefaultPriceList[0];
} }


public DefaultPlan getPlanFrom(final Product product, final BillingPeriod period, final String priceListName) throws CatalogApiException { public Plan getPlanFrom(final Product product, final BillingPeriod period, final String priceListName) throws CatalogApiException {


DefaultPlan[] plans = null; Collection<Plan> plans = null;
final DefaultPriceList pl = findPriceListFrom(priceListName); final DefaultPriceList pl = findPriceListFrom(priceListName);
if (pl != null) { if (pl != null) {
plans = pl.findPlans(product, period); plans = pl.findPlans(product, period);
} }
if (plans.length == 0) { if (plans.size() == 0) {
plans = defaultPricelist.findPlans(product, period); plans = defaultPricelist.findPlans(product, period);
} }
switch(plans.length) { switch(plans.size()) {
case 0: case 0:
return null; return null;
case 1: case 1:
return plans[0]; return plans.iterator().next();
default: default:
throw new CatalogApiException(ErrorCode.CAT_MULTIPLE_MATCHING_PLANS_FOR_PRICELIST, throw new CatalogApiException(ErrorCode.CAT_MULTIPLE_MATCHING_PLANS_FOR_PRICELIST,
priceListName, product.getName(), period); priceListName, product.getName(), period);
Expand Down

1 comment on commit f29457a

@pierre
Copy link
Member

@pierre pierre commented on f29457a Oct 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.