001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.web.portlet;
017:
018: import java.util.ArrayList;
019: import java.util.Collection;
020: import java.util.Enumeration;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026: import javax.portlet.PortletRequest;
027: import org.apache.commons.chain.web.MapEntry;
028:
029: /**
030: * <p>Private implementation of <code>Map</code> for portlet parameter
031: * name-value.</p>
032: *
033: * @author Craig R. McClanahan
034: * @version $Revision: 412719 $ $Date: 2006-06-08 11:49:18 +0100 (Thu, 08 Jun 2006) $
035: */
036:
037: final class PortletParamMap implements Map {
038:
039: public PortletParamMap(PortletRequest request) {
040: this .request = request;
041: }
042:
043: private PortletRequest request = null;
044:
045: public void clear() {
046: throw new UnsupportedOperationException();
047: }
048:
049: public boolean containsKey(Object key) {
050: return (request.getParameter(key(key)) != null);
051: }
052:
053: public boolean containsValue(Object value) {
054: Iterator values = values().iterator();
055: while (values.hasNext()) {
056: if (value.equals(values.next())) {
057: return (true);
058: }
059: }
060: return (false);
061: }
062:
063: public Set entrySet() {
064: Set set = new HashSet();
065: Enumeration keys = request.getParameterNames();
066: String key;
067: while (keys.hasMoreElements()) {
068: key = (String) keys.nextElement();
069: set
070: .add(new MapEntry(key, request.getParameter(key),
071: false));
072: }
073: return (set);
074: }
075:
076: public boolean equals(Object o) {
077: return (request.equals(o));
078: }
079:
080: public Object get(Object key) {
081: return (request.getParameter(key(key)));
082: }
083:
084: public int hashCode() {
085: return (request.hashCode());
086: }
087:
088: public boolean isEmpty() {
089: return (size() < 1);
090: }
091:
092: public Set keySet() {
093: Set set = new HashSet();
094: Enumeration keys = request.getParameterNames();
095: while (keys.hasMoreElements()) {
096: set.add(keys.nextElement());
097: }
098: return (set);
099: }
100:
101: public Object put(Object key, Object value) {
102: throw new UnsupportedOperationException();
103: }
104:
105: public void putAll(Map map) {
106: throw new UnsupportedOperationException();
107: }
108:
109: public Object remove(Object key) {
110: throw new UnsupportedOperationException();
111: }
112:
113: public int size() {
114: int n = 0;
115: Enumeration keys = request.getParameterNames();
116: while (keys.hasMoreElements()) {
117: keys.nextElement();
118: n++;
119: }
120: return (n);
121: }
122:
123: public Collection values() {
124: List list = new ArrayList();
125: Enumeration keys = request.getParameterNames();
126: while (keys.hasMoreElements()) {
127: list.add(request.getParameter((String) keys.nextElement()));
128: }
129: return (list);
130: }
131:
132: private String key(Object key) {
133: if (key == null) {
134: throw new IllegalArgumentException();
135: } else if (key instanceof String) {
136: return ((String) key);
137: } else {
138: return (key.toString());
139: }
140: }
141:
142: }
|