ScriptableなMap

Javaのクラスがこんな感じ。
hasInstance()の意味がよく分からなかった。


public class JsMap extends AbstractMap implements Scriptable {

private Set _entrySet = new LinkedHashSet();

public Set entrySet() {
return _entrySet;
}

public Object put(final Object key, final Object value) {
Map.Entry entry = new Map.Entry() {
private Object _key = key;

private Object _value = value;

public Object getKey() {
return _key;
}

public Object getValue() {
return _value;
}

public Object setValue(Object _value) {
this._value = _value;
return this._value;
}

};

_entrySet.add(entry);
return entry;
}

private Scriptable _prototype = null;

private Scriptable _parentScope = null;

public String getClassName() {
return this.getClass().getName();
}

public Object get(String name, Scriptable self) {
return get(name);
}

public Object get(int i, Scriptable self) {
throw new UnsupportedOperationException();
}

public boolean has(String name, Scriptable self) {
return containsKey(name);
}

public boolean has(int i, Scriptable self) {
throw new UnsupportedOperationException();
}

public void put(String name, Scriptable self, Object value) {
put(name, value);
}

public void put(int i, Scriptable self, Object value) {
throw new UnsupportedOperationException();
}

public void delete(String name) {
remove(name);
}

public void delete(int i) {
throw new UnsupportedOperationException();
}

public Scriptable getPrototype() {
return _prototype;
}

public void setPrototype(Scriptable prototype) {
_prototype = prototype;
}

public Scriptable getParentScope() {
return _parentScope;
}

public void setParentScope(Scriptable parentScope) {
_parentScope = parentScope;
}

public Object[] getIds() {
return keySet().toArray();
}

public Object getDefaultValue(Class type) {
return toString();
}

public boolean hasInstance(Scriptable self) {
return false;
}

}

で、こんな感じに使うと。

var m = new JsMap();
m.foo = "FOO";
m.bar = "BAR";
m.zoo = "ZOO";
m.baz = function() {print("hello. Rhino.")}

print(m.foo);
print(m.bar);
print(m.zoo);

print(m["foo"]);
print(m["bar"]);
print(m["zoo"]);

for(var i in m) {
print(i + ": " + m[i]);
}

m.baz();

Javaのメソッドが呼び出せなくなるけど…まあいいか。