KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > SimpleDependency


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 Mar 6, 2005
10  *
11  * Author Ben Yu
12  * ZBS
13  */

14 package jfun.yan;
15
16 import jfun.yan.function.Signature;
17
18
19 /**
20  * A simple implementation of the Dependency interface. <br>
21  * It simply calls ComponentMap.getComponent(Object) and
22  * ComponentMap.getComponentOfType(Class) to resolve component
23  * parts.<br>
24  * Properties and parameters are resolved by type.
25  * <p>
26  * Codehaus.org.
27  *
28  * @author Ben Yu
29  *
30  */

31 public class SimpleDependency implements Dependency {
32   private final Object JavaDoc ckey;
33   private final ComponentMap cmap;
34   
35   /**
36    * Create a SimpleDependency object.
37    * @param ckey the component key.
38    * @param cmap the container to look into.
39    */

40   public SimpleDependency(final Object JavaDoc ckey, final ComponentMap cmap) {
41     this.ckey = ckey;
42     this.cmap = cmap;
43   }
44   private Component obtainCreator(int k, Class JavaDoc type){
45     final Component cc = cmap.getComponentOfType(type);
46     if(cc == null){
47       throw new IrresolveableArgumentException(ckey, k, type);
48     }
49     return cc;
50   }
51   private Component obtainCreator(Object JavaDoc k, Class JavaDoc type){
52     final Component cc = cmap.getComponentOfType(type);
53     if(cc == null){
54       throw new IrresolveablePropertyException(ckey, k, type);
55     }
56     return cc;
57   }
58   public ComponentMap getComponentMap(){return cmap;}
59   public Object JavaDoc getArgument(final Signature src, int i, final Class JavaDoc type){
60     return obtainCreator(i, type).create(
61         getDependencyForType(type));
62   }
63   public Class JavaDoc verifyArgument(final Signature src, int i, Class JavaDoc type){
64     final Component cc = obtainCreator(i, type);
65     final Class JavaDoc r = cc.verify(this);
66     Utils.checkType(src, ckey, i, type, r);
67     return r;
68   }
69   public Object JavaDoc getProperty(final Class JavaDoc component_type, Object JavaDoc lkey, final Class JavaDoc type){
70     return obtainCreator(lkey, type).create(getDependencyForType(type));
71   }
72   public Class JavaDoc verifyProperty(final Class JavaDoc component_type, Object JavaDoc lkey, Class JavaDoc type){
73     final Component cc = obtainCreator(lkey, type);
74     final Class JavaDoc r = cc.verify(this);
75     Utils.checkType(component_type, ckey, lkey, type, r);
76     return r;
77   }
78   
79   public Object JavaDoc getComponentKey(){
80     return ckey;
81   }
82   public Dependency getParent(){return null;}
83   private Dependency getDependencyForType(Class JavaDoc type){
84     return cmap.getDependencyOfType(type, cmap);
85   }
86   public Dependency getOriginal(){
87     return this;
88   }
89   public Dependency seal(){
90     return new ManualDependency(cmap, ckey);
91   }
92 }
93
Popular Tags