KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > LazyComponent2Component


1 /**
2  *
3  */

4 package jfun.yan;
5
6 import jfun.yan.util.ReflectionUtil;
7
8 final class LazyComponent2Component<T> extends Component<T> {
9   private final LazyComponent<T> lcc;
10
11   private final Object JavaDoc key;
12
13   private Class JavaDoc type;
14
15   public boolean equals(Object JavaDoc obj) {
16     if(obj instanceof LazyComponent2Component){
17       final LazyComponent2Component other = (LazyComponent2Component)obj;
18       return lcc.equals(other.lcc);
19     }
20     else return false;
21   }
22
23   public int hashCode() {
24     return lcc.hashCode();
25   }
26
27   LazyComponent2Component(LazyComponent<T> lcc, Object JavaDoc key) {
28     this.lcc = lcc;
29     this.key = key;
30   }
31
32   public T create(Dependency dependency){
33     final T r = Components.ensureComponent(eval(), key)
34     .create(dependency);
35     return r;
36   }
37
38   public boolean isConcrete(){
39     return false;
40   }
41
42   public synchronized Class JavaDoc getType(){
43     if(type==null){
44       this.type = lcc.getType();
45     }
46     return this.type;
47     //do not call eval(), since it could cause infinite recursion.
48
/*
49     if(t!=null) return t;
50     final Component real = eval();
51     if(real==null) return null;
52     return real.getType();*/

53   }
54
55   public Class JavaDoc verify(Dependency dependency){
56     final Class JavaDoc t = Components.ensureComponent(eval(), key)
57     .verify(dependency);
58     if(t!=null)
59       cacheType(t);
60     return t;
61   }
62
63   private synchronized void cacheType(Class JavaDoc<T> type){
64     if(this.type==null || !ReflectionUtil.isAssignableFrom(type, this.type)){
65       this.type = type;
66     }
67   }
68
69   public boolean isSingleton(){
70     return false;
71   }
72
73   private Component<T> eval(){
74     return lcc.eval();
75   }
76
77   public String JavaDoc toString(){
78     return lcc.toString();
79   }
80 }
Popular Tags