This is an automated email from the ASF dual-hosted git repository. btellier pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/james-project.git
commit 6f944f5569b337eb99d9bcb49c25471df8fdacb4 Author: Matthieu Baechler <[email protected]> AuthorDate: Wed Apr 15 23:42:00 2020 +0200 [Refactoring] replace int constants by a enum --- .../james/imap/api/message/request/SearchKey.java | 191 ++++++++++----------- .../james/imap/processor/SearchProcessor.java | 82 ++++----- .../imap/api/message/request/SearchKeyTest.java | 2 +- 3 files changed, 135 insertions(+), 140 deletions(-) diff --git a/protocols/imap/src/main/java/org/apache/james/imap/api/message/request/SearchKey.java b/protocols/imap/src/main/java/org/apache/james/imap/api/message/request/SearchKey.java index 2f7a5e6..ebe2141 100644 --- a/protocols/imap/src/main/java/org/apache/james/imap/api/message/request/SearchKey.java +++ b/protocols/imap/src/main/java/org/apache/james/imap/api/message/request/SearchKey.java @@ -19,6 +19,47 @@ package org.apache.james.imap.api.message.request; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_ALL; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_AND; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_ANSWERED; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_BCC; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_BEFORE; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_BODY; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_CC; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_DELETED; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_DRAFT; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_FLAGGED; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_FROM; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_HEADER; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_KEYWORD; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_LARGER; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_MODSEQ; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_NEW; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_NOT; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_OLD; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_OLDER; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_ON; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_OR; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_RECENT; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_SEEN; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_SENTBEFORE; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_SENTON; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_SENTSINCE; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_SEQUENCE_SET; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_SINCE; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_SMALLER; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_SUBJECT; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_TEXT; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_TO; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_UID; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_UNANSWERED; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_UNDELETED; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_UNDRAFT; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_UNFLAGGED; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_UNKEYWORD; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_UNSEEN; +import static org.apache.james.imap.api.message.request.SearchKey.Type.TYPE_YOUNGER; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -36,93 +77,48 @@ import com.google.common.collect.ImmutableList; */ public final class SearchKey { - // NUMBERS - - public static final int TYPE_SEQUENCE_SET = 1; - public static final int TYPE_UID = 2; - - // NO PARAMETERS - public static final int TYPE_ALL = 3; - - public static final int TYPE_ANSWERED = 4; - - public static final int TYPE_DELETED = 5; - - public static final int TYPE_DRAFT = 6; - - public static final int TYPE_FLAGGED = 7; - - public static final int TYPE_NEW = 8; - - public static final int TYPE_OLD = 9; - - public static final int TYPE_RECENT = 10; - - public static final int TYPE_SEEN = 11; - - public static final int TYPE_UNANSWERED = 12; - - public static final int TYPE_UNDELETED = 13; - - public static final int TYPE_UNDRAFT = 14; - - public static final int TYPE_UNFLAGGED = 15; - - public static final int TYPE_UNSEEN = 16; - - // ONE VALUE - public static final int TYPE_BCC = 17; - - public static final int TYPE_BODY = 18; - - public static final int TYPE_CC = 19; - - public static final int TYPE_FROM = 20; - - public static final int TYPE_KEYWORD = 21; - - public static final int TYPE_SUBJECT = 22; - - public static final int TYPE_TEXT = 23; - - public static final int TYPE_TO = 24; - - public static final int TYPE_UNKEYWORD = 25; - - // ONE DATE - public static final int TYPE_BEFORE = 26; - - public static final int TYPE_ON = 27; - - public static final int TYPE_SENTBEFORE = 28; - - public static final int TYPE_SENTON = 29; - - public static final int TYPE_SENTSINCE = 30; - - public static final int TYPE_SINCE = 31; - - // FIELD VALUE - public static final int TYPE_HEADER = 32; - - // ONE NUMBER - public static final int TYPE_LARGER = 33; - - public static final int TYPE_SMALLER = 34; - - // NOT - public static final int TYPE_NOT = 35; - - // OR - public static final int TYPE_OR = 36; - - // AND - public static final int TYPE_AND = 37; - - public static final int TYPE_YOUNGER = 38; - public static final int TYPE_OLDER = 39; - - public static final int TYPE_MODSEQ = 40; + public enum Type { + TYPE_SEQUENCE_SET, + TYPE_UID, + TYPE_ALL, + TYPE_ANSWERED, + TYPE_DELETED, + TYPE_DRAFT, + TYPE_FLAGGED, + TYPE_NEW, + TYPE_OLD, + TYPE_RECENT, + TYPE_SEEN, + TYPE_UNANSWERED, + TYPE_UNDELETED, + TYPE_UNDRAFT, + TYPE_UNFLAGGED, + TYPE_UNSEEN, + TYPE_BCC, + TYPE_BODY, + TYPE_CC, + TYPE_FROM, + TYPE_KEYWORD, + TYPE_SUBJECT, + TYPE_TEXT, + TYPE_TO, + TYPE_UNKEYWORD, + TYPE_BEFORE, + TYPE_ON, + TYPE_SENTBEFORE, + TYPE_SENTON, + TYPE_SENTSINCE, + TYPE_SINCE, + TYPE_HEADER, + TYPE_LARGER, + TYPE_SMALLER, + TYPE_NOT, + TYPE_OR, + TYPE_AND, + TYPE_YOUNGER, + TYPE_OLDER, + TYPE_MODSEQ + } private static final SearchKey UNSEEN = new SearchKey(TYPE_UNSEEN, null, null, 0, null, null, null, null, -1, -1); @@ -334,7 +330,7 @@ public final class SearchKey { return new SearchKey(TYPE_MODSEQ, null, null, 0, null, null, null, null, -1, modSeq); } - private final int type; + private final Type type; private final DayMonthYear date; @@ -354,7 +350,7 @@ public final class SearchKey { private final long modSeq; - private SearchKey(int type, DayMonthYear date, List<SearchKey> keys, long number, String name, String value, UidRange[] uids, IdRange[] sequence, long seconds, long modSeq) { + private SearchKey(Type type, DayMonthYear date, List<SearchKey> keys, long number, String name, String value, UidRange[] uids, IdRange[] sequence, long seconds, long modSeq) { this.type = type; this.date = date; this.keys = keys; @@ -370,9 +366,8 @@ public final class SearchKey { /** * Gets a date value to be search upon. * - * @return the date when: {@link #TYPE_BEFORE}, {@link #TYPE_ON}, - * {@link #TYPE_SENTBEFORE}, {@link #TYPE_SENTON}, - * {@link #TYPE_SENTSINCE}, {@link #TYPE_SINCE}; otherwise null + * @return the date when: TYPE_BEFORE, TYPE_ON, + * TYPE_SENTBEFORE, TYPE_SENTON, TYPE_SENTSINCE, TYPE_SINCE, otherwise null */ public DayMonthYear getDate() { return date; @@ -390,7 +385,7 @@ public final class SearchKey { /** * Gets sequence numbers. * - * @return msn when {@link #TYPE_SEQUENCE_SET}, uids when {@link #TYPE_UID}, + * @return msn when TYPE_SEQUENCE_SET, uids when TYPE_UID, * null otherwise */ public IdRange[] getSequenceNumbers() { @@ -404,7 +399,7 @@ public final class SearchKey { /** * Gets the field name. * - * @return the field name when {@link #TYPE_HEADER}, null otherwise + * @return the field name when TYPE_HEADER, null otherwise */ public String getName() { return name; @@ -413,7 +408,7 @@ public final class SearchKey { /** * Gets the size searched for. * - * @return the size when {@link #TYPE_LARGER} or {@link #TYPE_SMALLER}, + * @return the size when TYPE_LARGER or TYPE_SMALLER, * otherwise 0 */ public long getSize() { @@ -424,7 +419,7 @@ public final class SearchKey { * Gets key two. * * @return <code>List</code> of <code>SearchKey</code>'s when - * {@link #TYPE_OR}, {@link #TYPE_AND} or {@link #TYPE_NOT} + * TYPE_OR, TYPE_AND or TYPE_NOT * otherwise null */ public List<SearchKey> getKeys() { @@ -436,7 +431,7 @@ public final class SearchKey { * * @return the type */ - public int getType() { + public Type getType() { return type; } @@ -452,7 +447,7 @@ public final class SearchKey { /** * Gets the size searched for. * - * @return the size when {@link #TYPE_MODSEQ} + * @return the size when TYPE_MODSEQ * otherwise -1 */ public long getModSeq() { diff --git a/protocols/imap/src/main/java/org/apache/james/imap/processor/SearchProcessor.java b/protocols/imap/src/main/java/org/apache/james/imap/processor/SearchProcessor.java index b28216d..16c80fb 100644 --- a/protocols/imap/src/main/java/org/apache/james/imap/processor/SearchProcessor.java +++ b/protocols/imap/src/main/java/org/apache/james/imap/processor/SearchProcessor.java @@ -272,32 +272,32 @@ public class SearchProcessor extends AbstractMailboxProcessor<SearchRequest> imp } private SearchQuery.Criterion toCriterion(SearchKey key, ImapSession session) throws MessageRangeException { - final int type = key.getType(); + final SearchKey.Type type = key.getType(); final DayMonthYear date = key.getDate(); switch (type) { - case SearchKey.TYPE_ALL: + case TYPE_ALL: return SearchQuery.all(); - case SearchKey.TYPE_AND: + case TYPE_AND: return and(key.getKeys(), session); - case SearchKey.TYPE_ANSWERED: + case TYPE_ANSWERED: return SearchQuery.flagIsSet(Flag.ANSWERED); - case SearchKey.TYPE_BCC: + case TYPE_BCC: return SearchQuery.address(AddressType.Bcc, key.getValue()); - case SearchKey.TYPE_BEFORE: + case TYPE_BEFORE: return SearchQuery.internalDateBefore(date.toDate(), DateResolution.Day); - case SearchKey.TYPE_BODY: + case TYPE_BODY: return SearchQuery.bodyContains(key.getValue()); - case SearchKey.TYPE_CC: + case TYPE_CC: return SearchQuery.address(AddressType.Cc, key.getValue()); - case SearchKey.TYPE_DELETED: + case TYPE_DELETED: return SearchQuery.flagIsSet(Flag.DELETED); - case SearchKey.TYPE_DRAFT: + case TYPE_DRAFT: return SearchQuery.flagIsSet(Flag.DRAFT); - case SearchKey.TYPE_FLAGGED: + case TYPE_FLAGGED: return SearchQuery.flagIsSet(Flag.FLAGGED); - case SearchKey.TYPE_FROM: + case TYPE_FROM: return SearchQuery.address(AddressType.From, key.getValue()); - case SearchKey.TYPE_HEADER: + case TYPE_HEADER: String value = key.getValue(); // Check if header exists if the value is empty. See IMAP-311 if (value == null || value.length() == 0) { @@ -305,67 +305,67 @@ public class SearchProcessor extends AbstractMailboxProcessor<SearchRequest> imp } else { return SearchQuery.headerContains(key.getName(), value); } - case SearchKey.TYPE_KEYWORD: + case TYPE_KEYWORD: return SearchQuery.flagIsSet(key.getValue()); - case SearchKey.TYPE_LARGER: + case TYPE_LARGER: return SearchQuery.sizeGreaterThan(key.getSize()); - case SearchKey.TYPE_NEW: + case TYPE_NEW: return SearchQuery.and(SearchQuery.flagIsSet(Flag.RECENT), SearchQuery.flagIsUnSet(Flag.SEEN)); - case SearchKey.TYPE_NOT: + case TYPE_NOT: return not(key.getKeys(), session); - case SearchKey.TYPE_OLD: + case TYPE_OLD: return SearchQuery.flagIsUnSet(Flag.RECENT); - case SearchKey.TYPE_ON: + case TYPE_ON: return SearchQuery.internalDateOn(date.toDate(), DateResolution.Day); - case SearchKey.TYPE_OR: + case TYPE_OR: return or(key.getKeys(), session); - case SearchKey.TYPE_RECENT: + case TYPE_RECENT: return SearchQuery.flagIsSet(Flag.RECENT); - case SearchKey.TYPE_SEEN: + case TYPE_SEEN: return SearchQuery.flagIsSet(Flag.SEEN); - case SearchKey.TYPE_SENTBEFORE: + case TYPE_SENTBEFORE: return SearchQuery.headerDateBefore(ImapConstants.RFC822_DATE, date.toDate(), DateResolution.Day); - case SearchKey.TYPE_SENTON: + case TYPE_SENTON: return SearchQuery.headerDateOn(ImapConstants.RFC822_DATE, date.toDate(), DateResolution.Day); - case SearchKey.TYPE_SENTSINCE: + case TYPE_SENTSINCE: // Include the date which is used as search param. See IMAP-293 Criterion onCrit = SearchQuery.headerDateOn(ImapConstants.RFC822_DATE, date.toDate(), DateResolution.Day); Criterion afterCrit = SearchQuery.headerDateAfter(ImapConstants.RFC822_DATE, date.toDate(), DateResolution.Day); return SearchQuery.or(onCrit, afterCrit); - case SearchKey.TYPE_SEQUENCE_SET: + case TYPE_SEQUENCE_SET: return sequence(key.getSequenceNumbers(), session); - case SearchKey.TYPE_SINCE: + case TYPE_SINCE: // Include the date which is used as search param. See IMAP-293 return SearchQuery.or(SearchQuery.internalDateOn(date.toDate(), DateResolution.Day), SearchQuery.internalDateAfter(date.toDate(), DateResolution.Day)); - case SearchKey.TYPE_SMALLER: + case TYPE_SMALLER: return SearchQuery.sizeLessThan(key.getSize()); - case SearchKey.TYPE_SUBJECT: + case TYPE_SUBJECT: return SearchQuery.headerContains(ImapConstants.RFC822_SUBJECT, key.getValue()); - case SearchKey.TYPE_TEXT: + case TYPE_TEXT: return SearchQuery.mailContains(key.getValue()); - case SearchKey.TYPE_TO: + case TYPE_TO: return SearchQuery.address(AddressType.To, key.getValue()); - case SearchKey.TYPE_UID: + case TYPE_UID: return uids(key.getUidRanges(), session); - case SearchKey.TYPE_UNANSWERED: + case TYPE_UNANSWERED: return SearchQuery.flagIsUnSet(Flag.ANSWERED); - case SearchKey.TYPE_UNDELETED: + case TYPE_UNDELETED: return SearchQuery.flagIsUnSet(Flag.DELETED); - case SearchKey.TYPE_UNDRAFT: + case TYPE_UNDRAFT: return SearchQuery.flagIsUnSet(Flag.DRAFT); - case SearchKey.TYPE_UNFLAGGED: + case TYPE_UNFLAGGED: return SearchQuery.flagIsUnSet(Flag.FLAGGED); - case SearchKey.TYPE_UNKEYWORD: + case TYPE_UNKEYWORD: return SearchQuery.flagIsUnSet(key.getValue()); - case SearchKey.TYPE_UNSEEN: + case TYPE_UNSEEN: return SearchQuery.flagIsUnSet(Flag.SEEN); - case SearchKey.TYPE_OLDER: + case TYPE_OLDER: Date withinDate = createWithinDate(key); return SearchQuery.or(SearchQuery.internalDateOn(withinDate, DateResolution.Second), SearchQuery.internalDateBefore(withinDate, DateResolution.Second)); - case SearchKey.TYPE_YOUNGER: + case TYPE_YOUNGER: Date withinDate2 = createWithinDate(key); return SearchQuery.or(SearchQuery.internalDateOn(withinDate2, DateResolution.Second), SearchQuery.internalDateAfter(withinDate2, DateResolution.Second)); - case SearchKey.TYPE_MODSEQ: + case TYPE_MODSEQ: session.setAttribute(SEARCH_MODSEQ, true); long modSeq = key.getModSeq(); return SearchQuery.or(SearchQuery.modSeqEquals(modSeq), SearchQuery.modSeqGreaterThan(modSeq)); diff --git a/protocols/imap/src/test/java/org/apache/james/imap/api/message/request/SearchKeyTest.java b/protocols/imap/src/test/java/org/apache/james/imap/api/message/request/SearchKeyTest.java index e8a962d..3c93398 100644 --- a/protocols/imap/src/test/java/org/apache/james/imap/api/message/request/SearchKeyTest.java +++ b/protocols/imap/src/test/java/org/apache/james/imap/api/message/request/SearchKeyTest.java @@ -46,6 +46,6 @@ public class SearchKeyTest { public void modSeqSearchKeyShouldBeOfTypeModSeq() { SearchKey searchKey = SearchKey.buildModSeq(36); - assertThat(searchKey.getType()).isEqualTo(SearchKey.TYPE_MODSEQ); + assertThat(searchKey.getType()).isEqualTo(SearchKey.Type.TYPE_MODSEQ); } } \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
