1 16 package org.apache.commons.chain.web.portlet; 17 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.Enumeration ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Set ; 27 import javax.portlet.PortletRequest; 28 29 30 37 38 final class PortletRequestScopeMap implements Map { 39 40 41 public PortletRequestScopeMap(PortletRequest request) { 42 this.request = request; 43 } 44 45 46 private PortletRequest request = null; 47 48 49 public void clear() { 50 Iterator keys = keySet().iterator(); 51 while (keys.hasNext()) { 52 request.removeAttribute((String ) keys.next()); 53 } 54 } 55 56 57 public boolean containsKey(Object key) { 58 return (request.getAttribute(key(key)) != null); 59 } 60 61 62 public boolean containsValue(Object value) { 63 if (value == null) { 64 return (false); 65 } 66 Enumeration keys = request.getAttributeNames(); 67 while (keys.hasMoreElements()) { 68 Object next = request.getAttribute((String ) keys.nextElement()); 69 if (next == value) { 70 return (true); 71 } 72 } 73 return (false); 74 } 75 76 77 public Set entrySet() { 78 Set set = new HashSet (); 79 Enumeration keys = request.getAttributeNames(); 80 while (keys.hasMoreElements()) { 81 set.add(request.getAttribute((String ) keys.nextElement())); 82 } 83 return (set); 84 } 85 86 87 public boolean equals(Object o) { 88 return (request.equals(o)); 89 } 90 91 92 public Object get(Object key) { 93 return (request.getAttribute(key(key))); 94 } 95 96 97 public int hashCode() { 98 return (request.hashCode()); 99 } 100 101 102 public boolean isEmpty() { 103 return (size() < 1); 104 } 105 106 107 public Set keySet() { 108 Set set = new HashSet (); 109 Enumeration keys = request.getAttributeNames(); 110 while (keys.hasMoreElements()) { 111 set.add(keys.nextElement()); 112 } 113 return (set); 114 } 115 116 117 public Object put(Object key, Object value) { 118 if (value == null) { 119 return (remove(key)); 120 } 121 String skey = key(key); 122 Object previous = request.getAttribute(skey); 123 request.setAttribute(skey, value); 124 return (previous); 125 } 126 127 128 public void putAll(Map map) { 129 Iterator keys = map.keySet().iterator(); 130 while (keys.hasNext()) { 131 String key = (String ) keys.next(); 132 request.setAttribute(key, map.get(key)); 133 } 134 } 135 136 137 public Object remove(Object key) { 138 String skey = key(key); 139 Object previous = request.getAttribute(skey); 140 request.removeAttribute(skey); 141 return (previous); 142 } 143 144 145 public int size() { 146 int n = 0; 147 Enumeration keys = request.getAttributeNames(); 148 while (keys.hasMoreElements()) { 149 keys.nextElement(); 150 n++; 151 } 152 return (n); 153 } 154 155 156 public Collection values() { 157 List list = new ArrayList (); 158 Enumeration keys = request.getAttributeNames(); 159 while (keys.hasMoreElements()) { 160 list.add(request.getAttribute((String ) keys.nextElement())); 161 } 162 return (list); 163 } 164 165 166 private String key(Object key) { 167 if (key == null) { 168 throw new IllegalArgumentException (); 169 } else if (key instanceof String ) { 170 return ((String ) key); 171 } else { 172 return (key.toString()); 173 } 174 } 175 176 177 } 178 | Popular Tags |