You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by rw...@apache.org on 2018/04/13 20:36:10 UTC

svn commit: r1829100 - in /pivot/trunk/wtk: src/org/apache/pivot/wtk/RangeSelection.java src/org/apache/pivot/wtk/Span.java test/org/apache/pivot/wtk/test/SpanTest.java

Author: rwhitcomb
Date: Fri Apr 13 20:36:10 2018
New Revision: 1829100

URL: http://svn.apache.org/viewvc?rev=1829100&view=rev
Log:
Move some of the methods and the comparators out of RangeSelection
and into Span where they more properly should be.  Add several new
methods to Span for convenience.
Update RangeSelection to use some of the new Span methods.
Add tests of the new Span stuff in SpanTest.

Modified:
    pivot/trunk/wtk/src/org/apache/pivot/wtk/RangeSelection.java
    pivot/trunk/wtk/src/org/apache/pivot/wtk/Span.java
    pivot/trunk/wtk/test/org/apache/pivot/wtk/test/SpanTest.java

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/RangeSelection.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/RangeSelection.java?rev=1829100&r1=1829099&r2=1829100&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/RangeSelection.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/RangeSelection.java Fri Apr 13 20:36:10 2018
@@ -16,8 +16,6 @@
  */
 package org.apache.pivot.wtk;
 
-import java.util.Comparator;
-
 import org.apache.pivot.collections.ArrayList;
 import org.apache.pivot.collections.Sequence;
 import org.apache.pivot.collections.immutable.ImmutableList;
