Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrian Vosburgh2013-11-21 23:29:51 +0000
committerBrian Vosburgh2015-07-16 18:54:39 +0000
commit0847528b0f90c0872b8dbf8baa93e85edf94c149 (patch)
treef2b0197907c4fcb8309d8eb11bef7206f59656f6
parent1c3592d3b02e4c829c5a98368b639e8245866574 (diff)
downloadwebtools.dali-0847528b0f90c0872b8dbf8baa93e85edf94c149.tar.gz
webtools.dali-0847528b0f90c0872b8dbf8baa93e85edf94c149.tar.xz
webtools.dali-0847528b0f90c0872b8dbf8baa93e85edf94c149.zip
normalize Range
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/Range.java42
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/SortedListValueModelAdapter.java2
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/SortedListValueModelWrapper.java2
-rw-r--r--common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/RangeTests.java26
4 files changed, 51 insertions, 21 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/Range.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/Range.java
index 8c6227f511..c52a936cf5 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/Range.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/Range.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2012 Oracle. All rights reserved.
+ * Copyright (c) 2005, 2013 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -14,34 +14,43 @@ import java.io.Serializable;
/**
* This simple container class simply puts a bit of semantics
* around a pair of numbers.
+ * <p>
+ * <strong>NB:</strong> The {@link #start} will be less than (or equal to)
+ * the {@link #end}.
*/
public class Range
implements Cloneable, Serializable
{
- /** The starting index of the range. */
+ /**
+ * The range's starting index. This will always be less than or equal to
+ * {@link #end the range's ending index}.
+ */
public final int start;
- /** The ending index of the range. */
+ /**
+ * The range's ending index. This will always be greater than or equal to
+ * {@link #start the range's starting index}.
+ */
public final int end;
/**
- * The size can be negative if the ending index
- * is less than the starting index.
+ * The range's length. The range's length will never be negative.
*/
- public final int size;
+ public final int length;
private static final long serialVersionUID = 1L;
/**
- * Construct with the specified start and end,
- * both of which are immutable.
+ * Construct a range with the specified start and end, both of which are
+ * mmutable. If the specified end is less than the specified start, the
+ * values will be swapped in the range.
*/
public Range(int start, int end) {
super();
- this.start = start;
- this.end = end;
- this.size = end - start + 1;
+ this.start = Math.min(start, end);
+ this.end = Math.max(start, end);
+ this.length = this.end - this.start + 1;
}
/**
@@ -81,7 +90,16 @@ public class Range
@Override
public String toString() {
- return '[' + this.start + ", " + this.end + ']'; //$NON-NLS-1$
+ StringBuilder sb = new StringBuilder();
+ this.toString(sb);
+ return sb.toString();
}
+ public void toString(StringBuilder sb) {
+ sb.append('[');
+ sb.append(this.start);
+ sb.append(", "); //$NON-NLS-1$
+ sb.append(this.end);
+ sb.append(']');
+ }
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/SortedListValueModelAdapter.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/SortedListValueModelAdapter.java
index 893b4b622b..3c7b7d2678 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/SortedListValueModelAdapter.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/SortedListValueModelAdapter.java
@@ -117,7 +117,7 @@ public class SortedListValueModelAdapter<E>
ArrayList<E> unsortedList = (ArrayList<E>) this.list.clone();
Collections.sort(this.list, this.comparator);
Range diffRange = ListTools.identityDifferenceRange(unsortedList, this.list);
- if (diffRange.size > 0) {
+ if (diffRange.length > 0) {
List<E> unsortedItems = unsortedList.subList(diffRange.start, diffRange.end + 1);
List<E> sortedItems = this.list.subList(diffRange.start, diffRange.end + 1);
this.fireItemsReplaced(LIST_VALUES, diffRange.start, sortedItems, unsortedItems);
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/SortedListValueModelWrapper.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/SortedListValueModelWrapper.java
index e92706ba69..47ddd0f785 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/SortedListValueModelWrapper.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/model/value/SortedListValueModelWrapper.java
@@ -234,7 +234,7 @@ public class SortedListValueModelWrapper<E>
ArrayList<E> unsortedList = (ArrayList<E>) this.sortedList.clone();
Collections.sort(this.sortedList, this.comparator);
Range diffRange = ListTools.identityDifferenceRange(unsortedList, this.sortedList);
- if (diffRange.size > 0) {
+ if (diffRange.length > 0) {
List<E> unsortedItems = unsortedList.subList(diffRange.start, diffRange.end + 1);
List<E> sortedItems = this.sortedList.subList(diffRange.start, diffRange.end + 1);
this.fireItemsReplaced(LIST_VALUES, diffRange.start, sortedItems, unsortedItems);
diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/RangeTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/RangeTests.java
index dcd959295a..b5ed5bdd3f 100644
--- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/RangeTests.java
+++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/RangeTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2007 Oracle. All rights reserved.
+ * Copyright (c) 2005, 2013 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0, which accompanies this distribution
* and is available at http://www.eclipse.org/legal/epl-v10.html.
@@ -12,8 +12,9 @@ package org.eclipse.jpt.common.utility.tests.internal;
import junit.framework.TestCase;
import org.eclipse.jpt.common.utility.internal.Range;
-public class RangeTests extends TestCase {
-
+public class RangeTests
+ extends TestCase
+{
public RangeTests(String name) {
super(name);
}
@@ -31,6 +32,19 @@ public class RangeTests extends TestCase {
assertFalse(range.includes(200));
}
+ public void testIncludes2() {
+ Range range = new Range(17, 5);
+ assertFalse(range.includes(-55));
+ assertFalse(range.includes(0));
+ assertFalse(range.includes(4));
+ assertTrue(range.includes(5));
+ assertTrue(range.includes(6));
+ assertTrue(range.includes(16));
+ assertTrue(range.includes(17));
+ assertFalse(range.includes(18));
+ assertFalse(range.includes(200));
+ }
+
public void testEquals() {
Range range1 = new Range(5, 17);
Range range2 = new Range(5, 17);
@@ -41,9 +55,8 @@ public class RangeTests extends TestCase {
assertEquals(range1.hashCode(), range2.hashCode());
range2 = new Range(17, 5);
- assertFalse(range1.equals(range2));
- assertFalse(range2.equals(range1));
- // although they are unequal, they can have the same hash code
+ assertEquals(range1, range2);
+ assertEquals(range2, range1);
assertEquals(range1.hashCode(), range2.hashCode());
range2 = new Range(5, 15);
@@ -70,5 +83,4 @@ public class RangeTests extends TestCase {
assertEquals(range2, range1);
assertEquals(range1.hashCode(), range2.hashCode());
}
-
}

Back to the top