001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.script.common;
020:
021: import java.util.AbstractMap;
022: import java.util.AbstractSet;
023: import java.util.Collection;
024: import java.util.Iterator;
025: import java.util.Map;
026:
027: import org.apache.beehive.netui.util.iterator.IteratorFactory;
028:
029: /**
030: * Base {@link java.util.Map} implementation that can be used by
031: * clients that need to expose implicit objects to an expression
032: * language through a Map. This Map implementation is read-only.
033: */
034: public abstract class AbstractScriptableMap extends AbstractMap {
035:
036: /**
037: * Default implementation of a {@link java.util.Set} that can be returned by the
038: * entrySet method of {@link java.util.Map}. This implementation simply takes an
039: * array of entries and implements iterator() and size().
040: */
041: class EntrySet extends AbstractSet {
042:
043: private Entry[] _entries = null;
044:
045: public EntrySet(Entry[] entries) {
046: _entries = entries;
047: }
048:
049: public Iterator iterator() {
050: return IteratorFactory.createIterator(_entries);
051: }
052:
053: public int size() {
054: return _entries != null ? _entries.length : 0;
055: }
056:
057: public boolean add(Object object) {
058: throw new UnsupportedOperationException();
059: }
060:
061: public boolean addAll(Collection coll) {
062: throw new UnsupportedOperationException();
063: }
064:
065: public void clear() {
066: throw new UnsupportedOperationException();
067: }
068:
069: public boolean remove(Object object) {
070: throw new UnsupportedOperationException();
071: }
072:
073: public boolean removeAll(Collection coll) {
074: throw new UnsupportedOperationException();
075: }
076:
077: public boolean retainAll(Collection coll) {
078: throw new UnsupportedOperationException();
079: }
080: }
081:
082: /**
083: * Default implementation of {@link java.util.Map.Entry} that handles
084: * key / value pairs in a very basic way. This is meant as a convenience
085: * to subclasses that need to provide an entrySet() to satisfy the
086: * {@link java.util.AbstractMap} contract.
087: */
088: class Entry implements Map.Entry {
089:
090: private final Object _key;
091: private final Object _value;
092:
093: Entry(Object key, Object value) {
094: _key = key;
095: _value = value;
096: }
097:
098: public Object getKey() {
099: return _key;
100: }
101:
102: public Object getValue() {
103: return _value;
104: }
105:
106: public Object setValue(Object value) {
107: throw new UnsupportedOperationException();
108: }
109:
110: public int hashCode() {
111: return ((_key == null ? 0 : _key.hashCode()) ^ (_value == null ? 0
112: : _value.hashCode()));
113: }
114:
115: public boolean equals(Object o) {
116: if (this == o)
117: return true;
118: if (o == null || getClass() != o.getClass())
119: return false;
120:
121: final Entry entry = (Entry) o;
122:
123: if (_key != null ? !_key.equals(entry._key)
124: : entry._key != null)
125: return false;
126: if (_value != null ? !_value.equals(entry._value)
127: : entry._value != null)
128: return false;
129:
130: return true;
131: }
132: }
133: }
|