View Javadoc

1   /*
2    * $Id: AbstractFilteredCollection.java,v 1.18 2011/06/16 09:29:41 jbmeslin Exp $
3    */
4   package org.rcfaces.core.model;
5   
6   import java.util.Arrays;
7   import java.util.Collection;
8   import java.util.Collections;
9   import java.util.Iterator;
10  
11  import javax.faces.component.UIComponent;
12  import javax.faces.model.SelectItem;
13  
14  /**
15   * 
16   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
17   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
18   */
19  public abstract class AbstractFilteredCollection implements Collection,
20          IFiltredCollection, IFiltredCollection2 {
21      private static final String REVISION = "$Revision: 1.18 $";
22  
23      public static final Collection EMPTY_COLLECTION = new AbstractFilteredCollection() {
24          private static final String REVISION = "$Revision: 1.18 $";
25  
26          protected boolean accept(IFilterProperties filter, SelectItem selectItem) {
27              return false;
28          }
29      };
30  
31      public static final IFiltredCollection EMPTY_FILTERED_COLLECTION = (IFiltredCollection) EMPTY_COLLECTION;
32  
33      protected final Collection collection;
34  
35      public AbstractFilteredCollection() {
36          this(Collections.EMPTY_LIST);
37      }
38  
39      public AbstractFilteredCollection(SelectItem selecItems[]) {
40          this(Arrays.asList(selecItems));
41      }
42  
43      public AbstractFilteredCollection(Collection collection) {
44          this.collection = collection;
45      }
46  
47      /**
48       * 
49       * @param filter
50       * @param selectItem
51       * @return <code>true</code> if the selectItem is sent to the client.
52       */
53      protected abstract boolean accept(IFilterProperties filter,
54              SelectItem selectItem);
55  
56      public int size() {
57          return collection.size();
58      }
59  
60      public boolean add(Object o) {
61          return collection.add(o);
62      }
63  
64      public boolean addAll(Collection c) {
65          return collection.addAll(c);
66      }
67  
68      public void clear() {
69          collection.clear();
70      }
71  
72      public boolean contains(Object o) {
73          return collection.contains(o);
74      }
75  
76      public boolean containsAll(Collection c) {
77          return collection.containsAll(c);
78      }
79  
80      public boolean isEmpty() {
81          return collection.isEmpty();
82      }
83  
84      public Iterator iterator() {
85          return collection.iterator();
86      }
87  
88      public boolean remove(Object o) {
89          return collection.remove(o);
90      }
91  
92      public boolean removeAll(Collection c) {
93          return collection.removeAll(c);
94      }
95  
96      public boolean retainAll(Collection c) {
97          return collection.retainAll(c);
98      }
99  
100     public Object[] toArray() {
101         return collection.toArray();
102     }
103 
104     public Object[] toArray(Object[] a) {
105         return collection.toArray(a);
106     }
107 
108     public Iterator iterator(IFilterProperties filterProperties,
109             int maxNumberResult) {
110         return new FilteredIterator(filterProperties, maxNumberResult);
111     }
112 
113     public Iterator iterator(UIComponent component,
114             IFilterProperties filterProperties, int maxNumberResult) {
115         return new FilteredIterator(component, filterProperties,
116                 maxNumberResult);
117     }
118 
119     /**
120      * 
121      * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
122      * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
123      */
124     protected class FilteredIterator implements IFiltredIterator {
125         private static final String REVISION = "$Revision: 1.18 $";
126 
127         private final IFilterProperties filterProperties;
128 
129         private final UIComponent component;
130 
131         private int maxResultNumber;
132 
133         private boolean limitTested = false;
134 
135         private int currentIndex = 0;
136 
137         private int size;
138 
139         private Iterator iterator;
140 
141         private SelectItem currentSelectItem;
142 
143         public FilteredIterator(IFilterProperties filterProperties) {
144             this(null, filterProperties, NO_MAXIMUM_RESULT_NUMBER);
145         }
146 
147         public FilteredIterator(IFilterProperties filterProperties,
148                 int maxResultNumber) {
149             this(null, filterProperties, maxResultNumber);
150         }
151 
152         public FilteredIterator(UIComponent component,
153                 IFilterProperties filterProperties, int maxResultNumber) {
154 
155             this.component = component;
156             this.filterProperties = filterProperties;
157             this.maxResultNumber = maxResultNumber;
158             this.size = 0;
159 
160             iterator = collection.iterator();
161         }
162 
163         public void remove() {
164             iterator.remove();
165         }
166 
167         public boolean hasNext() {
168             if (iterator == null) {
169                 return false;
170             }
171             if (currentSelectItem != null) {
172                 return true;
173             }
174 
175             if (maxResultNumber > 0 && currentIndex >= maxResultNumber) {
176                 if (limitTested) {
177                     return false;
178                 }
179 
180                 limitTested = true;
181 
182                 // La limite est atteinte, mais il en reste peut-etre
183                 // d'autres ...
184 
185                 for (; iterator.hasNext();) {
186                     SelectItem selectItem = (SelectItem) iterator.next();
187                     if (accept(filterProperties, selectItem) == false) {
188                         continue;
189                     }
190 
191                     size++;
192                     break;
193                 }
194 
195                 return false;
196             }
197 
198             for (;;) {
199                 if (iterator.hasNext() == false) {
200                     iterator = null;
201                     return false;
202                 }
203 
204                 SelectItem selectItem = (SelectItem) iterator.next();
205                 if (accept(filterProperties, selectItem) == false) {
206                     continue;
207                 }
208 
209                 currentSelectItem = selectItem;
210                 size++;
211                 return true;
212             }
213         }
214 
215         public Object next() {
216             if (currentSelectItem == null) {
217                 throw new IllegalStateException("No more selectItems ...");
218             }
219 
220             Object old = currentSelectItem;
221             currentSelectItem = null;
222             currentIndex++;
223 
224             return old;
225         }
226 
227         public int getSize() {
228             return size;
229         }
230 
231         public void release() {
232         }
233 
234         protected UIComponent getComponent() {
235             if (component == null) {
236                 throw new NullPointerException("Component is not known !");
237             }
238 
239             return component;
240         }
241     }
242 
243 }