I have a List<ParentObject> where ParentObject is defined as:
public class ParentObject
{
ChildObject child1;
// and other stuff...
}
I would like to expose that List<ParentObject> to others as an unmodifiable List<ChildObject> by constructing a wrapper list:
public class WrapperList implements List<ChildObject>
{
List<ParentObject> delegateList;
public WrapperList( List<ParentObject> delegateList )
{
this.delegateList = delegateList;
}
// fill in java.util.List methods...
}
I can take this approach and make it work, but this seems like a problem which may have been solved generally. Is there an existing library which provides a list wrapper like what I am proposing? Presumably I would define a translation method telling the wrapper list how to retrieve the ChildObject from a given ParentObject.
The basic question is: is this a good approach or is there a library (Guava or Apache Commons Collections or something) which already provides something like this?
The simplest way to do this with Guava would be
(Disclosure: I contribute to Guava.)