KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > operation > LocalContext


1 package org.jicengine.operation;
2
3 import java.util.Map JavaDoc;
4 import java.util.HashMap JavaDoc;
5
6 /**
7  * <p>
8  * local context is a context inside a larger context (parent context).
9  * objects in the local context override objects of the parent context.
10  * if an object is not found from the local context, the parent context
11  * is searched.
12  * </p>
13  *
14  * <p>
15  * new objects are added only to the local context.
16  * </p>
17  * <p>
18  * the local context is represented by another Local-instance. this way
19  * its implementation can be changed.
20  * </p>
21  * <p>
22  * Copyright (C) 2004 Timo Laitinen
23  * </p>
24  * @author Timo Laitinen
25  * @created 2004-09-20
26  * @since JICE-0.10
27  */

28
29 public class LocalContext extends AbstractContext {
30
31     private Context local;
32     private Context parent;
33
34     /**
35      *
36      */

37     public LocalContext(Context local, Context parent)
38     {
39         super(local + " + " + parent);
40         this.local = local;
41         this.parent = parent;
42     }
43
44     public LocalContext(String JavaDoc name, Context parent)
45     {
46         this(name, new SimpleContext(name), parent);
47     }
48
49
50     public LocalContext(String JavaDoc name, Context local, Context parent)
51     {
52         super(name);
53         this.local = local;
54         this.parent = parent;
55     }
56
57     public Context getParent()
58     {
59         return this.parent;
60     }
61
62     public Object JavaDoc getObject(String JavaDoc name) throws ObjectNotFoundException
63     {
64         if( this.local.hasObject(name) ){
65             return this.local.getObject(name);
66         }
67         else {
68             try {
69                 return getFromParent(name);
70             } catch (ObjectNotFoundException e){
71                 throw new ObjectNotFoundException(name, this);
72             }
73         }
74     }
75
76     protected Object JavaDoc getFromParent(String JavaDoc name) throws ObjectNotFoundException
77     {
78         return getParent().getObject(name);
79     }
80
81     public boolean hasObject(String JavaDoc name) {
82         return (this.local.hasObject(name)) || (hasInParent(name));
83     }
84
85     protected boolean hasInParent(String JavaDoc name)
86     {
87         return getParent().hasObject(name);
88     }
89
90     public void addObject(String JavaDoc name, Object JavaDoc object)
91     {
92         this.local.addObject(name,object);
93     }
94
95     protected void copyObjectsTo(Map JavaDoc map)
96     {
97         // first the parent
98
((AbstractContext)this.parent).copyObjectsTo(map);
99         // now the child so it may override the objects in the parent
100
((AbstractContext)this.local).copyObjectsTo(map);
101     }
102
103 }
104
Popular Tags