KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > script > common > AbstractScriptableMap


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.script.common;
19
20 import java.util.AbstractMap JavaDoc;
21 import java.util.AbstractSet JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.beehive.netui.util.iterator.IteratorFactory;
27
28 /**
29  * Base {@link java.util.Map} implementation that can be used by
30  * clients that need to expose implicit objects to an expression
31  * language through a Map. This Map implementation is read-only.
32  */

33 public abstract class AbstractScriptableMap
34     extends AbstractMap JavaDoc {
35
36     /**
37      * Default implementation of a {@link java.util.Set} that can be returned by the
38      * entrySet method of {@link java.util.Map}. This implementation simply takes an
39      * array of entries and implements iterator() and size().
40      */

41     class EntrySet
42         extends AbstractSet JavaDoc {
43
44         private Entry[] _entries = null;
45
46         public EntrySet(Entry[] entries) {
47             _entries = entries;
48         }
49
50         public Iterator JavaDoc iterator() {
51             return IteratorFactory.createIterator(_entries);
52         }
53
54         public int size() {
55             return _entries != null ? _entries.length : 0;
56         }
57
58         public boolean add(Object JavaDoc object) {
59             throw new UnsupportedOperationException JavaDoc();
60         }
61
62         public boolean addAll(Collection JavaDoc coll) {
63             throw new UnsupportedOperationException JavaDoc();
64         }
65
66         public void clear() {
67             throw new UnsupportedOperationException JavaDoc();
68         }
69
70         public boolean remove(Object JavaDoc object) {
71             throw new UnsupportedOperationException JavaDoc();
72         }
73
74         public boolean removeAll(Collection JavaDoc coll) {
75             throw new UnsupportedOperationException JavaDoc();
76         }
77
78         public boolean retainAll(Collection JavaDoc coll) {
79             throw new UnsupportedOperationException JavaDoc();
80         }
81     }
82
83     /**
84      * Default implementation of {@link java.util.Map.Entry} that handles
85      * key / value pairs in a very basic way. This is meant as a convenience
86      * to subclasses that need to provide an entrySet() to satisfy the
87      * {@link java.util.AbstractMap} contract.
88      */

89     class Entry
90         implements Map.Entry JavaDoc {
91
92         private final Object JavaDoc _key;
93         private final Object JavaDoc _value;
94
95         Entry(Object JavaDoc key, Object JavaDoc value) {
96             _key = key;
97             _value = value;
98         }
99
100         public Object JavaDoc getKey() {
101             return _key;
102         }
103
104         public Object JavaDoc getValue() {
105             return _value;
106         }
107
108         public Object JavaDoc setValue(Object JavaDoc value) {
109             throw new UnsupportedOperationException JavaDoc();
110         }
111
112         public int hashCode() {
113             return ((_key == null ? 0 : _key.hashCode()) ^ (_value == null ? 0 : _value.hashCode()));
114         }
115
116         public boolean equals(Object JavaDoc obj) {
117             if(obj == null || !(obj instanceof Map.Entry JavaDoc))
118                 return false;
119
120             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)obj;
121             Object JavaDoc key = entry.getKey();
122             Object JavaDoc value = entry.getValue();
123             if((key == null || (key != null && key.equals(_key))) &&
124                 (value == null || (value != null && value.equals(_value))))
125                 return true;
126
127             return false;
128         }
129     }
130 }
131
Popular Tags