01: /*
02: * PackageMap.java
03: *
04: * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.lang;
10:
11: import pnuts.lang.Package;
12: import pnuts.lang.Context;
13: import pnuts.lang.NamedValue;
14: import java.util.*;
15:
16: public class PackageMap extends AbstractMap {
17:
18: private Package pkg;
19:
20: public PackageMap(Package pkg) {
21: this .pkg = pkg;
22: }
23:
24: public Object get(Object key) {
25: String symbol = ((String) key).intern();
26: NamedValue value = pkg.lookup(symbol);
27: if (value != null) {
28: return value.get();
29: }
30: return null;
31: }
32:
33: public Object put(Object key, Object value) {
34: String symbol = ((String) key).intern();
35: NamedValue binding = pkg.lookup(symbol);
36: if (binding != null) {
37: Object old = binding.get();
38: binding.set(value);
39: return old;
40: } else {
41: pkg.set(symbol, value);
42: return null;
43: }
44: }
45:
46: public int size() {
47: return pkg.size();
48: }
49:
50: public Set entrySet() {
51: Enumeration e = pkg.bindings();
52: Set set = new HashSet();
53: while (e.hasMoreElements()) {
54: NamedValue binding = (NamedValue) e.nextElement();
55: set.add(new NamedValueEntry(binding));
56: }
57: return set;
58: }
59:
60: static class NamedValueEntry implements Map.Entry {
61: private NamedValue binding;
62:
63: NamedValueEntry(NamedValue binding) {
64: this .binding = binding;
65: }
66:
67: public Object getKey() {
68: return binding.getName();
69: }
70:
71: public Object getValue() {
72: return binding.get();
73: }
74:
75: public Object setValue(Object newValue) {
76: Object old = binding.get();
77: binding.set(newValue);
78: return old;
79: }
80:
81: public int hashCode() {
82: return binding.hashCode();
83: }
84:
85: public boolean equals(Object obj) {
86: if (obj instanceof NamedValueEntry) {
87: NamedValueEntry e = (NamedValueEntry) obj;
88: return binding.equals(e.binding);
89: }
90: return false;
91: }
92: }
93: }
|