1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
package com.modulus.qbar.core;
import java.io.Serializable;
import java.util.Map;
import java.util.Set;
/**
* Class is the very root of all the interpreter classes.
* a QBNamespace is anything that has the ability to store variables
* including QBObjects and Functions.
*
* @author jrahm
*
*/
public class QBNamespace implements Serializable{
/**
*
*/
private static final long serialVersionUID = -102581163781603724L;
private QBNamespace superNamespace;
private String name = super.toString();
// The map this namespace delegates,
// This map can be of different types
// like a HashTable.
private Map<String, QBObject> map;
/**
* Creates a new QBNamespace with the map <code>map</code>
* @param map
*/
public QBNamespace( Map<String, QBObject> map ){
this( map, null );
}
public QBNamespace( Map<String, QBObject> map, QBNamespace sup){
this.map = map;
this.superNamespace = sup;
}
/**
* Sets the object with the name <code>name</code>
* to the specific <code>QBObject obj</code>
*
* @param name the name of the variable to set
* @param obj the object to set that name equal to.
*/
public void set(String name, QBObject obj){
this.map.put(name, obj);
}
/**
* Returns the object that is referenced by the
* string <code>name</code> from this Namespace
*
* @param name the name of the object to retrieve
* @return the object referenced as <code>name</code>
*/
public QBObject get(String name){
// System.out.println("THIS: " + this);
QBObject obj = this.map.get(name);
if(obj == null && superNamespace != null)
obj = superNamespace.get(name);
if(obj == null)
throw new RuntimeException("The variable: " + name + " does not exist in the current namespace. " + this);
return obj;
}
/**
* Returns the object that is referenced by the
* string <code>name</code> from this Namespace
*
* This version of the function will not error
* if the object is not found
*
* @param name the name of the object to retrieve
* @return the object referenced as <code>name</code>
*/
public QBObject getNoError(String name){
QBObject obj = this.map.get(name);
if(obj == null && superNamespace != null)
obj = superNamespace.getNoError(name);
return obj;
}
public QBNamespace getSuper(){
return this.superNamespace;
}
public void setSuper( QBNamespace sup ){
this.superNamespace = sup;
}
public String toString2(){
StringBuffer buf = new StringBuffer();
for( String key : this.map.keySet() ){
buf.append(key + ":\t");
buf.append( this.get(key) + "\n");
}
return buf.toString();
}
public Set<String> getKeySet(){
return map.keySet();
}
public boolean hasKey( String key ){
return map.containsKey(key);
}
public QBObject hardGet(String[] args){
QBNamespace cur = this;
for(int i = 0;i < args.length - 1;i ++)
cur = cur.get(args[i]);
return cur.get(args[args.length - 1]);
}
public void hardSet(String[] args, QBObject obj){
QBNamespace cur = this;
for(int i = 0;i < args.length - 1;i ++)
cur = cur.get(args[i]);
cur.set(args[args.length - 1], obj);
}
/**
* Incorporates two namespaces and
* makes them clients to one and other,
* so that if something is added to one,
* then it is added to the other.
*
* @param other the other namespace
*/
public void incoporate( QBNamespace other ){
load( other );
// make the other one your bitch
other.map = this.map;
}
/**
* Same as incorporates, except that in this case,
* the two namespaces remain sovereign and do not
* become clients.
*
* @param other the other namespace
*/
public void load( QBNamespace other ){
for (String key : other.getKeySet() ){
this.set(key, other.get(key));
}
}
}
|