001: package org.gui4j.core;
002:
003: import java.io.Serializable;
004: import java.util.ArrayList;
005: import java.util.Collection;
006: import java.util.HashSet;
007: import java.util.Map;
008: import java.util.Set;
009:
010: /**
011: * The class <code>Gui4jAccess</code> works with parameter maps. Since usually only one
012: * parameter is used, this class provides a very simple implementation of the
013: * <code>Map</code> interface for exaclty one entry. Note that this class is only to
014: * improve performance.
015: * @author Joachim Schmid
016: */
017: public final class Gui4jMap1 implements Map, Map.Entry, Serializable {
018: private final Object mKey;
019: private Object mValue;
020:
021: public Gui4jMap1(Object key, Object value) {
022: mKey = key;
023: mValue = value;
024: }
025:
026: public Object getKey() {
027: return mKey;
028: }
029:
030: public Object getValue() {
031: return mValue;
032: }
033:
034: public Object setValue(Object value) {
035: mValue = value;
036: return value;
037: }
038:
039: /**
040: * @see java.util.Map#size()
041: */
042: public int size() {
043: return 1;
044: }
045:
046: /**
047: * @see java.util.Map#isEmpty()
048: */
049: public boolean isEmpty() {
050: return false;
051: }
052:
053: /**
054: * @see java.util.Map#containsKey(Object)
055: */
056: public boolean containsKey(Object key) {
057: if (mKey == null) {
058: return key == null;
059: } else {
060: if (key == null) {
061: return false;
062: }
063: return mKey.equals(key);
064: }
065: }
066:
067: /**
068: * @see java.util.Map#containsValue(Object)
069: */
070: public boolean containsValue(Object value) {
071: if (mValue == null) {
072: return value == null;
073: } else {
074: if (value == null) {
075: return false;
076: }
077: return mValue.equals(value);
078: }
079: }
080:
081: /**
082: * @see java.util.Map#get(Object)
083: */
084: public Object get(Object key) {
085: if (mKey == key) {
086: return mValue;
087: }
088: if (mKey == null || key == null) {
089: return null;
090: }
091: if (mKey.equals(key)) {
092: return mValue;
093: }
094: return null;
095: }
096:
097: /**
098: * @see java.util.Map#put(Object, Object)
099: */
100: public Object put(Object key, Object value) {
101: throw new UnsupportedOperationException(
102: "unexpected put operation");
103: }
104:
105: /**
106: * @see java.util.Map#remove(Object)
107: */
108: public Object remove(Object arg0) {
109: throw new UnsupportedOperationException(
110: "unexpected remove operation");
111: }
112:
113: /**
114: * @see java.util.Map#putAll(Map)
115: */
116: public void putAll(Map arg0) {
117: throw new UnsupportedOperationException(
118: "unexpected putAll operation");
119: }
120:
121: /**
122: * @see java.util.Map#clear()
123: */
124: public void clear() {
125: throw new UnsupportedOperationException(
126: "unexpected clear operation");
127: }
128:
129: /**
130: * @see java.util.Map#keySet()
131: */
132: public Set keySet() {
133: HashSet set = new HashSet();
134: set.add(mKey);
135: return set;
136: }
137:
138: /**
139: * @see java.util.Map#values()
140: */
141: public Collection values() {
142: ArrayList list = new ArrayList();
143: list.add(mValue);
144: return list;
145: }
146:
147: /**
148: * @see java.util.Map#entrySet()
149: */
150: public Set entrySet() {
151: Set set = new HashSet();
152: set.add(this);
153: return set;
154: }
155:
156: }
|