KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > CollectionComponent


1 package jfun.yan;
2
3 import java.util.Arrays JavaDoc;
4
5
6 import jfun.util.Misc;
7 import jfun.util.StringUtils;
8 import jfun.yan.element.ElementChecker;
9 import jfun.yan.element.ElementStore;
10
11 /**
12  * This class sequentially execute an array of components
13  * and collect the results into a ElementStore object.
14  * <p>
15  * It could be an issue to use polymorphism for array access.
16  * We'll keep it for now since it looks more elegant.
17  * When the performance needs to be enhanced, expanding the code
18  * to access array directly is always an option.
19  * </p>
20  * <p>
21  * @author Ben Yu
22  * Nov 12, 2005 2:34:45 PM
23  */

24 final class CollectionComponent<T, L> extends Component<L>{
25   private final Creator<T>[] arr;
26   private final ElementStore<T> sink;
27   private final ElementChecker checker;
28
29   CollectionComponent(Creator<T>[] ccs, ElementStore<T> sink, ElementChecker checker) {
30     this.arr = ccs;
31     this.sink = sink;
32     this.checker = checker;
33   }
34
35   public Class JavaDoc getType() {
36     return void.class;
37   }
38   public boolean isConcrete(){
39     return true;
40   }
41   public L create(Dependency dep){
42     for(int i=0; i<arr.length; i++){
43       sink.storeElement(i, arr[i].create(dep));
44     }
45     return null;
46   }
47
48   public Class JavaDoc verify(Dependency dep){
49     verifyElems(dep);
50     return getType();
51   }
52   private void verifyElems(Dependency dep){
53     for(int i=0; i<arr.length; i++){
54       checker.checkElement(i, arr[i].verify(dep));
55     }
56   }
57   public boolean equals(Object JavaDoc obj) {
58     if(obj instanceof CollectionComponent){
59       final CollectionComponent ac2 = (CollectionComponent)obj;
60       return sink.equals(ac2.sink) &&
61         Arrays.equals(arr, ac2.arr);
62     }
63     else return false;
64   }
65   public int hashCode() {
66     return Misc.getArrayHashcode(arr)*31+
67     sink.hashCode();
68   }
69   public String JavaDoc toString() {
70     return sink.toString() + StringUtils.listString("[", ",", "]", arr);
71   }
72   public boolean isSingleton(){
73     return false;
74   }
75 }
76
Popular Tags