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 bt...@apache.org on 2019/11/19 02:43:29 UTC

[james-project] 27/43: [Refactoring] IdRange: reorder methods

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 76906992314ad743c8afe1a24f5f97904da5295b
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Fri Nov 15 14:13:05 2019 +0700

    [Refactoring] IdRange: reorder methods
---
 .../org/apache/james/imap/api/message/IdRange.java | 207 ++++++++++-----------
 1 file changed, 102 insertions(+), 105 deletions(-)

diff --git a/protocols/imap/src/main/java/org/apache/james/imap/api/message/IdRange.java b/protocols/imap/src/main/java/org/apache/james/imap/api/message/IdRange.java
index 4dee168..b2b4c08 100644
--- a/protocols/imap/src/main/java/org/apache/james/imap/api/message/IdRange.java
+++ b/protocols/imap/src/main/java/org/apache/james/imap/api/message/IdRange.java
@@ -34,6 +34,78 @@ import com.google.common.collect.ImmutableList;
  * Represents a range of UID or MSN values.
  */
 public final class IdRange implements Iterable<Long>, Comparable<IdRange> {
+    /**
+     * {@link Iterator} of a range of msn/uid
+     */
+    private final class RangeIterator implements Iterator<Long> {
+
+        private final long to;
+        private long current;
+
+        public RangeIterator(long from, long to) {
+            this.to = to;
+            this.current = from;
+        }
+
+        @Override
+        public boolean hasNext() {
+            return current <= to;
+        }
+
+        @Override
+        public Long next() {
+            if (hasNext()) {
+                return current++;
+            } else {
+                throw new NoSuchElementException("Highest id of " + to + " was reached before");
+            }
+        }
+
+        @Override
+        public void remove() {
+            throw new UnsupportedOperationException("Read-Only");
+        }
+
+    }
+
+    /**
+     * Utility method which will copy the given {@link List} and try to merge
+     * the {@link IdRange} in the copy before return it.
+     *
+     * @return mergedRanges
+     */
+    public static List<IdRange> mergeRanges(List<IdRange> ranges) {
+        List<IdRange> copy = new ArrayList<>(ranges);
+        Collections.sort(copy);
+
+        boolean lastUid = false;
+
+        for (int i = 0; i < copy.size() - 1; i++) {
+            IdRange current = copy.get(i);
+            IdRange next = copy.get(i + 1);
+            if (next.getLowVal() == Long.MAX_VALUE && next.getHighVal() == Long.MAX_VALUE) {
+                if (lastUid) {
+                    copy.remove(next);
+                    i--;
+                } else {
+                    lastUid = true;
+                }
+            } else {
+                // Make sure we handle the "*" and "*:*" correctly and don't
+                // remove ranges by error. See IMAP-289
+                if ((current.getLowVal() != Long.MAX_VALUE && current.getHighVal() != Long.MAX_VALUE) && (current.getHighVal() >= next.getLowVal() - 1)) {
+                    if (next.getHighVal() > current.getHighVal()) {
+                        current.setHighVal(next.getHighVal());
+                    }
+                    // remove the merged id range and decrease the count
+                    copy.remove(next);
+                    i--;
+                }
+            }
+
+        }
+        return copy;
+    }
 
     public static String toString(IdRange[] ranges) {
         return Optional.ofNullable(ranges)
@@ -78,36 +150,6 @@ public final class IdRange implements Iterable<Long>, Comparable<IdRange> {
         this.highVal = highVal;
     }
 
-    @Override
-    public int hashCode() {
-        final int PRIME = 31;
-        int result = 1;
-        result = PRIME * result + (int) (highVal ^ (highVal >>> 32));
-        result = PRIME * result + (int) (lowVal ^ (lowVal >>> 32));
-        return result;
-    }
-
-    @Override
-    public boolean equals(Object obj) {
-        if (this == obj) {
-            return true;
-        }
-        if (obj == null) {
-            return false;
-        }
-        if (getClass() != obj.getClass()) {
-            return false;
-        }
-        final IdRange other = (IdRange) obj;
-        if (highVal != other.highVal) {
-            return false;
-        }
-        if (lowVal != other.lowVal) {
-            return false;
-        }
-        return true;
-    }
-
     /**
      * Renders text suitable for logging.
      * 
@@ -126,47 +168,6 @@ public final class IdRange implements Iterable<Long>, Comparable<IdRange> {
     }
 
     /**
-     * Utility method which will copy the given {@link List} and try to merge
-     * the {@link IdRange} in the copy before return it.
-     *
-     * @return mergedRanges
-     */
-    public static List<IdRange> mergeRanges(List<IdRange> ranges) {
-        List<IdRange> copy = new ArrayList<>(ranges);
-        Collections.sort(copy);
-
-        boolean lastUid = false;
-
-        for (int i = 0; i < copy.size() - 1; i++) {
-            IdRange current = copy.get(i);
-            IdRange next = copy.get(i + 1);
-            if (next.getLowVal() == Long.MAX_VALUE && next.getHighVal() == Long.MAX_VALUE) {
-                if (lastUid) {
-                    copy.remove(next);
-                    i--;
-                } else {
-                    lastUid = true;
-                }
-            } else {
-                // Make sure we handle the "*" and "*:*" correctly and don't
-                // remove ranges by error. See IMAP-289
-                if ((current.getLowVal() != Long.MAX_VALUE && current.getHighVal() != Long.MAX_VALUE) && (current.getHighVal() >= next.getLowVal() - 1)) {
-                    if (next.getHighVal() > current.getHighVal()) {
-                        current.setHighVal(next.getHighVal());
-                    }
-                    // remove the merged id range and decrease the count
-                    copy.remove(next);
-                    i--;
-                }
-            }
-
-        }
-        return copy;
-
-    }
-
-
-    /**
      * Return a read-only {@link Iterator} which contains all msn/uid which fail in the specified range.
      */
     @Override
@@ -178,40 +179,6 @@ public final class IdRange implements Iterable<Long>, Comparable<IdRange> {
         long to = getHighVal();
         return new RangeIterator(from, to);
     }
-    
-    /**
-     * {@link Iterator} of a range of msn/uid
-     */
-    private final class RangeIterator implements Iterator<Long> {
-
-        private final long to;
-        private long current;
-        
-        public RangeIterator(long from, long to) {
-            this.to = to;
-            this.current = from;
-        }
-        
-        @Override
-        public boolean hasNext() {
-            return current <= to;
-        }
-
-        @Override
-        public Long next() {
-            if (hasNext()) {
-                return current++;
-            } else {
-                throw new NoSuchElementException("Highest id of " + to + " was reached before");
-            }
-        }
-
-        @Override
-        public void remove() {
-            throw new UnsupportedOperationException("Read-Only");
-        }
-        
-    }
 
     @Override
     public int compareTo(IdRange range2) {
@@ -228,4 +195,34 @@ public final class IdRange implements Iterable<Long>, Comparable<IdRange> {
         }
     }
 
+    @Override
+    public int hashCode() {
+        final int PRIME = 31;
+        int result = 1;
+        result = PRIME * result + (int) (highVal ^ (highVal >>> 32));
+        result = PRIME * result + (int) (lowVal ^ (lowVal >>> 32));
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final IdRange other = (IdRange) obj;
+        if (highVal != other.highVal) {
+            return false;
+        }
+        if (lowVal != other.lowVal) {
+            return false;
+        }
+        return true;
+    }
+
 }


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