/Users/lyon/j4p/src/classUtils/pack/util/ForEach.java

1    package classUtils.pack.util; 
2     
3    import java.util.*; 
4     
5    /** 
6     * Execute the same action for each object in a collection, optionally filtering the objects by the given filter. 
7     * <p> 
8     * By default, ForEach doesn't do anything, so the typical way to employ ForEach is by subclassing anonymously 
9     * overriding the method <a href="#action(java.lang.Object)>action()</a>.  For example 
10    * <p><pre> 
11    * List list; 
12    *  ...<i>create and populate list</i>... 
13    * new ForEach(l) { 
14    *   public void action(Object obj) { 
15    *      System.out.println(obj); 
16    *    } 
17    * } 
18    * </pre> 
19    * prints all the elements of a List collection. 
20    * <p> 
21    * The default implementation of <a href="#action(java.lang.Object)>action()</a> does exactly this. 
22    
23    */ 
24   public class ForEach { 
25    
26       /** 
27        * Users can implement this interface (anonymously or via a named class) to apply the action 
28        * only to a subset of the objects in the collection. 
29        */ 
30       public interface Filter { 
31           /** 
32            * Determines if the action is to be applied to the object or not 
33            * @param obj the object to verify 
34            * @return <b>true</b> if the action must be applied to the object 
35            */ 
36           public boolean accept(Object obj); 
37       } 
38    
39       /** 
40        * A Null filter, which doesn't filter any object 
41        */ 
42       private static class NullFilter implements Filter { 
43           /** 
44            * Returns always true 
45            * @param obj the object to verify 
46            * @return <b>true</b> always 
47            */ 
48           public boolean accept(Object obj) { return true; } 
49       } 
50    
51       private Iterator i; 
52       private Filter filter; 
53       private boolean atStart=true; 
54    
55       /** 
56        * Creates a ForEach object applying to the elements of the given collection satisfying the given filter 
57        * @param c the collection on whose elements which the action will be executed 
58        * @param filter the filter to apply 
59        */ 
60       public ForEach(Collection c, Filter filter) { 
61           this(c.iterator(), filter); 
62       } 
63    
64       /** 
65        * Creates a ForEach object applying to all the elements of the given collection 
66        * @param c the collection on whose elements which the action will be executed 
67        */ 
68       public ForEach(Collection c) { 
69           this(c, null); 
70       } 
71    
72       /** 
73        * Creates a ForEach object applying to the elements of the given array satisfying the given filter 
74        * @param array the array on whose elements which the action will be executed 
75        * @param filter the filter to apply 
76        */ 
77       public ForEach(Object [] array, Filter filter) { 
78           this(Arrays.asList(array), filter); 
79       } 
80    
81       /** 
82        * Creates a ForEach object applying to all the elements of the given array 
83        * @param array the array on whose elements which the action will be executed 
84        */ 
85       public ForEach(Object [] array) { 
86           this(array, null); 
87       } 
88    
89       /** 
90        * Creates a ForEach object applying to the elements of the given iterator satisfying the given filter 
91        * @param i an iterator, on whose elements which the action will be executed 
92        * @param filter the filter to apply 
93        */ 
94       public ForEach(Iterator i, Filter filter) { 
95           if (i==null) throw new IllegalArgumentException("Collection can't be 'null'"); 
96           if (filter==null) filter=new NullFilter(); 
97           this.i=i; 
98           this.filter=filter; 
99       } 
100   
101      /** 
102       * Creates a ForEach object applying to all the elements of the given iterator 
103       * @param i an iterator, on whose elements which the action will be executed 
104       */ 
105      public ForEach(Iterator i) { 
106          this(i, null); 
107      } 
108   
109      /** 
110       * Executes the action of the elements of the collection/iterator. If a filter has been defined, 
111       * the action is executed only on the elements satisfying the filter. 
112       * 
113       */ 
114      public void execute() { 
115          if (! atStart) throw new IllegalStateException("execute() already invoked: use reInit() before reinvoking"); 
116          for(;i.hasNext();) { 
117              Object obj = i.next(); 
118              if (filter.accept(obj)) action(obj); 
119          } 
120          atStart=false; 
121      } 
122   
123      /** 
124       * Re-initializes the ForEach object on the given collection 
125       * @param Collection c the collection on whose elements which the action will be executed 
126       */ 
127      public void reInit(Collection c) { 
128          reInit(c.iterator()); 
129      } 
130   
131      /** 
132       * Re-initializes the ForEach object on the given iterator 
133       * @param Iterator i an iterator, on whose elements which the action will be executed 
134       */ 
135      public void reInit(Iterator i) { 
136          this.i=i; 
137          atStart=true; 
138      } 
139   
140   
141      /** 
142       * This method must be overridden by a subclass to define the actual action. 
143       * <p>By default, the object's toString() method is invoked and the result 
144       * printed on System.out. 
145       */ 
146      public void action(Object obj) { 
147          System.out.println(obj); 
148      } 
149   
150   
151  }