james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [02/12] james-project git commit: JAMES-2161 Keyword should rely on factory method
Date Tue, 27 Nov 2018 02:06:29 GMT
JAMES-2161 Keyword should rely on factory method


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/20a15278
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/20a15278
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/20a15278

Branch: refs/heads/master
Commit: 20a15278c4d9ccf2c0e0b1ada30a009fe07f414d
Parents: 92a4182
Author: Benoit Tellier <btellier@linagora.com>
Authored: Fri Nov 23 10:04:03 2018 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Tue Nov 27 09:01:33 2018 +0700

----------------------------------------------------------------------
 .../james/jmap/model/FilterCondition.java       |  4 +-
 .../org/apache/james/jmap/model/Keyword.java    | 47 ++++++++++++--------
 .../org/apache/james/jmap/model/Keywords.java   |  6 +--
 .../james/jmap/utils/FilterToSearchQuery.java   |  2 +-
 .../apache/james/jmap/model/KeywordTest.java    | 40 ++++++++---------
 .../apache/james/jmap/model/KeywordsTest.java   |  2 +-
 .../james/jmap/model/MessageFactoryTest.java    |  2 +-
 .../james/jmap/utils/KeywordsCombinerTest.java  | 10 ++---
 8 files changed, 62 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/20a15278/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/FilterCondition.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/FilterCondition.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/FilterCondition.java