@@ -29,30 +27,6 @@ public class RangeSelection {
     // The coalesced selected ranges
     private ArrayList<Span> selectedRanges = new ArrayList<>();
 
-    // Comparator that determines the index of the first intersecting range.
-    private static final Comparator<Span> START_COMPARATOR = new Comparator<Span>() {
-        @Override
-        public int compare(Span range1, Span range2) {
-            return (range1.end - range2.start);
-        }
-    };
-
-    // Comparator that determines the index of the last intersecting range.
-    private static final Comparator<Span> END_COMPARATOR = new Comparator<Span>() {
-        @Override
-        public int compare(Span range1, Span range2) {
-            return (range1.start - range2.end);
-        }
-    };
-
-    // Comparator that determines if two ranges intersect.
-    private static final Comparator<Span> INTERSECTION_COMPARATOR = new Comparator<Span>() {
-        @Override
-        public int compare(Span range1, Span range2) {
-            return (range1.start > range2.end) ? 1 : (range2.start > range1.end) ? -1 : 0;
-        }
-    };
-
     /**
      * Adds a range to the selection, merging and removing intersecting ranges
      * as needed.
@@ -64,7 +38,7 @@ public class RangeSelection {
     public Sequence<Span> addRange(int start, int end) {
         ArrayList<Span> addedRanges = new ArrayList<>();
 
-        Span range = normalize(start, end);
+        Span range = Span.normalize(start, end);
         assert (range.start >= 0);
 
         int n = selectedRanges.getLength();
@@ -76,7 +50,7 @@ public class RangeSelection {
             addedRanges.add(range);
         } else {
             // Locate the lower bound of the intersection
-            int i = ArrayList.binarySearch(selectedRanges, range, START_COMPARATOR);
+            int i = ArrayList.binarySearch(selectedRanges, range, Span.START_COMPARATOR);
             if (i < 0) {
                 i = -(i + 1);
             }
@@ -96,7 +70,7 @@ public class RangeSelection {
                 addedRanges.add(range);
             } else {
                 // Locate the upper bound of the intersection
-                int j = ArrayList.binarySearch(selectedRanges, range, END_COMPARATOR);
+                int j = ArrayList.binarySearch(selectedRanges, range, Span.END_COMPARATOR);
                 if (j < 0) {
                     j = -(j + 1);
                 } else {
@@ -120,8 +94,8 @@ public class RangeSelection {
                     Span lowerRange = selectedRanges.get(i);
                     Span upperRange = selectedRanges.get(j - 1);
 
-                    range = new Span(Math.min(range.start, lowerRange.start), Math.max(range.end,
-                        upperRange.end));
+                    range = new Span(Math.min(range.start, lowerRange.start),
+                                     Math.max(range.end, upperRange.end));
 
                     // Add the gaps to the added list
                     if (range.start < lowerRange.start) {
@@ -162,14 +136,14 @@ public class RangeSelection {
     public Sequence<Span> removeRange(int start, int end) {
         ArrayList<Span> removedRanges = new ArrayList<>();
 
-        Span range = normalize(start, end);
+        Span range = Span.normalize(start, end);
         assert (range.start >= 0);
 
         int n = selectedRanges.getLength();
 
         if (n > 0) {
             // Locate the lower bound of the intersection
-            int i = ArrayList.binarySearch(selectedRanges, range, START_COMPARATOR);
+            int i = ArrayList.binarySearch(selectedRanges, range, Span.START_COMPARATOR);
             if (i < 0) {
                 i = -(i + 1);
             }
@@ -193,7 +167,7 @@ public class RangeSelection {
                     }
 
                     // Locate the upper bound of the intersection
-                    int j = ArrayList.binarySearch(selectedRanges, range, END_COMPARATOR);
+                    int j = ArrayList.binarySearch(selectedRanges, range, Span.END_COMPARATOR);
                     if (j < 0) {
                         j = -(j + 1);
                     } else {
@@ -275,7 +249,7 @@ public class RangeSelection {
         assert (range != null);
 
         int index = -1;
-        int i = ArrayList.binarySearch(selectedRanges, range, INTERSECTION_COMPARATOR);
+        int i = ArrayList.binarySearch(selectedRanges, range, Span.INTERSECTION_COMPARATOR);
 
         if (i >= 0) {
             index = (range.equals(selectedRanges.get(i))) ? i : -1;
@@ -292,7 +266,7 @@ public class RangeSelection {
      */
     public boolean containsIndex(int index) {
         Span range = new Span(index);
-        int i = ArrayList.binarySearch(selectedRanges, range, INTERSECTION_COMPARATOR);
+        int i = ArrayList.binarySearch(selectedRanges, range, Span.INTERSECTION_COMPARATOR);
 
         return (i >= 0);
     }
@@ -310,7 +284,7 @@ public class RangeSelection {
         // Get the insertion point for the range corresponding to the given
         // index
         Span range = new Span(index);
-        int i = ArrayList.binarySearch(selectedRanges, range, INTERSECTION_COMPARATOR);
+        int i = ArrayList.binarySearch(selectedRanges, range, Span.INTERSECTION_COMPARATOR);
 
         if (i < 0) {
             // The inserted index does not intersect with a selected range
@@ -322,7 +296,7 @@ public class RangeSelection {
             // If the inserted index falls within the current range, increment
             // the endpoint only
             if (selectedRange.start < index) {
-                selectedRanges.update(i, new Span(selectedRange.start, selectedRange.end + 1));
+                selectedRanges.update(i, selectedRange.lengthen(1));
 
                 // Start incrementing range bounds beginning at the next range
                 i++;
@@ -333,7 +307,7 @@ public class RangeSelection {
         int n = selectedRanges.getLength();
         while (i < n) {
             Span selectedRange = selectedRanges.get(i);
-            selectedRanges.update(i, new Span(selectedRange.start + 1, selectedRange.end + 1));
+            selectedRanges.update(i, selectedRange.offset(1));
             updated++;
             i++;
         }
@@ -356,7 +330,7 @@ public class RangeSelection {
 
         // Decrement any subsequent selection indexes
         Span range = new Span(index);
-        int i = ArrayList.binarySearch(selectedRanges, range, INTERSECTION_COMPARATOR);
+        int i = ArrayList.binarySearch(selectedRanges, range, Span.INTERSECTION_COMPARATOR);
         assert (i < 0) : "i should be negative, since index should no longer be selected";
 
         i = -(i + 1);
@@ -365,8 +339,7 @@ public class RangeSelection {
         int n = selectedRanges.getLength();
         while (i < n) {
             Span selectedRange = selectedRanges.get(i);
-            selectedRanges.update(i, new Span(selectedRange.start - count, selectedRange.end
-                - count));
+            selectedRanges.update(i, selectedRange.offset(-count));
             updated++;
             i++;
         }
@@ -374,14 +347,4 @@ public class RangeSelection {
         return updated;
     }
 
-    /**
-     * Ensures that the start value is less than or equal to the end value.
-     *
-     * @param start The new proposed start value.
-     * @param end The new proposed end.
-     * @return A span containing the normalized range.
-     */
-    public static Span normalize(int start, int end) {
-        return new Span(Math.min(start, end), Math.max(start, end));
-    }
 }

Modified: pivot/trunk/wtk/src/org/apache/pivot/wtk/Span.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/src/org/apache/pivot/wtk/Span.java?rev=1829100&r1=1829099&r2=1829100&view=diff
==============================================================================
--- pivot/trunk/wtk/src/org/apache/pivot/wtk/Span.java (original)
+++ pivot/trunk/wtk/src/org/apache/pivot/wtk/Span.java Fri Apr 13 20:36:10 2018
@@ -16,6 +16,8 @@
  */
 package org.apache.pivot.wtk;
 
+import java.util.Comparator;
+
 import org.apache.pivot.collections.Dictionary;
 import org.apache.pivot.collections.List;
 import org.apache.pivot.collections.Sequence;
@@ -272,11 +274,22 @@ public final class Span {
     }
 
     /**
+     * Create a span where the start value is less than or equal to the end value.
+     *
+     * @param start The new proposed start value.
+     * @param end The new proposed end.
+     * @return A span containing the normalized range.
+     */
+    public static Span normalize(int start, int end) {
+        return new Span(Math.min(start, end), Math.max(start, end));
+    }
+
+    /**
      * @return A normalized equivalent of the span in which <tt>start</tt> is
      * guaranteed to be less than <tt>end</tt>.
      */
     public Span normalize() {
-        return new Span(normalStart(), normalEnd());
+        return normalize(start, end);
     }
 
     /**
@@ -294,6 +307,28 @@ public final class Span {
     }
 
     /**
+     * Returns a new {@link Span} with the end value offset by the given value.
+     *
+     * @param offset The positive or negative amount by which to "lengthen" this
+     * span (only the end).
+     * @return A new {@link Span} with updated value.
+     */
+    public Span lengthen(int offset) {
+        return new Span(this.start, this.end + offset);
+    }
+
+    /**
+     * Returns a new {@link Span} with the start value offset by the given value.
+     *
+     * @param offset The positive or negative amount by which to "shift" this
+     * span (only the start).
+     * @return A new {@link Span} with updated value.
+     */
+    public Span move(int offset) {
+        return new Span(this.start + offset, this.end);
+    }
+
+    /**
      * Decides whether the normalized version of this span is equal the
      * normalized version of the other span.  Saves the overhead of making
      * a new object (with {@link #normalize}).
@@ -384,4 +419,30 @@ public final class Span {
 
         return span;
     }
+
+    /** Comparator that determines the index of the first intersecting range. */
+    public static final Comparator<Span> START_COMPARATOR = new Comparator<Span>() {
+        @Override
+        public int compare(Span range1, Span range2) {
+            return (range1.end - range2.start);
+        }
+    };
+
+    /** Comparator that determines the index of the last intersecting range. */
+    public static final Comparator<Span> END_COMPARATOR = new Comparator<Span>() {
+        @Override
+        public int compare(Span range1, Span range2) {
+            return (range1.start - range2.end);
+        }
+    };
+
+    /** Comparator that determines if two ranges intersect. */
+    public static final Comparator<Span> INTERSECTION_COMPARATOR = new Comparator<Span>() {
+        @Override
+        public int compare(Span range1, Span range2) {
+            return (range1.start > range2.end) ? 1 : (range2.start > range1.end) ? -1 : 0;
+        }
+    };
+
+
 }

Modified: pivot/trunk/wtk/test/org/apache/pivot/wtk/test/SpanTest.java
URL: http://svn.apache.org/viewvc/pivot/trunk/wtk/test/org/apache/pivot/wtk/test/SpanTest.java?rev=1829100&r1=1829099&r2=1829100&view=diff
==============================================================================
--- pivot/trunk/wtk/test/org/apache/pivot/wtk/test/SpanTest.java (original)
+++ pivot/trunk/wtk/test/org/apache/pivot/wtk/test/SpanTest.java Fri Apr 13 20:36:10 2018
@@ -47,6 +47,8 @@ public class SpanTest {
         Span spAll = sp1.union(sp0).union(sp2).union(sp3).union(sp4);
         Span sp6 = Span.decode("4, 6");
         Span sp6a = new Span(4, 6);
+        Span sp6b = sp6.lengthen(-3);
+        Span sp6d = sp6b.move(-2);
 
         assertEquals(spMinus1.getLength(), 2);
         assertEquals(sp0.getLength(), 1);
@@ -82,6 +84,8 @@ public class SpanTest {
         assertEquals(sp6, sp6a);
         assertEquals(sp6.getLength(), 3);
         assertEquals(sp6.toString(), "Span {start:4, end:6}");
+        assertEquals(sp6b, sp5b);
+        assertEquals(sp6d, sp3a);
     }
 
 }