diff --git a/org.springframework.core/src/main/java/org/springframework/util/StringKeyedMapAdapter.java b/org.springframework.core/src/main/java/org/springframework/util/StringKeyedMapAdapter.java deleted file mode 100644 index c748243689e47e27c204602a24d5cb903e6890a9..0000000000000000000000000000000000000000 --- a/org.springframework.core/src/main/java/org/springframework/util/StringKeyedMapAdapter.java +++ /dev/null @@ -1,354 +0,0 @@ -/* - * Copyright 2002-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.util; - -import java.util.AbstractSet; -import java.util.Collection; -import java.util.Iterator; -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Set; - -/** - * Base class for map adapters whose keys are String values. - * Concrete classes need only implement the abstract hook methods defined by this class. - * @author Keith Donald - */ -public abstract class StringKeyedMapAdapter implements Map { - - private Set keySet; - - private Collection values; - - private Set> entrySet; - - - // implementing Map - - public void clear() { - for (Iterator it = getAttributeNames(); it.hasNext();) { - removeAttribute((String) it.next()); - } - } - - public boolean containsKey(Object key) { - return getAttribute(key.toString()) != null; - } - - public boolean containsValue(Object value) { - if (value == null) { - return false; - } - for (Iterator it = getAttributeNames(); it.hasNext();) { - Object aValue = getAttribute((String) it.next()); - if (value.equals(aValue)) { - return true; - } - } - return false; - } - - public Set> entrySet() { - return (entrySet != null) ? entrySet : (entrySet = new EntrySet()); - } - - public V get(Object key) { - return getAttribute(key.toString()); - } - - public boolean isEmpty() { - return !getAttributeNames().hasNext(); - } - - public Set keySet() { - return (keySet != null) ? keySet : (keySet = new KeySet()); - } - - public V put(String key, V value) { - String stringKey = String.valueOf(key); - V previousValue = getAttribute(stringKey); - setAttribute(stringKey, value); - return previousValue; - } - - public void putAll(Map map) { - for (Map.Entry entry : map.entrySet()) { - setAttribute(entry.getKey().toString(), entry.getValue()); - } - } - - public V remove(Object key) { - String stringKey = key.toString(); - V retval = getAttribute(stringKey); - removeAttribute(stringKey); - return retval; - } - - public int size() { - int size = 0; - for (Iterator it = getAttributeNames(); it.hasNext();) { - size++; - it.next(); - } - return size; - } - - public Collection values() { - return (values != null) ? values : (values = new Values()); - } - - - // hook methods - - /** - * Hook method that needs to be implemented by concrete subclasses. Gets a value associated with a key. - * @param key the key to lookup - * @return the associated value, or null if none - */ - protected abstract V getAttribute(String key); - - /** - * Hook method that needs to be implemented by concrete subclasses. Puts a key-value pair in the map, overwriting - * any possible earlier value associated with the same key. - * @param key the key to associate the value with - * @param value the value to associate with the key - */ - protected abstract void setAttribute(String key, V value); - - /** - * Hook method that needs to be implemented by concrete subclasses. Removes a key and its associated value from the - * map. - * @param key the key to remove - */ - protected abstract void removeAttribute(String key); - - /** - * Hook method that needs to be implemented by concrete subclasses. Returns an enumeration listing all keys known to - * the map. - * @return the key enumeration - */ - protected abstract Iterator getAttributeNames(); - - - // internal helper classes - - private class KeySet extends AbstractSet { - - public boolean isEmpty() { - return StringKeyedMapAdapter.this.isEmpty(); - } - - public int size() { - return StringKeyedMapAdapter.this.size(); - } - - public void clear() { - StringKeyedMapAdapter.this.clear(); - } - - public Iterator iterator() { - return new KeyIterator(); - } - - public boolean contains(Object o) { - return StringKeyedMapAdapter.this.containsKey(o); - } - - public boolean remove(Object o) { - return StringKeyedMapAdapter.this.remove(o) != null; - } - } - - - private class KeyIterator implements Iterator { - - private final Iterator it = getAttributeNames(); - - private String currentKey; - - public boolean hasNext() { - return it.hasNext(); - } - - public String next() { - return currentKey = it.next(); - } - - public void remove() { - if (currentKey == null) { - throw new NoSuchElementException("You must call next() at least once"); - } - StringKeyedMapAdapter.this.remove(currentKey); - } - } - - - private class Values extends AbstractSet { - - public boolean isEmpty() { - return StringKeyedMapAdapter.this.isEmpty(); - } - - public int size() { - return StringKeyedMapAdapter.this.size(); - } - - public void clear() { - StringKeyedMapAdapter.this.clear(); - } - - public Iterator iterator() { - return new ValuesIterator(); - } - - public boolean contains(Object o) { - return StringKeyedMapAdapter.this.containsValue(o); - } - - public boolean remove(Object o) { - if (o == null) { - return false; - } - for (Iterator it = iterator(); it.hasNext();) { - if (o.equals(it.next())) { - it.remove(); - return true; - } - } - return false; - } - } - - - private class ValuesIterator implements Iterator { - - private final Iterator it = getAttributeNames(); - - private String currentKey; - - public boolean hasNext() { - return it.hasNext(); - } - - public V next() { - currentKey = it.next(); - return StringKeyedMapAdapter.this.get(currentKey); - } - - public void remove() { - if (currentKey == null) { - throw new NoSuchElementException("You must call next() at least once"); - } - StringKeyedMapAdapter.this.remove(currentKey); - } - } - - - private class EntrySet extends AbstractSet> { - - public boolean isEmpty() { - return StringKeyedMapAdapter.this.isEmpty(); - } - - public int size() { - return StringKeyedMapAdapter.this.size(); - } - - public void clear() { - StringKeyedMapAdapter.this.clear(); - } - - public Iterator> iterator() { - return new EntryIterator(); - } - - @SuppressWarnings("unchecked") - public boolean contains(Object o) { - if (!(o instanceof Entry)) { - return false; - } - Entry entry = (Entry) o; - Object key = entry.getKey(); - Object value = entry.getValue(); - if (key == null || value == null) { - return false; - } - return value.equals(StringKeyedMapAdapter.this.get(key)); - } - - @SuppressWarnings("unchecked") - public boolean remove(Object o) { - if (!(o instanceof Entry)) { - return false; - } - Entry entry = (Entry) o; - Object key = entry.getKey(); - Object value = entry.getValue(); - if (key == null || value == null || !value.equals(StringKeyedMapAdapter.this.get(key))) { - return false; - } - return StringKeyedMapAdapter.this.remove(((Entry) o).getKey()) != null; - } - } - - - private class EntryIterator implements Iterator> { - - private final Iterator it = getAttributeNames(); - - private String currentKey; - - public boolean hasNext() { - return it.hasNext(); - } - - public Map.Entry next() { - currentKey = it.next(); - return new EntrySetEntry(currentKey); - } - - public void remove() { - if (currentKey == null) { - throw new NoSuchElementException("You must call next() at least once"); - } - StringKeyedMapAdapter.this.remove(currentKey); - } - - } - - - private class EntrySetEntry implements Entry { - - private final String currentKey; - - public EntrySetEntry(String currentKey) { - this.currentKey = currentKey; - } - - public String getKey() { - return currentKey; - } - - public V getValue() { - return StringKeyedMapAdapter.this.get(currentKey); - } - - public V setValue(V value) { - return StringKeyedMapAdapter.this.put(currentKey, value); - } - } - -} diff --git a/org.springframework.core/src/test/java/org/springframework/util/StringKeyedMapAdapterTests.java b/org.springframework.core/src/test/java/org/springframework/util/StringKeyedMapAdapterTests.java deleted file mode 100644 index bc888f011fb52508d9495a16c4a59f2eb04d9570..0000000000000000000000000000000000000000 --- a/org.springframework.core/src/test/java/org/springframework/util/StringKeyedMapAdapterTests.java +++ /dev/null @@ -1,82 +0,0 @@ -package org.springframework.util; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -import junit.framework.TestCase; - -/** - * Unit tests for {@link org.springframework.binding.collection.StringKeyedMapAdapter}. - */ -public class StringKeyedMapAdapterTests extends TestCase { - - private Map contents = new HashMap(); - - private StringKeyedMapAdapter map = new StringKeyedMapAdapter() { - - protected Object getAttribute(String key) { - return contents.get(key); - } - - protected Iterator getAttributeNames() { - return contents.keySet().iterator(); - } - - protected void removeAttribute(String key) { - contents.remove(key); - } - - protected void setAttribute(String key, Object value) { - contents.put(key, value); - } - }; - - public void testGetPutRemove() { - assertTrue(map.size() == 0); - assertTrue(map.isEmpty()); - assertNull(map.get("foo")); - assertFalse(map.containsKey("foo")); - map.put("foo", "bar"); - assertTrue(map.size() == 1); - assertFalse(map.isEmpty()); - assertNotNull(map.get("foo")); - assertTrue(map.containsKey("foo")); - assertTrue(map.containsValue("bar")); - assertEquals("bar", map.get("foo")); - map.remove("foo"); - assertTrue(map.size() == 0); - assertNull(map.get("foo")); - } - - public void testPutAll() { - Map all = new HashMap(); - all.put("foo", "bar"); - all.put("bar", "baz"); - map.putAll(all); - assertTrue(map.size() == 2); - } - - public void testEntrySet() { - map.put("foo", "bar"); - map.put("bar", "baz"); - Set entrySet = map.entrySet(); - assertTrue(entrySet.size() == 2); - } - - public void testKeySet() { - map.put("foo", "bar"); - map.put("bar", "baz"); - Set keySet = map.keySet(); - assertTrue(keySet.size() == 2); - } - - public void testValues() { - map.put("foo", "bar"); - map.put("bar", "baz"); - Collection values = map.values(); - assertTrue(values.size() == 2); - } -} diff --git a/org.springframework.web/src/main/java/org/springframework/web/context/request/NativeWebRequestParameterMap.java b/org.springframework.web/src/main/java/org/springframework/web/context/request/NativeWebRequestParameterMap.java deleted file mode 100644 index 8789c29d3e71b29d73768eb85aafccfb3753a38e..0000000000000000000000000000000000000000 --- a/org.springframework.web/src/main/java/org/springframework/web/context/request/NativeWebRequestParameterMap.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 2004-2009 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.web.context.request; - -import java.util.Iterator; - -import org.springframework.util.Assert; -import org.springframework.util.CompositeIterator; -import org.springframework.util.StringKeyedMapAdapter; -import org.springframework.web.multipart.MultipartRequest; - -/** - * Map backed by a Web request parameter map for accessing request parameters. - * Also provides support for multi-part requests, providing transparent access to the request "fileMap" as a request parameter entry. - * @author Keith Donald - * @since 3.0 - */ -public class NativeWebRequestParameterMap extends StringKeyedMapAdapter { - - /** - * The wrapped native request. - */ - private NativeWebRequest request; - - /** - * Create a new map wrapping the parameters of given request. - */ - public NativeWebRequestParameterMap(NativeWebRequest request) { - Assert.notNull(request, "The NativeWebRequest is required"); - this.request = request; - } - - protected Object getAttribute(String key) { - if (request instanceof MultipartRequest) { - MultipartRequest multipartRequest = (MultipartRequest) request; - Object data = multipartRequest.getFileMap().get(key); - if (data != null) { - return data; - } - } - String[] parameters = request.getParameterValues(key); - if (parameters == null) { - return null; - } else if (parameters.length == 1) { - return parameters[0]; - } else { - return parameters; - } - } - - protected void setAttribute(String key, Object value) { - throw new UnsupportedOperationException("WebRequest parameter maps are immutable"); - } - - protected void removeAttribute(String key) { - throw new UnsupportedOperationException("WebRequest parameter maps are immutable"); - } - - protected Iterator getAttributeNames() { - if (request instanceof MultipartRequest) { - MultipartRequest multipartRequest = (MultipartRequest) request; - CompositeIterator iterator = new CompositeIterator(); - iterator.add(multipartRequest.getFileMap().keySet().iterator()); - iterator.add(request.getParameterNames()); - return iterator; - } else { - return request.getParameterNames(); - } - } -} \ No newline at end of file diff --git a/org.springframework.web/src/test/java/org/springframework/web/context/request/NativeWebRequestParameterMapTests.java b/org.springframework.web/src/test/java/org/springframework/web/context/request/NativeWebRequestParameterMapTests.java deleted file mode 100644 index 00234938330f2ee5224ae663413b58fc04ed9a23..0000000000000000000000000000000000000000 --- a/org.springframework.web/src/test/java/org/springframework/web/context/request/NativeWebRequestParameterMapTests.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.springframework.web.context.request; - -import java.util.Iterator; - -import junit.framework.TestCase; - -import org.springframework.mock.web.MockHttpServletRequest; - -/** - * Unit test for the {@link HttpServletRequestParameterMap} class. - * - * @author Ulrik Sandberg - */ -public class NativeWebRequestParameterMapTests extends TestCase { - - private NativeWebRequestParameterMap tested; - - private MockHttpServletRequest request; - - protected void setUp() throws Exception { - super.setUp(); - request = new MockHttpServletRequest(); - tested = new NativeWebRequestParameterMap(new ServletWebRequest(request)); - } - - protected void tearDown() throws Exception { - super.tearDown(); - request = null; - tested = null; - } - - public void testGetAttribute() { - request.setParameter("Some param", "Some value"); - // perform test - Object result = tested.getAttribute("Some param"); - assertEquals("Some value", result); - } - - public void testSetAttribute() { - // perform test - try { - tested.setAttribute("Some key", "Some value"); - fail("UnsupportedOperationException expected"); - } catch (UnsupportedOperationException expected) { - // expected - } - } - - public void testRemoveAttribute() { - request.setParameter("Some param", "Some value"); - // perform test - try { - tested.removeAttribute("Some param"); - fail("UnsupportedOperationException expected"); - } catch (UnsupportedOperationException expected) { - // expected - } - } - - public void testGetAttributeNames() { - request.setParameter("Some param", "Some value"); - // perform test - Iterator names = tested.getAttributeNames(); - assertNotNull("Null result unexpected", names); - assertTrue("More elements", names.hasNext()); - String name = (String) names.next(); - assertEquals("Some param", name); - } -} \ No newline at end of file