Last active
December 23, 2015 09:29
-
-
Save britter/6614535 to your computer and use it in GitHub Desktop.
Example of how an adapter collection could work in commons collections
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Container { | |
private Collection<Containment> containments = new ArrayList()<Containment>; | |
public Collection<Containment> getContainments() { | |
return containments; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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