1. /*
  2. * Copyright 2003-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.collections.list;
  17. import java.util.Collection;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.ListIterator;
  21. import org.apache.commons.collections.Predicate;
  22. import org.apache.commons.collections.collection.PredicatedCollection;
  23. import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
  24. /**
  25. * Decorates another <code>List</code> to validate that all additions
  26. * match a specified predicate.
  27. * <p>
  28. * This list exists to provide validation for the decorated list.
  29. * It is normally created to decorate an empty list.
  30. * If an object cannot be added to the list, an IllegalArgumentException is thrown.
  31. * <p>
  32. * One usage would be to ensure that no null entries are added to the list.
  33. * <pre>List list = PredicatedList.decorate(new ArrayList(), NotNullPredicate.INSTANCE);</pre>
  34. * <p>
  35. * This class is Serializable from Commons Collections 3.1.
  36. *
  37. * @since Commons Collections 3.0
  38. * @version $Revision: 1.6 $ $Date: 2004/06/03 22:02:13 $
  39. *
  40. * @author Stephen Colebourne
  41. * @author Paul Jack
  42. */
  43. public class PredicatedList extends PredicatedCollection implements List {
  44. /** Serialization version */
  45. private static final long serialVersionUID = -5722039223898659102L;
  46. /**
  47. * Factory method to create a predicated (validating) list.
  48. * <p>
  49. * If there are any elements already in the list being decorated, they
  50. * are validated.
  51. *
  52. * @param list the list to decorate, must not be null
  53. * @param predicate the predicate to use for validation, must not be null
  54. * @throws IllegalArgumentException if list or predicate is null
  55. * @throws IllegalArgumentException if the list contains invalid elements
  56. */
  57. public static List decorate(List list, Predicate predicate) {
  58. return new PredicatedList(list, predicate);
  59. }
  60. //-----------------------------------------------------------------------
  61. /**
  62. * Constructor that wraps (not copies).
  63. * <p>
  64. * If there are any elements already in the list being decorated, they
  65. * are validated.
  66. *
  67. * @param list the list to decorate, must not be null
  68. * @param predicate the predicate to use for validation, must not be null
  69. * @throws IllegalArgumentException if list or predicate is null
  70. * @throws IllegalArgumentException if the list contains invalid elements
  71. */
  72. protected PredicatedList(List list, Predicate predicate) {
  73. super(list, predicate);
  74. }
  75. /**
  76. * Gets the list being decorated.
  77. *
  78. * @return the decorated list
  79. */
  80. protected List getList() {
  81. return (List) getCollection();
  82. }
  83. //-----------------------------------------------------------------------
  84. public Object get(int index) {
  85. return getList().get(index);
  86. }
  87. public int indexOf(Object object) {
  88. return getList().indexOf(object);
  89. }
  90. public int lastIndexOf(Object object) {
  91. return getList().lastIndexOf(object);
  92. }
  93. public Object remove(int index) {
  94. return getList().remove(index);
  95. }
  96. //-----------------------------------------------------------------------
  97. public void add(int index, Object object) {
  98. validate(object);
  99. getList().add(index, object);
  100. }
  101. public boolean addAll(int index, Collection coll) {
  102. for (Iterator it = coll.iterator(); it.hasNext(); ) {
  103. validate(it.next());
  104. }
  105. return getList().addAll(index, coll);
  106. }
  107. public ListIterator listIterator() {
  108. return listIterator(0);
  109. }
  110. public ListIterator listIterator(int i) {
  111. return new PredicatedListIterator(getList().listIterator(i));
  112. }
  113. public Object set(int index, Object object) {
  114. validate(object);
  115. return getList().set(index, object);
  116. }
  117. public List subList(int fromIndex, int toIndex) {
  118. List sub = getList().subList(fromIndex, toIndex);
  119. return new PredicatedList(sub, predicate);
  120. }
  121. /**
  122. * Inner class Iterator for the PredicatedList
  123. */
  124. protected class PredicatedListIterator extends AbstractListIteratorDecorator {
  125. protected PredicatedListIterator(ListIterator iterator) {
  126. super(iterator);
  127. }
  128. public void add(Object object) {
  129. validate(object);
  130. iterator.add(object);
  131. }
  132. public void set(Object object) {
  133. validate(object);
  134. iterator.set(object);
  135. }
  136. }
  137. }