01: package org.jicengine.operation;
02:
03: import java.util.Map;
04: import java.util.HashMap;
05:
06: /**
07: * <p>
08: * local context is a context inside a larger context (parent context).
09: * 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: super (local + " + " + parent);
39: this .local = local;
40: this .parent = parent;
41: }
42:
43: public LocalContext(String name, Context parent) {
44: this (name, new SimpleContext(name), parent);
45: }
46:
47: public LocalContext(String name, Context local, Context parent) {
48: super (name);
49: this .local = local;
50: this .parent = parent;
51: }
52:
53: public Context getParent() {
54: return this .parent;
55: }
56:
57: public Object getObject(String name) throws ObjectNotFoundException {
58: if (this .local.hasObject(name)) {
59: return this .local.getObject(name);
60: } else {
61: try {
62: return getFromParent(name);
63: } catch (ObjectNotFoundException e) {
64: throw new ObjectNotFoundException(name, this );
65: }
66: }
67: }
68:
69: protected Object getFromParent(String name)
70: throws ObjectNotFoundException {
71: return getParent().getObject(name);
72: }
73:
74: public boolean hasObject(String name) {
75: return (this .local.hasObject(name)) || (hasInParent(name));
76: }
77:
78: protected boolean hasInParent(String name) {
79: return getParent().hasObject(name);
80: }
81:
82: public void addObject(String name, Object object) {
83: this .local.addObject(name, object);
84: }
85:
86: protected void copyObjectsTo(Map map) {
87: // first the parent
88: ((AbstractContext) this .parent).copyObjectsTo(map);
89: // now the child so it may override the objects in the parent
90: ((AbstractContext) this.local).copyObjectsTo(map);
91: }
92:
93: }
|