
org.broadleafcommerce.common.util.BLCCollectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of broadleaf-common Show documentation
Show all versions of broadleaf-common Show documentation
A collection of classes shared by broadleaf profile, cms, admin, and core.
/*
* #%L
* BroadleafCommerce Common Libraries
* %%
* Copyright (C) 2009 - 2013 Broadleaf Commerce
* %%
* Licensed 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.
* #L%
*/
package org.broadleafcommerce.common.util;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.springframework.util.ClassUtils;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* Convenience methods for interacting with collections.
*
* @author Andre Azzolini (apazzolini)
*/
public class BLCCollectionUtils {
/**
* Delegates to {@link CollectionUtils#collect(Collection, Transformer)}, but performs the necessary type coercion
* to allow the returned collection to be correctly casted based on the TypedTransformer.
*
* @param inputCollection
* @param transformer
* @return the typed, collected Collection
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Collection collect(Collection inputCollection, TypedTransformer transformer) {
return CollectionUtils.collect(inputCollection, transformer);
}
/**
* The same as {@link #collect(Collection, TypedTransformer)} but returns an ArrayList
* @param inputCollection
* @param transformer
* @return
*/
@SuppressWarnings("rawtypes")
public static List collectList(Collection inputCollection, TypedTransformer transformer) {
List returnList = new ArrayList();
for (Object obj : inputCollection) {
T transformed = transformer.transform(obj);
returnList.add(transformed);
}
return returnList;
}
/**
* The same as {@link #collect(Collection, TypedTransformer)} but returns an array
* @param inputCollection
* @param transformer
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static T[] collectArray(Collection inputCollection, TypedTransformer transformer, Class clazz) {
T[] returnArray = (T[]) Array.newInstance(clazz, inputCollection.size());
int i = 0;
for (Object obj : inputCollection) {
T transformed = transformer.transform(obj);
returnArray[i++] = transformed;
}
return returnArray;
}
/**
* Delegates to {@link CollectionUtils#select(Collection, org.apache.commons.collections.Predicate)}, but will
* force the return type to be a List.
*
* @param inputCollection
* @param predicate
* @return
*/
public static List selectList(Collection inputCollection, TypedPredicate predicate) {
ArrayList answer = new ArrayList(inputCollection.size());
CollectionUtils.select(inputCollection, predicate, answer);
return answer;
}
/**
* It is common to want to make sure that a collection you receive is not null. Instead, we'd rather have
* an empty list.
*
* @param list
* @return the passed in list if not null, otherwise a new ArrayList of the same type
*/
public static List createIfNull(List list) {
return (list == null) ? new ArrayList() : list;
}
/**
* Create a collection proxy that will perform some piece of work whenever modification methods are called on the
* proxy. This includes the add, allAll, remove, removeAll, clear methods. Additionally, calling remove on an iterator
* created from this collection is also covered.
*
* @param work the work to perform on collection modification
* @param original the original collection to make change aware
* @param the collection type (e.g. List, Set, etc...)
* @return the proxied collection
*/
public static T createChangeAwareCollection(final WorkOnChange work, final Collection original) {
T proxy = (T) Proxy.newProxyInstance(BLCCollectionUtils.class.getClassLoader(), ClassUtils.getAllInterfacesForClass(original.getClass()), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().startsWith("add") || method.getName().startsWith("remove") || method.getName()
.startsWith("clear")) {
work.doWork(original);
}
if (method.getName().equals("iterator")) {
final Iterator itr = (Iterator) method.invoke(original, args);
Iterator proxyItr = (Iterator) Proxy.newProxyInstance(getClass().getClassLoader(), ClassUtils.getAllInterfacesForClass(itr.getClass()), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("remove")) {
work.doWork(original);
}
return method.invoke(itr, args);
}
});
return proxyItr;
}
return method.invoke(original, args);
}
});
return proxy;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy