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: package org.apache.jetspeed.om.page.impl;
018:
019: import java.util.AbstractMap;
020: import java.util.AbstractSet;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.Map;
024: import java.util.Set;
025:
026: import org.apache.jetspeed.page.impl.DatabasePageManagerUtils;
027:
028: /**
029: * FragmentPropertyMap
030: *
031: * @author <a href="mailto:rwatler@apache.org">Randy Watler</a>
032: * @version $Id$
033: */
034: class FragmentPropertyMap extends AbstractMap {
035: private FragmentImpl fragment;
036: private FragmentPropertiesEntrySet entrySet;
037:
038: FragmentPropertyMap(FragmentImpl fragment) {
039: super ();
040: this .fragment = fragment;
041: // populate fragment properties using property members
042: entrySet = new FragmentPropertiesEntrySet();
043: Iterator keyIter = fragment.getPropertyMemberKeys().iterator();
044: while (keyIter.hasNext()) {
045: String key = (String) keyIter.next();
046: entrySet.add(new FragmentPropertiesEntry(key, fragment
047: .getPropertyMember(key)));
048: }
049: }
050:
051: /* (non-Javadoc)
052: * @see java.util.Map#put(java.lang.Object, java.lang.Object)
053: */
054: public Object put(Object key, Object value) {
055: // implement for modifiable AbstractMap:
056: // set map entry value or add new map entry
057: // using iterator to find key entry
058: FragmentPropertiesEntry entry = new FragmentPropertiesEntry(
059: key, value);
060: Iterator entryIter = entrySet.iterator();
061: while (entryIter.hasNext()) {
062: FragmentPropertiesEntry testEntry = (FragmentPropertiesEntry) entryIter
063: .next();
064: if (testEntry.equals(entry)) {
065: Object oldValue = testEntry.getValue();
066: testEntry.setValue(entry.getValue());
067: return oldValue;
068: }
069: }
070: entrySet.add(entry);
071: return null;
072: }
073:
074: /* (non-Javadoc)
075: * @see java.util.Map#entrySet()
076: */
077: public Set entrySet() {
078: // implement for modifiable AbstractMap
079: return entrySet;
080: }
081:
082: private class FragmentPropertiesEntrySet extends AbstractSet {
083: private Collection entries = DatabasePageManagerUtils
084: .createCollection();
085:
086: /* (non-Javadoc)
087: * @see java.util.Set#add(java.lang.Object)
088: */
089: public boolean add(Object o) {
090: // implement for modifiable AbstractSet:
091: FragmentPropertiesEntry entry = (FragmentPropertiesEntry) o;
092: if (!entries.contains(entry)) {
093: // set fragment explicit property member
094: fragment.setPropertyMember(entry.getKey().toString(),
095: entry.getValue().toString());
096: // add entry to set
097: entries.add(o);
098: return true;
099: }
100: return false;
101: }
102:
103: /* (non-Javadoc)
104: * @see java.util.Set#iterator()
105: */
106: public Iterator iterator() {
107: // implement for modifiable AbstractSet:
108: return new Iterator() {
109: private Iterator iter = entries.iterator();
110: private FragmentPropertiesEntry last;
111:
112: /* (non-Javadoc)
113: * @see java.util.Iterator#hasNext()
114: */
115: public boolean hasNext() {
116: // implement for modifiable AbstractSet:
117: return iter.hasNext();
118: }
119:
120: /* (non-Javadoc)
121: * @see java.util.Iterator#next()
122: */
123: public Object next() {
124: // implement for modifiable AbstractSet:
125: last = (FragmentPropertiesEntry) iter.next();
126: return last;
127: }
128:
129: /* (non-Javadoc)
130: * @see java.util.Iterator#remove()
131: */
132: public void remove() {
133: // implement for modifiable AbstractSet:
134: // clear fragment explicit property associated with entry
135: if (last == null) {
136: throw new IllegalStateException(
137: "No preceding call to next() or remove() already invoked");
138: }
139: FragmentPropertyMap.this .fragment
140: .clearPropertyMember(last.getKey()
141: .toString());
142: last = null;
143: // remove entry using iterator
144: iter.remove();
145: }
146: };
147: }
148:
149: /* (non-Javadoc)
150: * @see java.util.Set#size()
151: */
152: public int size() {
153: // implement for modifiable AbstractSet:
154: return entries.size();
155: }
156: }
157:
158: private class FragmentPropertiesEntry implements Map.Entry {
159: private Object key;
160: private Object value;
161:
162: public FragmentPropertiesEntry(Object key, Object value) {
163: this .key = key;
164: this .value = value;
165: }
166:
167: /* (non-Javadoc)
168: * @see java.util.Map.Entry#getKey()
169: */
170: public Object getKey() {
171: return key;
172: }
173:
174: /* (non-Javadoc)
175: * @see java.util.Map.Entry#getValue()
176: */
177: public Object getValue() {
178: return value;
179: }
180:
181: /* (non-Javadoc)
182: * @see java.util.Map.Entry#setValue(java.lang.Object)
183: */
184: public Object setValue(Object newValue) {
185: // set fragment explicit property associated with entry
186: FragmentPropertyMap.this .fragment.setPropertyMember(key
187: .toString(), newValue.toString());
188: // set entry value
189: Object oldValue = value;
190: value = newValue;
191: return oldValue;
192: }
193:
194: /* (non-Javadoc)
195: * @see java.lang.Object#equals(java.lang.Object)
196: */
197: public boolean equals(Object o) {
198: if (o instanceof FragmentPropertiesEntry) {
199: if (key != null) {
200: return key.equals(((FragmentPropertiesEntry) o)
201: .getKey());
202: }
203: return (((FragmentPropertiesEntry) o).getKey() == null);
204: }
205: return false;
206: }
207:
208: /* (non-Javadoc)
209: * @see java.lang.Object#hashCode()
210: */
211: public int hashCode() {
212: if (key != null) {
213: return key.hashCode();
214: }
215: return 0;
216: }
217: }
218: }
|