I am looking for sample code which explains Guava ForwardingList class. Basically I am implementing a custom ArrayList class which will be used to solve this requirement men
ForwardingList (which extends ForwardingCollection, which in turn extends ForwardingObject) implements the decorator pattern.
To use, you simply need to do two things:
@Override delegate()
to return the backing delegate instance that methods are forwarded to@Override
whatever List
method you want/need to decorateThe decorator pattern allows you to use composition instead of inheritance (Effective Java 2nd Edition, Favor composition over inheritance), and ForwardingList
from Guava provides a convenient template from which to write your own List
implementation, providing all the plumbing mechanism for you.
Note that if you are planning to decorate an ArrayList
, you'd probably want your ForwardingList
subclass to also implement RandomAccess.
ListWithDefault
Here's an (incomplete!) example of a ForwardingList
that substitutes null
values in the delegate with a given default value.
import java.util.*;
import com.google.common.collect.*;
public class ListWithDefault<E> extends ForwardingList<E> {
final E defaultValue;
final List<E> delegate;
ListWithDefault(List<E> delegate, E defaultValue) {
this.delegate = delegate;
this.defaultValue = defaultValue;
}
@Override protected List delegate() {
return delegate;
}
@Override public E get(int index) {
E v = super.get(index);
return (v == null ? defaultValue : v);
}
@Override public Iterator<E> iterator() {
final Iterator<E> iter = super.iterator();
return new ForwardingIterator<E>() {
@Override protected Iterator<E> delegate() {
return iter;
}
@Override public E next() {
E v = super.next();
return (v == null ? defaultValue : v);
}
};
}
}
We can then test it as follows:
public static void main(String[] args) {
List<String> names = new ListWithDefault<String>(
Arrays.asList("Alice", null, "Bob", "Carol", null),
"UNKNOWN"
);
for (String name : names) {
System.out.println(name);
}
// Alice
// UNKNOWN
// Bob
// Carol
// UNKNOWN
System.out.println(names);
// [Alice, null, Bob, Carol, null]
}
Note that this is an incomplete implementation. The toString()
method still returns the delegate's toString()
, which isn't aware of the default value. A few other methods must be @Override
as well for a more complete implementation.
You should implement delegate() method of ForwardingList to return delegate list you are using in your elements.
public class YourList<E> extends ForwardingList<E> {
private final List<E> delegate;
public YourList(List<E> list1, List<E> list2) {
delegate = new ArrayList<E>(list1);
delegate.addAll(list2);
}
@Override
public List<E> delegate() {
return delegate;
}
}