001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.migration.model22.impl.user;
023:
024: import org.jboss.portal.migration.model22.model.PropertyMap;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Collections;
029: import java.util.HashMap;
030: import java.util.HashSet;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.Set;
034:
035: /**
036: * A mutable map that expose user properties.
037: *
038: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
039: * @version $Revision: 8784 $
040: */
041: public class PropertyMapImpl implements PropertyMap {
042:
043: private User22Impl user;
044:
045: public PropertyMapImpl(User22Impl user) {
046: this .user = user;
047: }
048:
049: public boolean isReadOnly(Object key) {
050: if (!(key instanceof String)) {
051: return false;
052: }
053: User22Impl.PropertyAccessor accessor = (User22Impl.PropertyAccessor) User22Impl.ACCESSORS
054: .get(key);
055: return accessor != null && !accessor.isWritable();
056: }
057:
058: public int size() {
059: return User22Impl.ACCESSORS.size() + user.getDynamic().size();
060: }
061:
062: public boolean isEmpty() {
063: return false;
064: }
065:
066: public boolean containsKey(Object key) {
067: if (!(key instanceof String)) {
068: return false;
069: }
070: User22Impl.PropertyAccessor accessor = (User22Impl.PropertyAccessor) User22Impl.ACCESSORS
071: .get(key);
072: if (accessor != null) {
073: return true;
074: }
075: return user.getDynamic().containsKey(key);
076: }
077:
078: public boolean containsValue(Object value1) {
079: for (Iterator i = User22Impl.ACCESSORS.values().iterator(); i
080: .hasNext();) {
081: User22Impl.PropertyAccessor accessor = (User22Impl.PropertyAccessor) i
082: .next();
083: Object value2 = accessor.get(user);
084: if (value1 == value2
085: || (value1 != null && value1.equals(value2))) {
086: return true;
087: }
088: }
089: return user.getDynamic().containsValue(value1);
090: }
091:
092: public Object get(Object key) {
093: if (!(key instanceof String)) {
094: return null;
095: }
096: User22Impl.PropertyAccessor accessor = (User22Impl.PropertyAccessor) User22Impl.ACCESSORS
097: .get(key);
098: if (accessor != null) {
099: return accessor.get(user);
100: }
101: return user.getDynamic().get(key);
102: }
103:
104: /** @throws IllegalArgumentException if the key is not modifiable */
105: public Object put(Object key, Object newValue)
106: throws IllegalArgumentException {
107: if (!(key instanceof String)) {
108: throw new IllegalArgumentException("Key is not a String");
109: }
110: User22Impl.PropertyAccessor accessor = (User22Impl.PropertyAccessor) User22Impl.ACCESSORS
111: .get(key);
112: if (accessor != null) {
113: if (!accessor.isWritable()) {
114: throw new IllegalArgumentException("Key " + key
115: + " is not modifiable");
116: } else {
117: Object oldValue = accessor.get(user);
118: accessor.set(user, (String) newValue);
119: return oldValue;
120: }
121: }
122: return user.getDynamic().put(key, newValue);
123: }
124:
125: /**
126: * Only affect dynamic properties, otherwise it throws an IllegalArgumentException.
127: *
128: * @throws IllegalArgumentException if the key is a not removable user property
129: */
130: public Object remove(Object key) throws IllegalArgumentException {
131: if (!(key instanceof String)) {
132: return null;
133: }
134: User22Impl.PropertyAccessor accessor = (User22Impl.PropertyAccessor) User22Impl.ACCESSORS
135: .get(key);
136: if (accessor != null) {
137: throw new IllegalArgumentException("Key " + key
138: + " is not removable");
139: }
140: return user.getDynamic().remove(key);
141: }
142:
143: /** Clear only dynamic properties. */
144: public void clear() {
145: user.getDynamic().clear();
146: }
147:
148: public Set keySet() {
149: // Get
150: Set set = new HashSet(size());
151:
152: //
153: set.addAll(user.getDynamic().keySet());
154: set.addAll(User22Impl.ACCESSORS.keySet());
155: return set;
156: }
157:
158: public Collection values() {
159: ArrayList collection = new ArrayList(size());
160: for (Iterator i = User22Impl.ACCESSORS.values().iterator(); i
161: .hasNext();) {
162: User22Impl.PropertyAccessor accessor = (User22Impl.PropertyAccessor) i
163: .next();
164: collection.add(accessor.get(user));
165: }
166: collection.addAll(user.getDynamic().values());
167: return collection;
168: }
169:
170: /** Returns an immutable collection of entries. */
171: public Set entrySet() {
172: Map copy = new HashMap(user.getDynamic());
173: for (Iterator i = User22Impl.ACCESSORS.values().iterator(); i
174: .hasNext();) {
175: User22Impl.PropertyAccessor accessor = (User22Impl.PropertyAccessor) i
176: .next();
177: copy.put(accessor.getPropertyName(), accessor.get(user));
178: }
179: return Collections.unmodifiableMap(copy).entrySet();
180: }
181:
182: public void putAll(Map map) {
183: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
184: Map.Entry entry = (Entry) i.next();
185: Object key = entry.getKey();
186: if (key instanceof String) {
187: Object value = entry.getValue();
188: User22Impl.PropertyAccessor accessor = (User22Impl.PropertyAccessor) User22Impl.ACCESSORS
189: .get(key);
190: if (accessor != null) {
191: if (accessor.isWritable()) {
192: accessor.set(user, (String) value);
193: } else {
194: // julien : Do something better ?
195: }
196: } else {
197: user.getDynamic().put(key, value);
198: }
199: }
200: }
201: }
202: }
|