apache collection utils
TRANSCRIPT
CollectionUtilsLet me throw you for a loop
Null safety
if(collection!=null && !collection.isEmpty()) { doSomthing(); }
Null Safety
CollectionUtils.isEmpty(collection); CollectionUtils.isNotEmpty(collection); MapUtils.isEmpty(map); MapUtils.isNotEmpty(map);
Set operations
Set operations
Union Collection union = new ArrayList(group1); for (Object o : group1) { if(!group2.contains(o)) { union.add(o); } } "
CollectionUtils.union(group1, group2);
Set operationsIntersection
Collection<Object> intersection = new ArrayList<>(); for (Object o : group1) { if (group2.contains(o)) { intersection.add(o); } } for (Object o : group2) { if (group1.contains(o)) { intersection.add(o); } } CollectionUtils.intersection(group1, group2);
Set operationsDisjunction
Collection<Object> disjunction = new ArrayList<>(); for (Object o : group1) { if (!group2.contains(o)) { disjunction.add(o); } } for (Object o : group2) { if (!group1.contains(o)) { disjunction.add(o); } } CollectionUtils.disjunction(group1, group2);
Set operations
Subtraction
Collection subtraction = new ArrayList<>(group1); for (Object o : group1) { if(group2.contains(o)) { subtraction.remove(o); } } CollectionUtils.subtract(group1, group2);
Cardinality
Object item = new Object(); int count = 0; for (Object o : group1) { if(o.equals(item)) { ++count; } } CollectionUtils.cardinality(item, group1); CollectionUtils.getCardinalityMap(group1);
The real deal
Predicates
Transformers
Closures
Predicate
Collection<Campaign> campaigns = getCampaigns(); Iterator<Campaign> campaignIterator = campaigns.iterator(); while (campaignIterator.hasNext()) { if (campaignIterator.next().getExposureCount() > 0) { campaignIterator.remove(); } }
Predicateprivate static class Campaign { public static final Predicate<Campaign> EXPOSED = new Predicate<Campaign>() { @Override public boolean evaluate(Campaign campaign) { return campaign.getExposureCount() > 0; } }; private int exposureCount = 0; public int getExposureCount() { return exposureCount; } public void setExposureCount(int exposureCount) { this.exposureCount = exposureCount; } }
�12
Predicate
CollectionUtils.filter(campaigns, Campaign.EXPOSED); CollectionUtils.select(campaigns, Campaign.EXPOSED); CollectionUtils.selectRejected(campaigns, Campaign.EXPOSED); CollectionUtils.countMatches(campaigns, Campaign.EXPOSED); ""
PredicateUtils.notPredicate(Campaign.EXPOSED); PredicateUtils.instanceofPredicate(Campaign.class); PredicateUtils.notNullPredicate();
Transformer
Collection<Campaign.Type> campaignTypes = new ArrayList<>(campaigns.size()); for (Campaign campaign : campaigns) { campaignTypes.add(campaign.getType()); }
Transformerprivate static class Campaign { public enum Type { Sales, Service, Other } public static final Transformer<Campaign, Campaign.Type> TO_TYPE = new Transformer<Campaign, Type>() { @Override public Type transform(Campaign campaign) { return campaign.getType(); } }; private Type type; public Type getType() { return type; } public void setType(Type type) { this.type = type; } }
Transformer
CollectionUtils.collect(campaigns, Campaign.TO_TYPE);
TransformerUtils.invokerTransformer("getType"); //DO NOT USE
Closure
for (Campaign campaign : campaigns) { campaign.setExposureCount(campaign.getExposureCount() + 1); }
Closureprivate static class Campaign { public static final Closure<Campaign> INCREMENT_EXPOSURE = new Closure<Campaign>() { @Override public void execute(Campaign input) { ++input.exposureCount; } }; private int exposureCount = 0; public int getExposureCount() { return exposureCount; } public void setExposureCount(int exposureCount) { this.exposureCount = exposureCount; } }
Closure
CollectionUtils.forAllDo(campaigns, Campaign.INCREMENT_EXPOSURE); ClosureUtils.chainedClosure(Campaign.INCREMENT_EXPOSURE, Campaign.INCREMENT_EXPOSURE);
What I like about it:
Clean logic code
Testable code!!!
Reusable code
Smells like functional
Notes
Java 8 Lambda expressions gives a lot of it…
Be aware of anonymous functions