[Arakhnę-Dev] [326] * Avoid ClassCastException from Class to Compable.

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 326
Author:   galland
Date:     2012-02-06 15:50:19 +0100 (Mon, 06 Feb 2012)
Log Message:
-----------
* Avoid ClassCastException from Class to Compable.

Modified Paths:
--------------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java

Added Paths:
-----------
    trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClassComparator.java

Added: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClassComparator.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClassComparator.java	                        (rev 0)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClassComparator.java	2012-02-06 14:50:19 UTC (rev 326)
@@ -0,0 +1,64 @@
+/* 
+ * $Id$
+ * 
+ * Copyright (C) 2012 Stephane GALLAND.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+
+package org.arakhne.vmutil;
+
+import java.util.Comparator;
+
+/**
+ * This comparator permits to compare two class objects.
+ * 
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ * @since 7.0
+ */
+public class ClassComparator implements Comparator<Class<?>> {
+
+	/**
+	 * Singleton of a class comparator.
+	 */
+	public static final ClassComparator SINGLETON = new ClassComparator();
+	
+	/**
+	 */
+	protected ClassComparator() {
+		//
+	}
+
+	/**
+	 * {@inheritDoc}
+	 */
+	@Override
+	public int compare(Class<?> o1, Class<?> o2) {
+		if (o1==o2) return 0;
+		if (o1==null) return Integer.MIN_VALUE;
+		if (o2==null) return Integer.MAX_VALUE;
+		String n1 = o1.getCanonicalName();
+		String n2 = o2.getCanonicalName();
+		if (n1==n2) return 0;
+		if (n1==null) return Integer.MIN_VALUE;
+		if (n2==null) return Integer.MAX_VALUE;
+		return n1.compareTo(n2);
+	}
+	
+}


Property changes on: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ClassComparator.java
___________________________________________________________________
Added: svn:mime-type
   + text/plain

Modified: trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java
===================================================================
--- trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java	2012-02-06 13:55:51 UTC (rev 325)
+++ trunk/arakhneVmutils/java/src/main/java/org/arakhne/vmutil/ReflectionUtil.java	2012-02-06 14:50:19 UTC (rev 326)
@@ -520,7 +520,7 @@
 	@SuppressWarnings("unchecked")
 	public static <T,I> Set<Class<? extends I>> getAllDirectInterfaces(Class<? extends T> lowestType, Class<T> highestType, Class<I> interfaceType) {
 		assert(lowestType!=null);
-		Set<Class<? extends I>> collection = new TreeSet<Class<? extends I>>();
+		Set<Class<? extends I>> collection = new TreeSet<Class<? extends I>>(ClassComparator.SINGLETON);
 		Class<?> type = lowestType;
 		boolean cont;
 		do {
@@ -571,7 +571,7 @@
 	 */
 	public static <T> Set<Class<?>> getAllDirectInterfaces(Class<? extends T> lowestType, Class<T> highestType) {
 		assert(lowestType!=null);
-		Set<Class<?>> collection = new TreeSet<Class<?>>();
+		Set<Class<?>> collection = new TreeSet<Class<?>>(ClassComparator.SINGLETON);
 		Class<?> type = lowestType;
 		boolean cont;
 		do {


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/