james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1617739 - in /james/mime4j/trunk/dom/src: main/java/org/apache/james/mime4j/dom/ main/java/org/apache/james/mime4j/message/ test/java/org/apache/james/mime4j/message/
Date Wed, 13 Aug 2014 15:19:29 GMT
Author: olegk
Date: Wed Aug 13 15:19:29 2014
New Revision: 1617739

URL: http://svn.apache.org/r1617739
Log:
Added typed #getField methods

Modified:
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/Header.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/AbstractEntityBuilder.java
    james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/AbstractHeader.java
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/AbstractEntityBuilderTest.java
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/BodyPartBuilderTest.java

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/Header.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/Header.java?rev=1617739&r1=1617738&r2=1617739&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/Header.java (original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/dom/Header.java Wed Aug 13
15:19:29 2014
@@ -54,6 +54,17 @@ public interface Header extends Iterable
     Field getField(String name);
 
     /**
+     * Gets a <code>Field</code> given a field name and of the given type.
+     * If there are multiple such fields defined in this header the first
+     * one will be returned.
+     *
+     * @param name the field name (e.g. From, Subject).
+     * @param clazz the field class.
+     * @return the field or <code>null</code> if none found.
+     */
+    <F extends Field> F getField(String name, Class<F> clazz);
+
+    /**
      * Gets all <code>Field</code>s having the specified field name.
      *
      * @param name the field name (e.g. From, Subject).
@@ -62,6 +73,16 @@ public interface Header extends Iterable
     List<Field> getFields(final String name);
 
     /**
+     * Gets all <code>Field</code>s having the specified field name
+     * and of the given type.
+     *
+     * @param name the field name (e.g. From, Subject).
+     * @param clazz the field class.
+     * @return the list of fields.
+     */
+    <F extends Field> List<F> getFields(final String name, Class<F> clazz);
+
+    /**
      * Returns an iterator over the list of fields of this header.
      *
      * @return an iterator.

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/AbstractEntityBuilder.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/AbstractEntityBuilder.java?rev=1617739&r1=1617738&r2=1617739&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/AbstractEntityBuilder.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/AbstractEntityBuilder.java
Wed Aug 13 15:19:29 2014
@@ -19,6 +19,7 @@
 
 package org.apache.james.mime4j.message;
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
@@ -97,6 +98,26 @@ abstract class AbstractEntityBuilder {
     }
 
     /**
+     * Gets a <code>Field</code> given a field name and of the given type.
+     * If there are multiple such fields defined in this header the first
+     * one will be returned.
+     *
+     * @param name the field name (e.g. From, Subject).
+     * @param clazz the field class.
+     * @return the field or <code>null</code> if none found.
+     */
+    public <F extends Field> F getField(final String name, final Class<F> clazz)
{
+        List<Field> l = fieldMap.get(name.toLowerCase(Locale.US));
+        for (int i = 0; i < l.size(); i++) {
+            Field field = l.get(i);
+            if (clazz.isInstance(field)) {
+                return clazz.cast(field);
+            }
+        }
+        return null;
+    }
+
+    /**
      * Returns <code>true<code/> if there is at least one explicitly
      * set field with the given name.
      *
@@ -128,6 +149,30 @@ abstract class AbstractEntityBuilder {
     }
 
     /**
+     * Gets all <code>Field</code>s having the specified field name
+     * and of the given type.
+     *
+     * @param name the field name (e.g. From, Subject).
+     * @param clazz the field class.
+     * @return the list of fields.
+     */
+    public <F extends Field> List<F> getFields(final String name, final Class<F>
clazz) {
+        final String lowerCaseName = name.toLowerCase(Locale.US);
+        final List<Field> l = fieldMap.get(lowerCaseName);
+        if (l == null) {
+            return Collections.emptyList();
+        }
+        final List<F> results = new ArrayList<F>();
+        for (int i = 0; i < l.size(); i++) {
+            Field field = l.get(i);
+            if (clazz.isInstance(field)) {
+                results.add(clazz.cast(field));
+            }
+        }
+        return results;
+    }
+
+    /**
      * Removes all <code>Field</code>s having the specified field name.
      *
      * @param name

Modified: james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/AbstractHeader.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/AbstractHeader.java?rev=1617739&r1=1617738&r2=1617739&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/AbstractHeader.java
(original)
+++ james/mime4j/trunk/dom/src/main/java/org/apache/james/mime4j/message/AbstractHeader.java
Wed Aug 13 15:19:29 2014
@@ -19,11 +19,13 @@
 
 package org.apache.james.mime4j.message;
 
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
 import org.apache.james.mime4j.dom.Header;
@@ -65,10 +67,10 @@ public abstract class AbstractHeader imp
      * @param field the field to add.
      */
     public void addField(Field field) {
-        List<Field> values = fieldMap.get(field.getName().toLowerCase());
+        List<Field> values = fieldMap.get(field.getName().toLowerCase(Locale.US));
         if (values == null) {
             values = new LinkedList<Field>();
-            fieldMap.put(field.getName().toLowerCase(), values);
+            fieldMap.put(field.getName().toLowerCase(Locale.US), values);
         }
         values.add(field);
         fields.add(field);
@@ -92,7 +94,7 @@ public abstract class AbstractHeader imp
      * @return the field or <code>null</code> if none found.
      */
     public Field getField(String name) {
-        List<Field> l = fieldMap.get(name.toLowerCase());
+        List<Field> l = fieldMap.get(name.toLowerCase(Locale.US));
         if (l != null && !l.isEmpty()) {
             return l.get(0);
         }
@@ -100,13 +102,33 @@ public abstract class AbstractHeader imp
     }
 
     /**
+     * Gets a <code>Field</code> given a field name and of the given type.
+     * If there are multiple such fields defined in this header the first
+     * one will be returned.
+     *
+     * @param name the field name (e.g. From, Subject).
+     * @param clazz the field class.
+     * @return the field or <code>null</code> if none found.
+     */
+    public <F extends Field> F getField(final String name, final Class<F> clazz)
{
+        List<Field> l = fieldMap.get(name.toLowerCase(Locale.US));
+        for (int i = 0; i < l.size(); i++) {
+            Field field = l.get(i);
+            if (clazz.isInstance(field)) {
+                return clazz.cast(field);
+            }
+        }
+        return null;
+    }
+
+    /**
      * Gets all <code>Field</code>s having the specified field name.
      *
      * @param name the field name (e.g. From, Subject).
      * @return the list of fields.
      */
     public List<Field> getFields(final String name) {
-        final String lowerCaseName = name.toLowerCase();
+        final String lowerCaseName = name.toLowerCase(Locale.US);
         final List<Field> l = fieldMap.get(lowerCaseName);
         final List<Field> results;
         if (l == null || l.isEmpty()) {
@@ -118,6 +140,30 @@ public abstract class AbstractHeader imp
     }
 
     /**
+     * Gets all <code>Field</code>s having the specified field name
+     * and of the given type.
+     *
+     * @param name the field name (e.g. From, Subject).
+     * @param clazz the field class.
+     * @return the list of fields.
+     */
+    public <F extends Field> List<F> getFields(final String name, final Class<F>
clazz) {
+        final String lowerCaseName = name.toLowerCase(Locale.US);
+        final List<Field> l = fieldMap.get(lowerCaseName);
+        if (l == null) {
+            return Collections.emptyList();
+        }
+        final List<F> results = new ArrayList<F>();
+        for (int i = 0; i < l.size(); i++) {
+            Field field = l.get(i);
+            if (clazz.isInstance(field)) {
+                results.add(clazz.cast(field));
+            }
+        }
+        return results;
+    }
+
+    /**
      * Returns an iterator over the list of fields of this header.
      *
      * @return an iterator.
@@ -134,7 +180,7 @@ public abstract class AbstractHeader imp
      * @return number of fields removed.
      */
     public int removeFields(String name) {
-        final String lowerCaseName = name.toLowerCase();
+        final String lowerCaseName = name.toLowerCase(Locale.US);
         List<Field> removed = fieldMap.remove(lowerCaseName);
         if (removed == null || removed.isEmpty())
             return 0;
@@ -161,7 +207,7 @@ public abstract class AbstractHeader imp
      * @param field the field to set.
      */
     public void setField(Field field) {
-        final String lowerCaseName = field.getName().toLowerCase();
+        final String lowerCaseName = field.getName().toLowerCase(Locale.US);
         List<Field> l = fieldMap.get(lowerCaseName);
         if (l == null || l.isEmpty()) {
             addField(field);

Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/AbstractEntityBuilderTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/AbstractEntityBuilderTest.java?rev=1617739&r1=1617738&r2=1617739&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/AbstractEntityBuilderTest.java
(original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/AbstractEntityBuilderTest.java
Wed Aug 13 15:19:29 2014
@@ -29,6 +29,7 @@ import org.apache.james.mime4j.dom.Binar
 import org.apache.james.mime4j.dom.Message;
 import org.apache.james.mime4j.dom.Multipart;
 import org.apache.james.mime4j.dom.TextBody;
+import org.apache.james.mime4j.dom.field.ContentTransferEncodingField;
 import org.apache.james.mime4j.dom.field.ContentTypeField;
 import org.apache.james.mime4j.dom.field.ParsedField;
 import org.apache.james.mime4j.field.DefaultFieldParser;
@@ -85,20 +86,27 @@ public class AbstractEntityBuilderTest {
         builder.addField(field3);
 
         Assert.assertSame(field2, builder.getField("Content-Transfer-Encoding"));
+        Assert.assertSame(field2, builder.getField("Content-Transfer-Encoding", ContentTransferEncodingField.class));
         List<Field> fields5 = builder.getFields("Content-Transfer-Encoding");
         Assert.assertNotNull(fields5);
         Assert.assertEquals(2, fields5.size());
         Assert.assertSame(field2, fields5.get(0));
         Assert.assertSame(field3, fields5.get(1));
 
+        List<ContentTransferEncodingField> fields6 = builder.getFields("Content-Transfer-Encoding",
ContentTransferEncodingField.class);
+        Assert.assertNotNull(fields6);
+        Assert.assertEquals(2, fields6.size());
+        Assert.assertSame(field2, fields6.get(0));
+        Assert.assertSame(field3, fields6.get(1));
+
         builder.clearFields();
 
         Assert.assertFalse(builder.containsField("Content-Type"));
         Assert.assertFalse(builder.containsField("Content-Transfer-Encoding"));
 
-        List<Field> fields6 = builder.getFields();
-        Assert.assertNotNull(fields6);
-        Assert.assertEquals(0, fields6.size());
+        List<Field> fields7 = builder.getFields();
+        Assert.assertNotNull(fields7);
+        Assert.assertEquals(0, fields7.size());
     }
 
     @Test

Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/BodyPartBuilderTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/BodyPartBuilderTest.java?rev=1617739&r1=1617738&r2=1617739&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/BodyPartBuilderTest.java
(original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/BodyPartBuilderTest.java
Wed Aug 13 15:19:29 2014
@@ -69,7 +69,7 @@ public class BodyPartBuilderTest {
         Assert.assertNotNull(bodyPart);
         Body body = bodyPart.getBody();
         Assert.assertSame(bodyPart, body.getParent());
-        ContentTypeField field = (ContentTypeField) bodyPart.getHeader().getField("Content-Type");
+        ContentTypeField field = bodyPart.getHeader().getField("Content-Type", ContentTypeField.class);
         Assert.assertEquals("text/stuff", field.getMimeType());
         Assert.assertEquals(null, field.getCharset());
 
@@ -99,7 +99,7 @@ public class BodyPartBuilderTest {
         Assert.assertNotNull(bodyPart);
         Body body = bodyPart.getBody();
         Assert.assertSame(bodyPart, body.getParent());
-        ContentTypeField field = (ContentTypeField) bodyPart.getHeader().getField("Content-Type");
+        ContentTypeField field = bodyPart.getHeader().getField("Content-Type", ContentTypeField.class);
         Assert.assertEquals("application/octet-stream", field.getMimeType());
         Assert.assertEquals(null, field.getCharset());
 



Mime
View raw message