james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From matth...@apache.org
Subject svn commit: r1711993 - in /james/project/trunk/server/data: data-api/src/main/java/org/apache/james/rrt/lib/ data-library/src/main/java/org/apache/james/rrt/lib/ data-library/src/test/java/org/apache/james/rrt/lib/
Date Mon, 02 Nov 2015 13:48:35 GMT
Author: matthieu
Date: Mon Nov  2 13:48:34 2015
New Revision: 1711993

URL: http://svn.apache.org/viewvc?rev=1711993&view=rev
Log:
JAMES-1595 add select/contains/exclude methods to Mappings

Modified:
    james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mappings.java
    james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
    james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingsImplTest.java

Modified: james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mappings.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mappings.java?rev=1711993&r1=1711992&r2=1711993&view=diff
==============================================================================
--- james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mappings.java
(original)
+++ james/project/trunk/server/data/data-api/src/main/java/org/apache/james/rrt/lib/Mappings.java
Mon Nov  2 13:48:34 2015
@@ -20,6 +20,7 @@
 
 package org.apache.james.rrt.lib;
 
+import org.apache.james.rrt.lib.Mapping.Type;
 
 public interface Mappings extends Iterable<Mapping> {
 
@@ -34,5 +35,11 @@ public interface Mappings extends Iterab
     Iterable<String> asStrings();
     
     String serialize();
+
+    boolean contains(Type type);
+    
+    Mappings select(Type type);
+
+    Mappings exclude(Type type);
     
 }
\ No newline at end of file

Modified: james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java?rev=1711993&r1=1711992&r2=1711993&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
(original)
+++ james/project/trunk/server/data/data-library/src/main/java/org/apache/james/rrt/lib/MappingsImpl.java
Mon Nov  2 13:48:34 2015
@@ -25,8 +25,14 @@ import java.util.Collection;
 import java.util.Iterator;
 import java.util.StringTokenizer;
 
+import org.apache.james.rrt.lib.Mapping.Type;
+
 import com.google.common.base.Function;
 import com.google.common.base.Joiner;
+import com.google.common.base.Objects;
+import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
+import com.google.common.base.Predicates;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
@@ -59,6 +65,14 @@ public class MappingsImpl implements Map
         return builder.build();
     }
     
+    public static MappingsImpl fromMappings(Iterable<Mapping> mappings) {
+        Builder builder = builder();
+        for (Mapping mapping: mappings) {
+            builder.add(mapping);
+        }
+        return builder.build();
+    }
+    
     public static Builder from(Mappings from) {
         Builder builder = new Builder();
         builder.addAll(from);
@@ -78,10 +92,15 @@ public class MappingsImpl implements Map
         }
 
         public Builder add(String mapping) {
-            mappings.add(MappingImpl.of(mapping));
+            return add(MappingImpl.of(mapping));
+        }
+
+        public Builder add(Mapping mapping) {
+            mappings.add(mapping);
             return this;
         }
 
+        
         public Builder addAll(Mappings mappings) {
             this.mappings.addAll(mappings);
             return this;
@@ -144,5 +163,53 @@ public class MappingsImpl implements Map
     public String serialize() {
         return Joiner.on(';').join(asStrings());
     }
+    
+    private Predicate<Mapping> hasType(final Mapping.Type type) {
+        return new Predicate<Mapping>() {
+            @Override
+            public boolean apply(Mapping input) {
+                return input.getType().equals(type);
+            }
+        };
+    }
+    
+    @Override
+    public boolean contains(Type type) {
+        Preconditions.checkNotNull(type);
+        return FluentIterable.from(mappings).anyMatch(hasType(type));
+    }
+    
+    @Override
+    public Mappings select(Type type) {
+        Preconditions.checkNotNull(type);
+        return fromMappings(FluentIterable.from(mappings).filter(hasType(type)));
+    }
+    
+    
+    @Override
+    public Mappings exclude(Type type) {
+        Preconditions.checkNotNull(type);
+        return fromMappings(FluentIterable.from(mappings).filter(Predicates.not(hasType(type))));
+    }
+    
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(mappings);
+    }
+    
+    @Override
+    public boolean equals(Object obj) {
+        if (obj instanceof MappingsImpl) {
+            MappingsImpl other = (MappingsImpl) obj;
+            return Objects.equal(mappings, other.mappings);
+        }
+        return false;
+    }
+    
+    @Override
+    public String toString() {
+        return Objects.toStringHelper(getClass()).add("mappings", mappings).toString();
+    }
+    
 
 }
\ No newline at end of file

Modified: james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingsImplTest.java
URL: http://svn.apache.org/viewvc/james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingsImplTest.java?rev=1711993&r1=1711992&r2=1711993&view=diff
==============================================================================
--- james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingsImplTest.java
(original)
+++ james/project/trunk/server/data/data-library/src/test/java/org/apache/james/rrt/lib/MappingsImplTest.java
Mon Nov  2 13:48:34 2015
@@ -41,82 +41,82 @@ public class MappingsImplTest {
     @Test
     public void fromRawStringShouldReturnSingletonCollectionWhenSingleElementString() {
         MappingsImpl actual = MappingsImpl.fromRawString("value");
-        assertThat(actual).containsExactly(MappingImpl.of("value"));
+        assertThat(actual).containsExactly(MappingImpl.address("value"));
     }
 
     @Test
     public void fromRawStringShouldReturnCollectionWhenSeveralElementsString() {
         MappingsImpl actual = MappingsImpl.fromRawString("value1;value2");
-        assertThat(actual).containsExactly(MappingImpl.of("value1"), MappingImpl.of("value2"));
+        assertThat(actual).containsExactly(MappingImpl.address("value1"), MappingImpl.address("value2"));
     }
     
     @Test
     public void fromRawStringShouldReturnSingleElementCollectionWhenTrailingDelimiterString()
{
         MappingsImpl actual = MappingsImpl.fromRawString("value1;");
-        assertThat(actual).containsExactly(MappingImpl.of("value1"));
+        assertThat(actual).containsExactly(MappingImpl.address("value1"));
     }
 
     @Test
     public void fromRawStringShouldReturnSingleElementCollectionWhenHeadingDelimiterString()
{
         MappingsImpl actual = MappingsImpl.fromRawString(";value1");
-        assertThat(actual).containsExactly(MappingImpl.of("value1"));
+        assertThat(actual).containsExactly(MappingImpl.address("value1"));
     }
     
 
     @Test
     public void fromRawStringShouldTrimValues() {
         MappingsImpl actual = MappingsImpl.fromRawString("value1 ; value2  ");
-        assertThat(actual).containsExactly(MappingImpl.of("value1"), MappingImpl.of("value2"));
+        assertThat(actual).containsExactly(MappingImpl.address("value1"), MappingImpl.address("value2"));
     }
     
     @Test
     public void fromRawStringShouldNotSkipEmptyValue() {
         MappingsImpl actual = MappingsImpl.fromRawString("value1; ;value2");
-        assertThat(actual).containsExactly(MappingImpl.of("value1"), MappingImpl.of(""),
MappingImpl.of("value2"));
+        assertThat(actual).containsExactly(MappingImpl.address("value1"), MappingImpl.address(""),
MappingImpl.address("value2"));
     }
     
     @Test
     public void fromRawStringShouldReturnCollectionWhenValueContainsCommaSeperatedValues()
{
         MappingsImpl actual = MappingsImpl.fromRawString("value1,value2");
-        assertThat(actual).containsExactly(MappingImpl.of("value1"),MappingImpl.of("value2"));
+        assertThat(actual).containsExactly(MappingImpl.address("value1"),MappingImpl.address("value2"));
     }
 
     @Test
     public void fromRawStringShouldReturnCollectionWhenValueContainsColonSeperatedValues()
{
         MappingsImpl actual = MappingsImpl.fromRawString("value1:value2");
-        assertThat(actual).containsExactly(MappingImpl.of("value1"),MappingImpl.of("value2"));
+        assertThat(actual).containsExactly(MappingImpl.address("value1"),MappingImpl.address("value2"));
     }
 
     @Test
     public void fromRawStringShouldUseCommaDelimiterBeforeSemicolonWhenValueContainsBoth()
{
         MappingsImpl actual = MappingsImpl.fromRawString("value1;value1,value2");
-        assertThat(actual).containsExactly(MappingImpl.of("value1;value1"),MappingImpl.of("value2"));
+        assertThat(actual).containsExactly(MappingImpl.address("value1;value1"),MappingImpl.address("value2"));
     }
 
     @Test
     public void fromRawStringShouldUseSemicolonDelimiterBeforeColonWhenValueContainsBoth()
{
         MappingsImpl actual = MappingsImpl.fromRawString("value1:value1;value2");
-        assertThat(actual).containsExactly(MappingImpl.of("value1:value1"),MappingImpl.of("value2"));
+        assertThat(actual).containsExactly(MappingImpl.address("value1:value1"),MappingImpl.address("value2"));
     }
     
     @Test
     public void fromRawStringShouldNotUseColonDelimiterWhenValueStartsWithError() {
         MappingsImpl actual = MappingsImpl.fromRawString("error:test");
-        assertThat(actual).containsExactly(MappingImpl.of("error:test"));
+        assertThat(actual).containsExactly(MappingImpl.error("test"));
     }
     
 
     @Test
     public void fromRawStringShouldNotUseColonDelimiterWhenValueStartsWithDomain() {
         MappingsImpl actual = MappingsImpl.fromRawString("domain:test");
-        assertThat(actual).containsExactly(MappingImpl.of("domain:test"));
+        assertThat(actual).containsExactly(MappingImpl.domain("test"));
     }
     
 
     @Test
     public void fromRawStringShouldNotUseColonDelimiterWhenValueStartsWithRegex() {
         MappingsImpl actual = MappingsImpl.fromRawString("regex:test");
-        assertThat(actual).containsExactly(MappingImpl.of("regex:test"));
+        assertThat(actual).containsExactly(MappingImpl.regex("test"));
     }
 
     @Test
@@ -136,5 +136,85 @@ public class MappingsImplTest {
         assertThat(actual).isEqualTo("value1;value2");
     }
 
+    @Test
+    public void containsShouldReturnTrueWhenMatchingMapping() {
+        MappingsImpl mappings = MappingsImpl.builder().add(MappingImpl.regex("toto")).build();
+        assertThat(mappings.contains(Mapping.Type.Regex)).isTrue();
+    }
     
+    @Test
+    public void containsShouldReturnFalseWhenNoMatchingMapping() {
+        MappingsImpl mappings = MappingsImpl.builder().add(MappingImpl.regex("toto")).build();
+        assertThat(mappings.contains(Mapping.Type.Error)).isFalse();
+    }
+
+    
+    @Test(expected=NullPointerException.class)
+    public void containsShouldThrowWhenNull() {
+        MappingsImpl mappings = MappingsImpl.builder().add(MappingImpl.regex("toto")).build();
+        assertThat(mappings.contains((Mapping.Type)null));
+    }
+    
+    @Test
+    public void selectShouldReturnMatchingElementsInOrderWhenMatchingMapping() {
+        MappingsImpl mappings = MappingsImpl.builder()
+                .add(MappingImpl.regex("toto"))
+                .add(MappingImpl.address("toto")) 
+                .add(MappingImpl.domain("domain"))
+                .add(MappingImpl.regex("tata"))
+                .build();
+        MappingsImpl expected = MappingsImpl.builder()
+                .add(MappingImpl.regex("toto"))
+                .add(MappingImpl.regex("tata"))
+                .build();
+        assertThat(mappings.select(Mapping.Type.Regex)).isEqualTo(expected);
+    }
+    
+    @Test
+    public void selectShouldReturnEmptyCollectionWhenNoMatchingMapping() {
+        MappingsImpl mappings = MappingsImpl.builder()
+                .add(MappingImpl.regex("toto"))
+                .add(MappingImpl.address("toto")) 
+                .add(MappingImpl.address("tata"))
+                .build();
+        assertThat(mappings.select(Mapping.Type.Domain)).isEqualTo(MappingsImpl.empty());
+    }
+
+    
+    @Test(expected=NullPointerException.class)
+    public void selectShouldThrowWhenNull() {
+        MappingsImpl mappings = MappingsImpl.builder().add(MappingImpl.regex("toto")).build();
+        assertThat(mappings.select((Mapping.Type)null));
+    }
+
+    @Test
+    public void excludeShouldReturnNonMatchingElementsInOrderWhenNonMatchingMapping() {
+        MappingsImpl mappings = MappingsImpl.builder()
+                .add(MappingImpl.regex("toto"))
+                .add(MappingImpl.address("toto")) 
+                .add(MappingImpl.domain("domain"))
+                .add(MappingImpl.regex("tata"))
+                .build();
+        MappingsImpl expected = MappingsImpl.builder()
+                .add(MappingImpl.address("toto")) 
+                .add(MappingImpl.domain("domain"))
+                .build();
+        assertThat(mappings.exclude(Mapping.Type.Regex)).isEqualTo(expected);
+    }
+    
+    @Test
+    public void excludeShouldReturnEmptyCollectionWhenOnlyMatchingMapping() {
+        MappingsImpl mappings = MappingsImpl.builder()
+                .add(MappingImpl.address("toto")) 
+                .add(MappingImpl.address("tata"))
+                .build();
+        assertThat(mappings.exclude(Mapping.Type.Address)).isEqualTo(MappingsImpl.empty());
+    }
+
+    
+    @Test(expected=NullPointerException.class)
+    public void excludeShouldThrowWhenNull() {
+        MappingsImpl mappings = MappingsImpl.builder().add(MappingImpl.regex("toto")).build();
+        assertThat(mappings.exclude((Mapping.Type)null));
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message