Guava ForwardingList使用示例 [英] Guava ForwardingList usage example

查看:617
本文介绍了Guava ForwardingList使用示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找解释Guava ForwardingList类的示例代码。基本上我实现一个自定义的ArrayList类,将用于解决这个要求在我先前的SO问题。我从来没有使用过Google集合。但通过查看 ForwardingList的JavaDoc ,我认为我可以通过子类ForwardingList实现我的自定义类。

解决方案

ForwardingList (其中 扩展了ForwardingCollection 扩展ForwardingObject )实现了装饰器模式。 / p>

要使用,您只需执行两项操作:




  • @Override delegate()返回方法转发到的支持委托实例

  • @Override 任何列表方法您需要/需要修饰



装饰模式允许您使用组合而不是继承( Effective Java第2版,Favor组成继承)和来自Guava的 ForwardingList 提供了一个方便的模板编写自己的列表实现,为您提供所有的管道机制。



请注意,如果您计划装饰一个 ArrayList ,你可能希望你的 ForwardingList 子类也实现 RandomAccess 。 p>




示例: ListWithDefault



这是一个 ForwardingList 的一个(不完整的)示例,用代替 null 给定默认值。

  import java.util。*; 
import com.google.common.collect。*;

public class ListWithDefault< E>扩展ForwardingList< E> {
final E defaultValue;
final List< E>代表;

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);
}
@覆盖公共迭代器< E> iterator(){
final Iterator< E> iter = super.iterator();
return new ForwardingIterator< E>(){
@覆盖保护迭代器< E> delegate(){
return iter;
}
@覆盖public E next(){
E v = super.next();
return(v == null?defaultValue:v);
}
};
}
}

然后我们可以测试如下:

  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

系统。 out.println(names);
// [Alice,null,Bob,Carol,null]
}

请注意,这是一个不完整的实施。 toString()方法仍然返回委托的 toString(),它不知道默认值。还有一些其他方法必须 @Override 以及更完整的实现。


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 mentioned in my earlier SO question. I never used Google collection before. But by just looking at the JavaDoc of ForwardingList, I think I can implement my custom class by sub classing ForwardingList.

解决方案

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 decorate

The 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.


Example: 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.

这篇关于Guava ForwardingList使用示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