Skip to content

Instantly share code, notes, and snippets.

@britter
Last active December 23, 2015 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save britter/6614535 to your computer and use it in GitHub Desktop.
Save britter/6614535 to your computer and use it in GitHub Desktop.
Example of how an adapter collection could work in commons collections
/*
* Creates an adapter collection of the given collection. The returned collection is an unmodifiable
* view of {@code col}. Adapters will be created on the fly. In other words, when an element from col
* is requested, it is passed to {@code transformer} and the result is returned. The created adapters
* are not cached. If an element is requested twice, a new adapter will be created for each call.
*
* @param col the collection to create the adapter collection for
* @param transformer the tranformer that creates adapters from the elements of col
* @param E the type of the elements in col
* @param A the type of the adapters
*/
public static <A, E> Collection<A> adapterCollection(Collection<E> col, Transformer<A, E> transformer){
// what ever is necessary to do the trick...
}
public class Container {
private Collection<Containment> containments = new ArrayList()<Containment>;
public Collection<Containment> getContainments() {
return containments;
}
}
public class ContainerAdapter {
private Container container;
public ContainerAdapter(Container adaptee) {
container = adaptee;
}
public Collection<ContainmentAdapter> getContainments() {
return CollectionUtils.adapterCollection(container.getContainments, new Transformer<ContainmentAdapter, Containment>() {
@Override
public ContainmentAdapter transform(Containment containment) {
return new ContainmentAdapter(containment);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment