我创建了ArrayList的扩展名NullIgnoringArrayList,因为我的应用程序可能会不时添加null。我知道还有其他多种方法可以处理此问题,例如在插入ArrayList之前先检查是否为null或过滤掉null。但是我以这种方式做到了,一切都很好,但是现在我不知道该如何进行。

我应该实现ListIterator,因为我的测试需要此方法。我在ArrayList的实现中达到了顶峰,但是ListItr是使用ArrayList的私有字段的私有类,而NullIgnoringArrayList中没有私有字段。

公平地说,我可能设计过度,也许我应该放弃它。另一方面,一个人可能学到一两个东西。

NullIgnoringArrayList:

public class NullIgnoringArrayList<T> extends ArrayList<T> {

    @Override
    public boolean add(T element) {
        return !isNull(element) && super.add(element);
    }

    @Override
    public void add(int index, T element) {
        if (isNull(element)) {
            return;
        }
        super.add(index, element);
    }

    @Override
    public boolean addAll(Collection c) {
        return !isNull(c) && super.addAll(c);
    }

    @Override
    public boolean addAll(int index, Collection c) {
        return !isNull(c) && super.addAll(index, c);
    }

    @Override
    public ListIterator listIterator() {
        throw new NotImplementedException();
    }

    @Override
    public ListIterator listIterator(int index) {
        throw new NotImplementedException();
    }
}


SimpleListIterator:

public class SimpleListIterator<T> implements ListIterator {
    @Override
    public boolean hasNext() {
        return false;
    }

    @Override
    public Object next() {
        return null;
    }

    @Override
    public boolean hasPrevious() {
        return false;
    }

    @Override
    public Object previous() {
        return null;
    }

    @Override
    public int nextIndex() {
        return 0;
    }

    @Override
    public int previousIndex() {
        return 0;
    }

    @Override
    public void remove() {

    }

    @Override
    public void set(Object o) {

    }

    @Override
    public void add(Object o) {

    }
}

最佳答案

只需将方法调用委托给父级的ListIterator<T>即可:

public class NullIgnoringArrayList<T> extends ArrayList<T> {

    ...

    @Override
    public ListIterator<T> listIterator() {
        return new SimpleListIterator<>(super.listIterator());
    }

    @Override
    public ListIterator<T> listIterator(int index) {
        return new SimpleListIterator<>(super.listIterator(index));
    }
}

public class SimpleListIterator<T> implements ListIterator<T> {

    private final ListIterator<T> underlying;

    public SimpleListIterator(ListIterator<T> underlying) {
        this.underlying = underlying;
    }

    @Override public boolean hasNext() {return underlying.hasNext();}
    @Override public T next() { return underlying.next(); }
    @Override public boolean hasPrevious() {return underlying.hasPrevious();}
    @Override public T previous() {return underlying.previous();}
    @Override public int nextIndex() {return underlying.nextIndex();}
    @Override public int previousIndex() {return underlying.previousIndex();}
    @Override public void remove() { underlying.remove();}

    @Override
    public void set(T o) {
        if (isNull(o)) {return;}
        underlying.set(o);
    }

    @Override
    public void add(T o) {
        if (isNull(o)) {return;}
        underlying.add(o);
    }
}

08-04 21:23