Skip to content

Commit

Permalink
catalog: CR for 3695ec7. Rework CatalogEntityCollection implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrossie committed Oct 3, 2016
1 parent 3695ec7 commit 299b8d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
Expand Up @@ -18,28 +18,24 @@
package org.killbill.billing.catalog;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map;
import java.util.TreeMap;

import org.killbill.billing.catalog.api.CatalogEntity;
import org.weakref.jmx.internal.guava.collect.Ordering;

import com.google.common.collect.Ordering;

public class CatalogEntityCollection<T extends CatalogEntity> implements Collection<T> {

private final Set<String> keys;
private final HashMap<String, T> data;
private final Map<String, T> data;

public CatalogEntityCollection() {
this.keys = new TreeSet<String>(Ordering.<String>natural());
this.data = new HashMap<String, T>();
this.data = new TreeMap<String, T>(Ordering.<String>natural());
}

public CatalogEntityCollection(final T[] entities) {
this.keys = new TreeSet<String>(Ordering.<String>natural());
this.data = new HashMap<String, T>();
this.data = new TreeMap<String, T>(Ordering.<String>natural());
for (final T cur : entities) {
addEntry(cur);
}
Expand Down Expand Up @@ -73,12 +69,11 @@ public boolean contains(final Object o) {

@Override
public Iterator iterator() {

// Build an iterator that will return ordered using natural ordering with regard to CatalogEntity#name
final Iterator<String> keyIterator = keys.iterator();
final Iterator<String> keyIterator = data.keySet().iterator();
final Iterator it = new Iterator() {

private String prevKey = null;

@Override
public boolean hasNext() {
return keyIterator.hasNext();
Expand All @@ -99,7 +94,6 @@ public void remove() {
return it;
}


@Override
public boolean add(final T t) {
addEntry(t);
Expand Down Expand Up @@ -163,7 +157,6 @@ public Object[] toArray() {
return data.values().toArray(new Object[data.size()]);
}


@Override
public boolean equals(final Object o) {
if (this == o) {
Expand All @@ -174,9 +167,7 @@ public boolean equals(final Object o) {
}

final CatalogEntityCollection<?> that = (CatalogEntityCollection<?>) o;

return data != null ? data.equals(that.data) : that.data == null;

}

@Override
Expand All @@ -185,12 +176,10 @@ public int hashCode() {
}

private void addEntry(final T entry) {
keys.add(entry.getName());
data.put(entry.getName(), entry);
}

private boolean removeEntry(final T entry) {
keys.remove(entry.getName());
return data.remove(entry.getName()) != null;
}

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

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

import javax.xml.bind.annotation.XmlAccessType;
Expand Down Expand Up @@ -56,8 +55,8 @@ public DefaultPriceList(final DefaultPlan[] plans, final String name) {
}

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

/* (non-Javadoc)
Expand All @@ -74,10 +73,10 @@ public String getName() {
@Override
public DefaultPlan[] findPlans(final Product product, final BillingPeriod period) {
final List<DefaultPlan> result = new ArrayList<DefaultPlan>(plans.size());
for (final DefaultPlan cur : getPlans()) {
for (final Plan cur : getPlans()) {
if (cur.getProduct().equals(product) &&
(cur.getRecurringBillingPeriod() != null && cur.getRecurringBillingPeriod().equals(period))) {
result.add(cur);
result.add((DefaultPlan) cur);
}
}
return result.toArray(new DefaultPlan[result.size()]);
Expand Down

1 comment on commit 299b8d7

@pierre
Copy link
Member

@pierre pierre commented on 299b8d7 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.