001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.collections.list; 018 019 import java.util.Collection; 020 import java.util.Iterator; 021 import java.util.List; 022 import java.util.ListIterator; 023 024 import org.apache.commons.collections.Predicate; 025 import org.apache.commons.collections.collection.PredicatedCollection; 026 import org.apache.commons.collections.iterators.AbstractListIteratorDecorator; 027 028 /** 029 * Decorates another <code>List</code> to validate that all additions 030 * match a specified predicate. 031 * <p> 032 * This list exists to provide validation for the decorated list. 033 * It is normally created to decorate an empty list. 034 * If an object cannot be added to the list, an IllegalArgumentException is thrown. 035 * <p> 036 * One usage would be to ensure that no null entries are added to the list. 037 * <pre>List list = PredicatedList.decorate(new ArrayList(), NotNullPredicate.INSTANCE);</pre> 038 * <p> 039 * This class is Serializable from Commons Collections 3.1. 040 * 041 * @since Commons Collections 3.0 042 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 043 * 044 * @author Stephen Colebourne 045 * @author Paul Jack 046 */ 047 public class PredicatedList extends PredicatedCollection implements List { 048 049 /** Serialization version */ 050 private static final long serialVersionUID = -5722039223898659102L; 051 052 /** 053 * Factory method to create a predicated (validating) list. 054 * <p> 055 * If there are any elements already in the list being decorated, they 056 * are validated. 057 * 058 * @param list the list to decorate, must not be null 059 * @param predicate the predicate to use for validation, must not be null 060 * @throws IllegalArgumentException if list or predicate is null 061 * @throws IllegalArgumentException if the list contains invalid elements 062 */ 063 public static List decorate(List list, Predicate predicate) { 064 return new PredicatedList(list, predicate); 065 } 066 067 //----------------------------------------------------------------------- 068 /** 069 * Constructor that wraps (not copies). 070 * <p> 071 * If there are any elements already in the list being decorated, they 072 * are validated. 073 * 074 * @param list the list to decorate, must not be null 075 * @param predicate the predicate to use for validation, must not be null 076 * @throws IllegalArgumentException if list or predicate is null 077 * @throws IllegalArgumentException if the list contains invalid elements 078 */ 079 protected PredicatedList(List list, Predicate predicate) { 080 super(list, predicate); 081 } 082 083 /** 084 * Gets the list being decorated. 085 * 086 * @return the decorated list 087 */ 088 protected List getList() { 089 return (List) getCollection(); 090 } 091 092 //----------------------------------------------------------------------- 093 public Object get(int index) { 094 return getList().get(index); 095 } 096 097 public int indexOf(Object object) { 098 return getList().indexOf(object); 099 } 100 101 public int lastIndexOf(Object object) { 102 return getList().lastIndexOf(object); 103 } 104 105 public Object remove(int index) { 106 return getList().remove(index); 107 } 108 109 //----------------------------------------------------------------------- 110 public void add(int index, Object object) { 111 validate(object); 112 getList().add(index, object); 113 } 114 115 public boolean addAll(int index, Collection coll) { 116 for (Iterator it = coll.iterator(); it.hasNext(); ) { 117 validate(it.next()); 118 } 119 return getList().addAll(index, coll); 120 } 121 122 public ListIterator listIterator() { 123 return listIterator(0); 124 } 125 126 public ListIterator listIterator(int i) { 127 return new PredicatedListIterator(getList().listIterator(i)); 128 } 129 130 public Object set(int index, Object object) { 131 validate(object); 132 return getList().set(index, object); 133 } 134 135 public List subList(int fromIndex, int toIndex) { 136 List sub = getList().subList(fromIndex, toIndex); 137 return new PredicatedList(sub, predicate); 138 } 139 140 /** 141 * Inner class Iterator for the PredicatedList 142 */ 143 protected class PredicatedListIterator extends AbstractListIteratorDecorator { 144 145 protected PredicatedListIterator(ListIterator iterator) { 146 super(iterator); 147 } 148 149 public void add(Object object) { 150 validate(object); 151 iterator.add(object); 152 } 153 154 public void set(Object object) { 155 validate(object); 156 iterator.set(object); 157 } 158 } 159 160 }