You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by ba...@apache.org on 2007/09/04 00:30:58 UTC
svn commit: r572440 - in /james/mime4j/trunk/src:
main/java/org/apache/james/mime4j/util/StringArrayMap.java
test/java/org/apache/james/mime4j/util/StringArrayMapTest.java
Author: bago
Date: Mon Sep 3 15:30:57 2007
New Revision: 572440
URL: http://svn.apache.org/viewvc?rev=572440&view=rev
Log:
Introduced StringArrayMap as an utility class for mime4j users (MIME4J-24)
Patch submitted by Jochen Wiedmann.
Added:
james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/StringArrayMap.java (with props)
james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/StringArrayMapTest.java (with props)
Added: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/StringArrayMap.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/StringArrayMap.java?rev=572440&view=auto
==============================================================================
--- james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/StringArrayMap.java (added)
+++ james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/StringArrayMap.java Mon Sep 3 15:30:57 2007
@@ -0,0 +1,239 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.james.mime4j.util;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.NoSuchElementException;
+
+
+/**
+ * An object, which may be used to implement header, or parameter
+ * maps. The maps keys are the header or parameter names. The
+ * maps values are strings (single value), lists, or arrays.
+ */
+public class StringArrayMap implements Serializable {
+ private static final long serialVersionUID = -5833051164281786907L;
+ private final Map map = new HashMap();
+
+ /**
+ * <p>Converts the given object into a string. The object may be either of:
+ * <ul>
+ * <li>a string, which is returned without conversion</li>
+ * <li>a list of strings, in which case the first element is returned</li>
+ * <li>an array of strings, in which case the first element is returned</li>
+ * </ul>
+ */
+ public static String asString(Object pValue) {
+ if (pValue == null) {
+ return null;
+ }
+ if (pValue instanceof String) {
+ return (String) pValue;
+ }
+ if (pValue instanceof String[]) {
+ return ((String[]) pValue)[0];
+ }
+ if (pValue instanceof List) {
+ return (String) ((List) pValue).get(0);
+ }
+ throw new IllegalStateException("Invalid parameter class: " + pValue.getClass().getName());
+ }
+
+ /**
+ * <p>Converts the given object into a string array. The object may be either of:
+ * <ul>
+ * <li>a string, which is returned as an array with one element</li>
+ * <li>a list of strings, which is being converted into a string array</li>
+ * <li>an array of strings, which is returned without conversion</li>
+ * </ul>
+ */
+ public static String[] asStringArray(Object pValue) {
+ if (pValue == null) {
+ return null;
+ }
+ if (pValue instanceof String) {
+ return new String[]{(String) pValue};
+ }
+ if (pValue instanceof String[]) {
+ return (String[]) pValue;
+ }
+ if (pValue instanceof List) {
+ final List l = (List) pValue;
+ return (String[]) l.toArray(new String[l.size()]);
+ }
+ throw new IllegalStateException("Invalid parameter class: " + pValue.getClass().getName());
+ }
+
+ /**
+ * <p>Converts the given object into a string enumeration. The object may be either of:
+ * <ul>
+ * <li>a string, which is returned as an enumeration with one element</li>
+ * <li>a list of strings, which is being converted into a string enumeration</li>
+ * <li>an array of strings, which is being converted into a string enumeration</li>
+ * </ul>
+ */
+ public static Enumeration asStringEnum(final Object pValue) {
+ if (pValue == null) {
+ return null;
+ }
+ if (pValue instanceof String) {
+ return new Enumeration(){
+ private Object value = pValue;
+ public boolean hasMoreElements() {
+ return value != null;
+ }
+ public Object nextElement() {
+ if (value == null) {
+ throw new NoSuchElementException();
+ }
+ final String s = (String) value;
+ value = null;
+ return s;
+ }
+ };
+ }
+ if (pValue instanceof String[]) {
+ final String[] values = (String[]) pValue;
+ return new Enumeration() {
+ private int offset;
+ public boolean hasMoreElements() {
+ return offset < values.length;
+ }
+ public Object nextElement() {
+ if (offset >= values.length) {
+ throw new NoSuchElementException();
+ }
+ return values[offset++];
+ }
+ };
+ }
+ if (pValue instanceof List) {
+ return Collections.enumeration((List) pValue);
+ }
+ throw new IllegalStateException("Invalid parameter class: " + pValue.getClass().getName());
+ }
+
+ /**
+ * Converts the given map into a string array map: The map values
+ * are string arrays.
+ */
+ public static Map asMap(final Map pMap) {
+ for (Iterator iter = pMap.entrySet().iterator(); iter.hasNext(); ) {
+ final Map.Entry entry = (Map.Entry) iter.next();
+ final String[] value = asStringArray(entry.getValue());
+ entry.setValue(value);
+ }
+ return Collections.unmodifiableMap(pMap);
+ }
+
+ /**
+ * Adds a value to the given map.
+ */
+ protected void addMapValue(Map pMap, String pName, String pValue) {
+ Object o = pMap.get(pName);
+ if (o == null) {
+ o = pValue;
+ } else if (o instanceof String) {
+ final List list = new ArrayList();
+ list.add(o);
+ list.add(pValue);
+ o = list;
+ } else if (o instanceof List) {
+ ((List) o).add(pValue);
+ } else if (o instanceof String[]) {
+ final List list = new ArrayList();
+ final String[] arr = (String[]) o;
+ for (int i = 0; i < arr.length; i++) {
+ list.add(arr[i]);
+ }
+ list.add(pValue);
+ o = list;
+ } else {
+ throw new IllegalStateException("Invalid object type: " + o.getClass().getName());
+ }
+ pMap.put(pName, o);
+ }
+
+ /**
+ * Lower cases the given name.
+ */
+ protected String convertName(String pName) {
+ return pName.toLowerCase();
+ }
+
+ /**
+ * Returns the requested value.
+ */
+ public String getValue(String pName) {
+ return asString(map.get(convertName(pName)));
+ }
+
+ /**
+ * Returns the requested values as a string array.
+ */
+ public String[] getValues(String pName) {
+ return asStringArray(map.get(convertName(pName)));
+ }
+
+ /**
+ * Returns the requested values as an enumeration.
+ */
+ public Enumeration getValueEnum(String pName) {
+ return asStringEnum(map.get(convertName(pName)));
+ }
+
+ /**
+ * Returns the set of registered names as an enumeration.
+ * @see #getNameArray()
+ */
+ public Enumeration getNames() {
+ return Collections.enumeration(map.keySet());
+ }
+
+ /**
+ * Returns an unmodifiable map of name/value pairs. The map keys
+ * are the lower cased parameter/header names. The map values are
+ * string arrays.
+ */
+ public Map getMap() {
+ return asMap(map);
+ }
+
+ /**
+ * Adds a new name/value pair.
+ */
+ public void addValue(String pName, String pValue) {
+ addMapValue(map, convertName(pName), pValue);
+ }
+
+ /**
+ * Returns the set of registered names.
+ * @see #getNames()
+ */
+ public String[] getNameArray() {
+ final Collection c = map.keySet();
+ return (String[]) c.toArray(new String[c.size()]);
+ }
+}
Propchange: james/mime4j/trunk/src/main/java/org/apache/james/mime4j/util/StringArrayMap.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/StringArrayMapTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/StringArrayMapTest.java?rev=572440&view=auto
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/StringArrayMapTest.java (added)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/StringArrayMapTest.java Mon Sep 3 15:30:57 2007
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.james.mime4j.util;
+
+import java.text.Collator;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+
+/**
+ * Test case for {@link StringArray}.
+ */
+public class StringArrayMapTest extends TestCase {
+ private StringArrayMap getSampleMap() {
+ final StringArrayMap map = new StringArrayMap();
+ map.addValue("xYz", "a");
+ map.addValue("Xyz", "B");
+ map.addValue("xyz", "c");
+ map.addValue("xs", "1");
+ map.addValue("XS", "2");
+ map.addValue("foo", "bAr");
+ return map;
+ }
+
+ /**
+ * Test for {@link StringArrayMap#getMap()}.
+ */
+ public void testGetMap() {
+ final Map map = getSampleMap().getMap();
+ final List keys = new ArrayList(map.keySet());
+ assertEquals(keys.size(), 3);
+ Collections.sort(keys);
+ assertEquals("foo", keys.get(0));
+ assertEquals("xs", keys.get(1));
+ assertEquals("xyz", keys.get(2));
+ final String[] foo = (String[]) map.get("foo");
+ assertEquals("bAr", foo[0]);
+ final String[] xs = (String[]) map.get("xs");
+ assertEquals("1", xs[0]);
+ assertEquals("2", xs[1]);
+ final String[] xyz = (String[]) map.get("xyz");
+ assertEquals(3, xyz.length);
+ assertEquals("a", xyz[0]);
+ assertEquals("B", xyz[1]);
+ assertEquals("c", xyz[2]);
+ }
+
+ /**
+ * Test case for {@link StringArrayMap#getNameArray()}.
+ */
+ public void testGetNameArray() {
+ final String[] names = getSampleMap().getNameArray();
+ assertEquals(3, names.length);
+ Arrays.sort(names);
+ assertEquals("foo", names[0]);
+ assertEquals("xs", names[1]);
+ assertEquals("xyz", names[2]);
+ }
+
+ /**
+ * Test case for {@link StringArrayMap#getNames()}.
+ */
+ public void testGetNames() {
+ final Enumeration names = getSampleMap().getNames();
+ assertEquals(new String[]{"foo", "xs", "xyz"}, names);
+ }
+
+ /**
+ * Test case for {@link StringArrayMap#getValue(String)}.
+ */
+ public void testGetValue() {
+ final StringArrayMap map = getSampleMap();
+ assertEquals("bAr", map.getValue("foo"));
+ assertEquals("bAr", map.getValue("FOO"));
+ assertEquals("1", map.getValue("xs"));
+ assertEquals("a", map.getValue("xyz"));
+ assertEquals("a", map.getValue("xyZ"));
+ assertNull(map.getValue("xz"));
+ }
+
+ /**
+ * Test case for {@link StringArrayMap#getValue(String)}.
+ */
+ public void testGetValues() {
+ final StringArrayMap map = getSampleMap();
+ final String[] foo = map.getValues("foo");
+ assertEquals(1, foo.length);
+ assertEquals("bAr", foo[0]);
+ final String[] FOO = map.getValues("FOO");
+ assertEquals(1, FOO.length);
+ assertEquals("bAr", FOO[0]);
+ final String[] xs = map.getValues("xs");
+ assertEquals(2, xs.length);
+ assertEquals("1", xs[0]);
+ assertEquals("2", xs[1]);
+ final String[] XS = map.getValues("XS");
+ assertEquals(2, XS.length);
+ assertEquals("1", XS[0]);
+ assertEquals("2", XS[1]);
+ final String[] xyz = map.getValues("xyz");
+ assertEquals("a", xyz[0]);
+ assertEquals("B", xyz[1]);
+ assertEquals("c", xyz[2]);
+ final String[] XYZ = map.getValues("XYZ");
+ assertEquals("a", XYZ[0]);
+ assertEquals("B", XYZ[1]);
+ assertEquals("c", XYZ[2]);
+ assertNull(map.getValues("xz"));
+ }
+
+ private void assertEquals(String[] pArray, Enumeration pEnum) {
+ final List list = new ArrayList();
+ while (pEnum.hasMoreElements()) {
+ list.add(pEnum.nextElement());
+ }
+ Collections.sort(list, Collator.getInstance(Locale.US));
+ assertEquals(pArray.length, list.size());
+ for (int i = 0; i < pArray.length; i++) {
+ assertEquals(pArray[i], list.get(i));
+ }
+ }
+
+ /**
+ * Test case for {@link StringArrayMap#getValueEnum(String)}.
+ */
+ public void testGetValueEnum() {
+ final StringArrayMap map = getSampleMap();
+ assertEquals(new String[]{"bAr"}, map.getValueEnum("foo"));
+ assertEquals(new String[]{"bAr"}, map.getValueEnum("FOO"));
+ assertEquals(new String[]{"1", "2"}, map.getValueEnum("xs"));
+ assertEquals(new String[]{"1", "2"}, map.getValueEnum("Xs"));
+ assertEquals(new String[]{"a", "B", "c"}, map.getValueEnum("xyz"));
+ assertEquals(new String[]{"a", "B", "c"}, map.getValueEnum("XYZ"));
+ assertNull(map.getValues("xz"));
+ }
+}
Propchange: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/StringArrayMapTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org