You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by le...@apache.org on 2007/07/13 05:24:15 UTC
svn commit: r555856 - in /harmony/enhanced/classlib/trunk/modules/luni/src:
main/java/java/util/Collections.java
test/api/common/tests/api/java/util/CollectionsTest.java
Author: leoli
Date: Thu Jul 12 20:24:14 2007
New Revision: 555856
URL: http://svn.apache.org/viewvc?view=rev&rev=555856
Log:
Apply patch for the difference of comparation order in binarySearch between RI and Harmony.
Modified:
harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java
harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/CollectionsTest.java
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java?view=diff&rev=555856&r1=555855&r2=555856
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Collections.java Thu Jul 12 20:24:14 2007
@@ -1472,12 +1472,12 @@
return -1;
}
- Comparable<T> key = (Comparable<T>) object;
+
if (!(list instanceof RandomAccess)) {
- ListIterator<T> it = (ListIterator<T>) list.listIterator();
+ ListIterator<? extends Comparable<? super T>> it = list.listIterator();
while (it.hasNext()) {
int result;
- if ((result = key.compareTo(it.next())) <= 0) {
+ if ((result = -it.next().compareTo(object)) <= 0) {
if (result == 0) {
return it.previousIndex();
}
@@ -1490,7 +1490,7 @@
int low = 0, mid = list.size(), high = mid - 1, result = -1;
while (low <= high) {
mid = (low + high) >> 1;
- if ((result = key.compareTo((T) list.get(mid))) > 0) {
+ if ((result = -list.get(mid).compareTo(object)) > 0) {
low = mid + 1;
} else if (result == 0) {
return mid;
@@ -1530,7 +1530,7 @@
ListIterator<? extends T> it = list.listIterator();
while (it.hasNext()) {
int result;
- if ((result = comparator.compare(object, it.next())) <= 0) {
+ if ((result = -comparator.compare(it.next(), object)) <= 0) {
if (result == 0) {
return it.previousIndex();
}
@@ -1543,7 +1543,7 @@
int low = 0, mid = list.size(), high = mid - 1, result = -1;
while (low <= high) {
mid = (low + high) >> 1;
- if ((result = comparator.compare(object, list.get(mid))) > 0) {
+ if ((result = -comparator.compare(list.get(mid),object)) > 0) {
low = mid + 1;
} else if (result == 0) {
return mid;
Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/CollectionsTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/CollectionsTest.java?view=diff&rev=555856&r1=555855&r2=555856
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/CollectionsTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/api/common/tests/api/java/util/CollectionsTest.java Thu Jul 12 20:24:14 2007
@@ -1911,6 +1911,70 @@
assertEquals(errMsg, e.getCause().getMessage());
}
}
+
+ public void test_binarySearch_asymmetry_with_comparator() throws Exception{
+ List list = new ArrayList();
+ String s1 = new String("a");
+ String s2 = new String("aa");
+ String s3 = new String("aaa");
+ list.add(s1);
+ list.add(s2);
+ list.add(s3);
+ Collections.sort(list);
+ Object o = Collections.binarySearch(list, 1, new StringComparator());
+ assertSame(0,o);
+ }
+
+ public void test_binarySearch_asymmetry() throws Exception{
+ List list = new LinkedList();
+ String s1 = new String("a");
+ String s2 = new String("aa");
+ String s3 = new String("aaa");
+ list.add(new MyComparable(s1));
+ list.add(new MyComparable(s2));
+ list.add(new MyComparable(s3));
+ Collections.sort(list);
+ Object o = Collections.binarySearch(list, 1);
+ assertSame(0,o);
+ }
+
+
+ private class MyComparable implements Comparable {
+
+ public String s;
+
+ public MyComparable(String s) {
+ this.s = s;
+
+ }
+
+ public int compareTo(Object another) {
+ int length = 0;
+ if (another instanceof MyComparable) {
+ length = (((MyComparable) another).s).length();
+ } else {
+ length = (Integer) another;
+ }
+ return s.length() - length;
+ }
+
+ }
+
+ private class StringComparator implements Comparator {
+
+ public int compare(Object object1, Object object2) {
+ String s = (String) object1;
+ int length;
+ if(object2 instanceof String){
+ length = ((String)object2).length();
+ }
+ else
+ {
+ length = (Integer) object2;
+ }
+ return s.length() - length;
+ }
+ }
/**