KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > SingletonComponent


1 /*****************************************************************************
2  * Copyright (C) Codehaus.org. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Feb 28, 2005
10  *
11  * Author Ben Yu
12  * ZBS
13  */

14 package jfun.yan;
15
16 /**
17  * Codehaus.org.
18  *
19  * @author Ben Yu
20  *
21  */

22 final class SingletonComponent
23 extends DelegatingComponent{
24   private transient Object JavaDoc v = null;
25   private transient boolean cached = false;
26   public synchronized Object JavaDoc create(Dependency a) {
27     if(cached) return v;
28     this.v = super.create(a);
29     this.cached = true;
30     return v;
31   }
32   public synchronized Class JavaDoc verify(Dependency ac) {
33     if(!cached){
34       return super.verify(ac);
35     }
36     else{
37       return getCachedType();
38     }
39   }
40   public synchronized Class JavaDoc getType(){
41     if(v!=null)
42       return v.getClass();
43     else{
44       return super.getType();
45     }
46   }
47   public synchronized boolean isConcrete(){
48     return cached;
49   }
50   private Class JavaDoc getCachedType(){
51     return v==null?void.class:v.getClass();
52   }
53   SingletonComponent(final Component cc) {
54     super(cc);
55   }
56   public Component singleton(){return this;}
57   public boolean isSingleton(){
58     return true;
59   }
60 }
61
Popular Tags