KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > StoreBinder


1 package jfun.yan;
2
3 import java.util.Arrays JavaDoc;
4
5 import jfun.util.Misc;
6 import jfun.util.StringUtils;
7 import jfun.yan.element.ElementChecker;
8 import jfun.yan.element.ElementStore;
9
10 /**
11  * This implementation executes an array of components sequentially
12  * and collect the results into an {@link ElementStore} object created by the {@link #toStore(Object)}
13  * method.
14  * <p>
15  * @author Ben Yu
16  * Nov 12, 2005 2:13:47 PM
17  */

18 public abstract class StoreBinder<T,L> implements ComponentBinder<L,L>, ElementChecker {
19   private final Creator<T>[] steps;
20   
21   /**
22    * Create a StoreBinder object.
23    * @param steps the components to be executed sequentially.
24    */

25   public StoreBinder(Creator<T>[] steps) {
26     this.steps = steps;
27   }
28
29   public boolean equals(Object JavaDoc obj) {
30     if(obj instanceof StoreBinder){
31       final StoreBinder other = (StoreBinder)obj;
32       return Arrays.equals(steps, other.steps);
33     }
34     return false;
35   }
36
37   public int hashCode() {
38     return Misc.getArrayHashcode(steps);
39   }
40
41   public String JavaDoc toString() {
42     return StringUtils.listArray("[", ",", "]", steps);
43   }
44
45   public Verifiable verify(final Class JavaDoc type) {
46     return new Verifiable(){
47       public Class JavaDoc verify(Dependency dep) throws IrresolveableArgumentException, ParameterTypeMismatchException, AmbiguousComponentResolutionException, YanException {
48         for(int i=0; i<steps.length;i++){
49           checkElement(i, steps[i].verify(dep));
50         }
51         return type;
52       }
53     };
54   }
55
56   public Class JavaDoc bindType(Class JavaDoc type) {
57     return type;
58   }
59
60   public Creator<L> bind(L v) throws Throwable JavaDoc {
61     return new CollectionComponent<T,L>(steps, toStore(v), this);
62   }
63   /**
64    * Subclasses need to override this method to create
65    * the ElementStore object needed to collect the component instances.
66    * @param v this is the same value received from the {@link #bind(Object)}
67    * method. It is normally a collection object. Based on its type, a proper ElementStore
68    * object can be created to populate the instances into it.
69    * @return the ElementStore object.
70    */

71   public abstract ElementStore<T> toStore(L v);
72   /**
73    * Subclasses may choose to override this method
74    * to provide additional checking for the element types and indexes.
75    * @param i the index of the element.
76    * @param type the element type.
77    */

78   public void checkElement(int i, Class JavaDoc type){}
79
80   public Creator<T>[] getProducers() {
81     return steps;
82   }
83   
84 }
85
Popular Tags