index f6b89aa..3d35828 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/FilterCondition.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/FilterCondition.java
@@ -212,8 +212,8 @@ public class FilterCondition implements Filter {
         }
 
         public FilterCondition build() {
-            Preconditions.checkArgument(!hasKeyword.isPresent() || (new Keyword(hasKeyword.get())
!= null), "hasKeyword is not valid");
-            Preconditions.checkArgument(!notKeyword.isPresent() || (new Keyword(notKeyword.get())
!= null), "notKeyword is not valid");
+            Preconditions.checkArgument(!hasKeyword.isPresent() || (Keyword.of(hasKeyword.get())
!= null), "hasKeyword is not valid");
+            Preconditions.checkArgument(!notKeyword.isPresent() || (Keyword.of(notKeyword.get())
!= null), "notKeyword is not valid");
             return new FilterCondition(inMailboxes, notInMailboxes, Optional.ofNullable(before),
Optional.ofNullable(after), Optional.ofNullable(minSize), Optional.ofNullable(maxSize),
                     Optional.ofNullable(isFlagged), Optional.ofNullable(isUnread), Optional.ofNullable(isAnswered),
Optional.ofNullable(isDraft), Optional.ofNullable(isForwarded),
                     Optional.ofNullable(hasAttachment),

http://git-wip-us.apache.org/repos/asf/james-project/blob/20a15278/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Keyword.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Keyword.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Keyword.java
index 8d0f76c..40b46d2 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Keyword.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Keyword.java
@@ -28,7 +28,6 @@ import org.apache.james.util.UnicodeSetUtils;
 
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Objects;
-import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableBiMap;
 import com.google.common.collect.ImmutableList;
 import com.ibm.icu.text.UnicodeSet;
@@ -43,14 +42,14 @@ public class Keyword {
                 .add('-')
                 .freeze();
 
-    public static final Keyword DRAFT = new Keyword("$Draft");
-    public static final Keyword SEEN = new Keyword("$Seen");
-    public static final Keyword FLAGGED = new Keyword("$Flagged");
-    public static final Keyword ANSWERED = new Keyword("$Answered");
-    public static final Keyword DELETED = new Keyword("$Deleted");
-    public static final Keyword RECENT = new Keyword("$Recent");
-    public static final Keyword FORWARDED = new Keyword("$Forwarded");
-    public static final Boolean FLAG_VALUE = true;
+    public static final Keyword DRAFT = Keyword.of("$Draft");
+    public static final Keyword SEEN = Keyword.of("$Seen");
+    public static final Keyword FLAGGED = Keyword.of("$Flagged");
+    public static final Keyword ANSWERED = Keyword.of("$Answered");
+    public static final Keyword DELETED = Keyword.of("$Deleted");
+    public static final Keyword RECENT = Keyword.of("$Recent");
+    public static final Keyword FORWARDED = Keyword.of("$Forwarded");
+    static final Boolean FLAG_VALUE = true;
 
     private static final ImmutableList<Keyword> NON_EXPOSED_IMAP_KEYWORDS = ImmutableList.of(Keyword.RECENT,
Keyword.DELETED);
     private static final ImmutableBiMap<Flags.Flag, Keyword> IMAP_SYSTEM_FLAGS = ImmutableBiMap.<Flags.Flag,
Keyword>builder()
@@ -61,21 +60,27 @@ public class Keyword {
         .put(Flags.Flag.RECENT, RECENT)
         .put(Flags.Flag.DELETED, DELETED)
         .build();
+    public static final String VALIDATION_MESSAGE = "Flagname must not be null or empty,
must have length form 1-255, " +
+        "must not contain charater with hex from '\\u0000' to '\\u00019' or {'(' ')' '{'
']' '%' '*' '\"' '\\'} ";
 
-    private final String flagName;
+    public static Optional<Keyword> parse(String flagName) {
+        if (isValid(flagName)) {
+            return Optional.of(new Keyword(flagName));
+        }
+        return Optional.empty();
+    }
 
-    public static Keyword fromFlag(Flags.Flag flag) {
-        return IMAP_SYSTEM_FLAGS.get(flag);
+
+    public static Keyword of(String flagName) {
+        return parse(flagName)
+            .orElseThrow(() -> new IllegalArgumentException(VALIDATION_MESSAGE));
     }
 
-    public Keyword(String flagName) {
-        Preconditions.checkArgument(isValid(flagName),
-                "Flagname must not be null or empty, must have length form 1-255, " +
-                    "must not contain charater with hex from '\\u0000' to '\\u00019' or {'('
')' '{' ']' '%' '*' '\"' '\\'} ");
-        this.flagName = flagName;
+    public static Keyword fromFlag(Flags.Flag flag) {
+        return IMAP_SYSTEM_FLAGS.get(flag);
     }
 
-    private boolean isValid(String flagName) {
+    private static boolean isValid(String flagName) {
         if (StringUtils.isBlank(flagName)) {
             return false;
         }
@@ -88,6 +93,12 @@ public class Keyword {
         return true;
     }
 
+    private final String flagName;
+
+    public Keyword(String flagName) {
+        this.flagName = flagName;
+    }
+
     public String getFlagName() {
         return flagName;
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/20a15278/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Keywords.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Keywords.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Keywords.java
index 1f100c1..5ef8091 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Keywords.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/model/Keywords.java
@@ -94,7 +94,7 @@ public class Keywords {
 
         public Keywords fromList(List<String> keywords) {
             return fromSet(keywords.stream()
-                    .map(Keyword::new)
+                    .map(Keyword::of)
                     .collect(Guavate.toImmutableSet()));
         }
 
@@ -105,7 +105,7 @@ public class Keywords {
                 .allMatch(keywordValue -> keywordValue), "Keyword must be true");
             Set<Keyword> setKeywords = mapKeywords.keySet()
                 .stream()
-                .map(Keyword::new)
+                .map(Keyword::of)
                 .collect(Guavate.toImmutableSet());
 
             return fromSet(setKeywords);
@@ -122,7 +122,7 @@ public class Keywords {
 
         private Stream<Keyword> asKeyword(String flagName) {
             try {
-                return Stream.of(new Keyword(flagName));
+                return Stream.of(Keyword.of(flagName));
             } catch (IllegalArgumentException e) {
                 LOGGER.warn("Fail to parse {} flag", flagName, e);
                 return Stream.of();

http://git-wip-us.apache.org/repos/asf/james-project/blob/20a15278/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/FilterToSearchQuery.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/FilterToSearchQuery.java
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/FilterToSearchQuery.java
index ac5b97e..d3bb696 100644
--- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/FilterToSearchQuery.java
+++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/FilterToSearchQuery.java
@@ -89,7 +89,7 @@ public class FilterToSearchQuery {
     }
 
     private Optional<Criterion> keywordQuery(String stringKeyword, boolean isSet) {
-        Keyword keyword = new Keyword(stringKeyword);
+        Keyword keyword = Keyword.of(stringKeyword);
         if (keyword.isExposedImapKeyword()) {
             return Optional.of(getFlagCriterion(keyword, isSet));
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/20a15278/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/KeywordTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/KeywordTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/KeywordTest.java
index a729205..2d1e857 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/KeywordTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/KeywordTest.java
@@ -48,19 +48,19 @@ public class KeywordTest {
     @Test
     public void keywordShouldThrowWhenFlagNameLengthLessThanMinLength() throws Exception
{
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword("");
+        Keyword.of("");
     }
 
     @Test
     public void keywordShouldThrowWhenFlagNameLengthMoreThanMaxLength() throws Exception
{
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword(StringUtils.repeat("a", FLAG_NAME_MAX_LENTH + 1));
+        Keyword.of(StringUtils.repeat("a", FLAG_NAME_MAX_LENTH + 1));
     }
 
     @Test
     public void keywordShouldCreateNewOneWhenFlagNameLengthEqualsMaxLength() throws Exception
{
         String maxLengthFlagName = StringUtils.repeat("a", FLAG_NAME_MAX_LENTH);
-        Keyword keyword = new Keyword(maxLengthFlagName);
+        Keyword keyword = Keyword.of(maxLengthFlagName);
 
         assertThat(keyword.getFlagName()).isEqualTo(maxLengthFlagName);
     }
@@ -68,7 +68,7 @@ public class KeywordTest {
     @Test
     public void keywordShouldCreateNewOneWhenFlagNameLengthEqualsMinLength() throws Exception
{
         String minLengthFlagName = "a";
-        Keyword keyword = new Keyword(minLengthFlagName);
+        Keyword keyword = Keyword.of(minLengthFlagName);
 
         assertThat(keyword.getFlagName()).isEqualTo(minLengthFlagName);
     }
@@ -76,61 +76,61 @@ public class KeywordTest {
     @Test
     public void keywordShouldThrowWhenFlagNameContainsPercentageCharacter() throws Exception
{
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword("a%");
+        Keyword.of("a%");
     }
 
     @Test
     public void keywordShouldThrowWhenFlagNameContainsLeftBracket() throws Exception {
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword("a[");
+        Keyword.of("a[");
     }
 
     @Test
     public void keywordShouldThrowWhenFlagNameContainsRightBracket() throws Exception {
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword("a]");
+        Keyword.of("a]");
     }
 
     @Test
     public void keywordShouldThrowWhenFlagNameContainsLeftBrace() throws Exception {
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword("a{");
+        Keyword.of("a{");
     }
 
     @Test
     public void keywordShouldThrowWhenFlagNameContainsSlash() throws Exception {
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword("a\\");
+        Keyword.of("a\\");
     }
 
     @Test
     public void keywordShouldThrowWhenFlagNameContainsStar() throws Exception {
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword("a*");
+        Keyword.of("a*");
     }
 
     @Test
     public void keywordShouldThrowWhenFlagNameContainsQuote() throws Exception {
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword("a\"");
+        Keyword.of("a\"");
     }
 
     @Test
     public void keywordShouldThrowWhenFlagNameContainsOpeningParenthesis() throws Exception
{
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword("a(");
+        Keyword.of("a(");
     }
 
     @Test
     public void keywordShouldThrowWhenFlagNameContainsClosingParenthesis() throws Exception
{
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword("a)");
+        Keyword.of("a)");
     }
 
     @Test
     public void keywordShouldThrowWhenFlagNameContainsSpaceCharacter() throws Exception {
         expectedException.expect(IllegalArgumentException.class);
-        new Keyword("a b");
+        Keyword.of("a b");
     }
 
     @Test
@@ -150,7 +150,7 @@ public class KeywordTest {
 
     @Test
     public void isNotNonExposedImapKeywordShouldReturnTrueWhenAnyUserFlag() throws Exception
{
-        Keyword keyword = new Keyword(ANY_KEYWORD);
+        Keyword keyword = Keyword.of(ANY_KEYWORD);
         assertThat(keyword.isExposedImapKeyword()).isTrue();
     }
 
@@ -166,13 +166,13 @@ public class KeywordTest {
 
     @Test
     public void asSystemFlagShouldReturnSystemFlag() throws Exception {
-        assertThat(new Keyword("$Draft").asSystemFlag())
+        assertThat(Keyword.of("$Draft").asSystemFlag())
             .isEqualTo(Optional.of(Flags.Flag.DRAFT));
     }
 
     @Test
     public void asSystemFlagShouldReturnEmptyWhenNonSystemFlag() throws Exception {
-        assertThat(new Keyword(ANY_KEYWORD).asSystemFlag().isPresent())
+        assertThat(Keyword.of(ANY_KEYWORD).asSystemFlag().isPresent())
             .isFalse();
     }
 
@@ -184,7 +184,7 @@ public class KeywordTest {
 
     @Test
     public void asFlagsShouldReturnFlagsWhenUserFlag() throws Exception {
-        Keyword keyword = new Keyword(ANY_KEYWORD);
+        Keyword keyword = Keyword.of(ANY_KEYWORD);
         assertThat(keyword.asFlags())
             .isEqualTo(new Flags(ANY_KEYWORD));
     }
@@ -192,7 +192,7 @@ public class KeywordTest {
     @Test
     public void asFlagsShouldReturnFlagsWhenUserFlagContainsUnderscore() throws Exception
{
         String userFlag = "$has_cal";
-        Keyword keyword = new Keyword(userFlag);
+        Keyword keyword = Keyword.of(userFlag);
         assertThat(keyword.asFlags())
             .isEqualTo(new Flags(userFlag));
     }
@@ -201,7 +201,7 @@ public class KeywordTest {
     public void hyphenMinusShouldBeAllowedInKeyword() {
         String userFlag = "aa-bb";
 
-        assertThatCode(() -> new Keyword(userFlag))
+        assertThatCode(() -> Keyword.of(userFlag))
             .doesNotThrowAnyException();
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/20a15278/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/KeywordsTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/KeywordsTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/KeywordsTest.java
index 29f33ba..ab026a3 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/KeywordsTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/KeywordsTest.java
@@ -61,7 +61,7 @@ public class KeywordsTest {
             .fromMap(ImmutableMap.of(ANY_KEYWORD, Keyword.FLAG_VALUE));
 
         assertThat(keywords.getKeywords())
-            .containsOnly(new Keyword(ANY_KEYWORD));
+            .containsOnly(Keyword.of(ANY_KEYWORD));
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/james-project/blob/20a15278/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageFactoryTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageFactoryTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageFactoryTest.java
index a8ae758..dfe2c00 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageFactoryTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/model/MessageFactoryTest.java
@@ -749,7 +749,7 @@ public class MessageFactoryTest {
 
     @Test
     public void keywordWithUserFlagShouldBeSetIntoMessage() throws Exception {
-        Keywords keywords = Keywords.factory().from(Keyword.ANSWERED, new Keyword(FORWARDED));
+        Keywords keywords = Keywords.factory().from(Keyword.ANSWERED, Keyword.of(FORWARDED));
 
         MetaDataWithContent testMail = MetaDataWithContent.builder()
             .uid(MessageUid.of(2))

http://git-wip-us.apache.org/repos/asf/james-project/blob/20a15278/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/KeywordsCombinerTest.java
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/KeywordsCombinerTest.java
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/KeywordsCombinerTest.java
index df36ab4..a7bb19c 100644
--- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/KeywordsCombinerTest.java
+++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/KeywordsCombinerTest.java
@@ -76,7 +76,7 @@ public class KeywordsCombinerTest {
     public void applyShouldUnionCustomKeyword() {
         KeywordsCombiner keywordsCombiner = new KeywordsCombiner();
 
-        Keyword customKeyword = new Keyword("$Any");
+        Keyword customKeyword = Keyword.of("$Any");
         assertThat(keywordsCombiner.apply(
             Keywords.DEFAULT_VALUE,
             Keywords.factory().from(customKeyword)))
@@ -110,8 +110,8 @@ public class KeywordsCombinerTest {
             Keyword.DRAFT,
             Keyword.FLAGGED,
             Keyword.SEEN,
-            new Keyword("$Forwarded"),
-            new Keyword("$Any"));
+            Keyword.of("$Forwarded"),
+            Keyword.of("$Any"));
 
         ImmutableSet<Keywords> values = ImmutableSet.of(
             FACTORY.from(Keyword.ANSWERED),
@@ -120,8 +120,8 @@ public class KeywordsCombinerTest {
             FACTORY.from(Keyword.FLAGGED),
             FACTORY.from(Keyword.SEEN),
             FACTORY.from(),
-            FACTORY.from(new Keyword("$Forwarded")),
-            FACTORY.from(new Keyword("$Any")),
+            FACTORY.from(Keyword.of("$Forwarded")),
+            FACTORY.from(Keyword.of("$Any")),
             allKeyword);
 
         assertThat(


---------------------------------------------------------------------
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