[Arakhnę-Dev] [34] Bug fix: the Comparable weak/ soft references throw ClassCastException when the object passed to compareTo () is not castable to the generic T. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
- To: dev@xxxxxxxxxxx
- Subject: [Arakhnę-Dev] [34] Bug fix: the Comparable weak/ soft references throw ClassCastException when the object passed to compareTo () is not castable to the generic T.
- From: subversion@xxxxxxxxxxxxx
- Date: Fri, 20 Feb 2009 16:20:22 +0100
Revision: 34
Author: galland
Date: 2009-02-20 16:20:22 +0100 (Fri, 20 Feb 2009)
Log Message:
-----------
Bug fix: the Comparable weak/soft references throw ClassCastException when the object passed to compareTo() is not castable to the generic T.
Modified Paths:
--------------
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/ComparableSoftReference.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/ComparableWeakReference.java
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/ComparableSoftReference.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/ComparableSoftReference.java 2009-02-03 12:37:00 UTC (rev 33)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/ComparableSoftReference.java 2009-02-20 15:20:22 UTC (rev 34)
@@ -55,20 +55,9 @@
* @param o {@inheritDoc}
* @return {@inheritDoc}
*/
- @SuppressWarnings("unchecked")
@Override
public boolean equals(Object o) {
- if (super.equals(o)) return true;
-
- try {
- if (o instanceof Reference)
- return (compareToRef((Reference)o)==0);
-
- return compareToObject((T)o)==0;
- }
- catch(Exception _) {
- return false;
- }
+ return compareTo(o)==0;
}
/** Compare this reference to the specified object
@@ -80,17 +69,32 @@
*/
@SuppressWarnings("unchecked")
public int compareTo(Object o) {
- try {
- if (o instanceof Reference)
- return compareToRef((Reference)o);
+ Object oth = (o instanceof Reference) ? ((Reference<?>)o).get() : o;
+ T cur = get();
+
+ if (oth==null && cur==null) return 0;
+ if (cur==null) return 1;
+ if (oth==null) return -1;
- return compareToObject((T)o);
+ if (cur instanceof Comparable) {
+ try {
+ return ((Comparable<Object>)cur).compareTo(oth);
+ }
+ catch(Throwable _) {
+ //
+ }
}
- catch(Exception _) {
- int a = hashCode();
- int b = o.hashCode();
- return a - b;
+
+ if (oth instanceof Comparable) {
+ try {
+ return -((Comparable<Object>)oth).compareTo(cur);
+ }
+ catch(Throwable _) {
+ //
+ }
}
+
+ return oth.hashCode() - cur.hashCode();
}
/** Compare this reference to the specified object
@@ -100,29 +104,13 @@
* @param o the object to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
+ * @deprecated call {@link #compareTo(Object)} insteed
*/
- @SuppressWarnings("unchecked")
- public int compareToRef(Reference<T> o) {
- T obj = o.get();
- T cur = get();
-
- if (((cur==null)&&(obj==null))||(cur==obj)) return 0;
- if (cur==null) return -1;
- if (obj==null) return 1;
-
- if (cur instanceof Comparable) {
- Comparable cmp = (Comparable<?>)cur;
- return cmp.compareTo(obj);
- }
-
- if ((cur==obj)||(cur.equals(obj))) return 0;
-
- int a = cur.hashCode();
- int b = obj.hashCode();
-
- return a - b;
+ @Deprecated
+ public int compareToObject(T o) {
+ return compareTo(o);
}
-
+
/** Compare this reference to the specified object
* based on the {@link Object#hashCode()} if the
* references are not equals.
@@ -130,28 +118,11 @@
* @param o the object to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
+ * @deprecated call {@link #compareTo(Object)} insteed
*/
- @SuppressWarnings("unchecked")
- public int compareToObject(T o) {
- if (super.equals(o)) return 0;
-
- T cur = get();
-
- if ((cur==null)&&(o==null)) return 0;
- if (cur==null) return -1;
- if (o==null) return 1;
-
- if (cur instanceof Comparable) {
- Comparable cmp = (Comparable)cur;
- return cmp.compareTo(o);
- }
-
- if ((cur==o)||(cur.equals(o))) return 0;
-
- int a = cur.hashCode();
- int b = o.hashCode();
-
- return a - b;
+ @Deprecated
+ public int compareToRef(Reference<T> o) {
+ return compareTo(o);
}
/** {@inheritDoc}
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/ComparableWeakReference.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/ComparableWeakReference.java 2009-02-03 12:37:00 UTC (rev 33)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/ComparableWeakReference.java 2009-02-20 15:20:22 UTC (rev 34)
@@ -55,20 +55,9 @@
* @param o {@inheritDoc}
* @return {@inheritDoc}
*/
- @SuppressWarnings("unchecked")
@Override
public boolean equals(Object o) {
- if (super.equals(o)) return true;
-
- try {
- if (o instanceof Reference)
- return (compareToRef((Reference)o)==0);
-
- return compareToObject((T)o)==0;
- }
- catch(Exception _) {
- return false;
- }
+ return compareTo(o)==0;
}
/** Compare this reference to the specified object
@@ -80,17 +69,32 @@
*/
@SuppressWarnings("unchecked")
public int compareTo(Object o) {
- try {
- if (o instanceof Reference)
- return compareToRef((Reference)o);
+ Object oth = (o instanceof Reference) ? ((Reference<?>)o).get() : o;
+ T cur = get();
+
+ if (oth==null && cur==null) return 0;
+ if (cur==null) return 1;
+ if (oth==null) return -1;
- return compareToObject((T)o);
+ if (cur instanceof Comparable) {
+ try {
+ return ((Comparable<Object>)cur).compareTo(oth);
+ }
+ catch(Throwable _) {
+ //
+ }
}
- catch(Exception _) {
- int a = hashCode();
- int b = o.hashCode();
- return a - b;
+
+ if (oth instanceof Comparable) {
+ try {
+ return -((Comparable<Object>)oth).compareTo(cur);
+ }
+ catch(Throwable _) {
+ //
+ }
}
+
+ return oth.hashCode() - cur.hashCode();
}
/** Compare this reference to the specified object
@@ -100,28 +104,11 @@
* @param o the object to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
+ * @deprecated call {@link #compareTo(Object)} insteed
*/
- @SuppressWarnings("unchecked")
+ @Deprecated
public int compareToObject(T o) {
- if (super.equals(o)) return 0;
-
- T cur = get();
-
- if ((cur==null)&&(o==null)) return 0;
- if (cur==null) return -1;
- if (o==null) return 1;
-
- if (cur instanceof Comparable) {
- Comparable cmp = (Comparable)cur;
- return cmp.compareTo(o);
- }
-
- if ((cur==o)||(cur.equals(o))) return 0;
-
- int a = cur.hashCode();
- int b = o.hashCode();
-
- return a - b;
+ return compareTo(o);
}
/** Compare this reference to the specified object
@@ -131,27 +118,11 @@
* @param o the object to be compared.
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the specified object.
+ * @deprecated call {@link #compareTo(Object)} insteed
*/
- @SuppressWarnings("unchecked")
+ @Deprecated
public int compareToRef(Reference<T> o) {
- Object obj = o.get();
- Object cur = get();
-
- if (((cur==null)&&(obj==null))||(obj==cur)) return 0;
- if (cur==null) return -1;
- if (obj==null) return 1;
-
- if (cur instanceof Comparable) {
- Comparable cmp = (Comparable)cur;
- return cmp.compareTo(obj);
- }
-
- if ((cur==obj)||(cur.equals(obj))) return 0;
-
- int a = cur.hashCode();
- int b = obj.hashCode();
-
- return a - b;
+ return compareTo(o);
}
/** {@inheritDoc}