1 16 package org.apache.commons.collections.list; 17 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.ListIterator ; 22 23 import org.apache.commons.collections.Predicate; 24 import org.apache.commons.collections.collection.PredicatedCollection; 25 import org.apache.commons.collections.iterators.AbstractListIteratorDecorator; 26 27 46 public class PredicatedList extends PredicatedCollection implements List { 47 48 49 private static final long serialVersionUID = -5722039223898659102L; 50 51 62 public static List decorate(List list, Predicate predicate) { 63 return new PredicatedList(list, predicate); 64 } 65 66 78 protected PredicatedList(List list, Predicate predicate) { 79 super(list, predicate); 80 } 81 82 87 protected List getList() { 88 return (List ) getCollection(); 89 } 90 91 public Object get(int index) { 93 return getList().get(index); 94 } 95 96 public int indexOf(Object object) { 97 return getList().indexOf(object); 98 } 99 100 public int lastIndexOf(Object object) { 101 return getList().lastIndexOf(object); 102 } 103 104 public Object remove(int index) { 105 return getList().remove(index); 106 } 107 108 public void add(int index, Object object) { 110 validate(object); 111 getList().add(index, object); 112 } 113 114 public boolean addAll(int index, Collection coll) { 115 for (Iterator it = coll.iterator(); it.hasNext(); ) { 116 validate(it.next()); 117 } 118 return getList().addAll(index, coll); 119 } 120 121 public ListIterator listIterator() { 122 return listIterator(0); 123 } 124 125 public ListIterator listIterator(int i) { 126 return new PredicatedListIterator(getList().listIterator(i)); 127 } 128 129 public Object set(int index, Object object) { 130 validate(object); 131 return getList().set(index, object); 132 } 133 134 public List subList(int fromIndex, int toIndex) { 135 List sub = getList().subList(fromIndex, toIndex); 136 return new PredicatedList(sub, predicate); 137 } 138 139 142 protected class PredicatedListIterator extends AbstractListIteratorDecorator { 143 144 protected PredicatedListIterator(ListIterator iterator) { 145 super(iterator); 146 } 147 148 public void add(Object object) { 149 validate(object); 150 iterator.add(object); 151 } 152 153 public void set(Object object) { 154 validate(object); 155 iterator.set(object); 156 } 157 } 158 159 } 160 | Popular Tags |