commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ralf Hauser (JIRA)" <j...@apache.org>
Subject [jira] Commented: (LANG-259) ValuedEnum.compareTo(Object other) not typesafe - it easily could be...
Date Mon, 22 May 2006 16:01:30 GMT
    [ http://issues.apache.org/jira/browse/LANG-259?page=comments#action_12412798 ] 

Ralf Hauser commented on LANG-259:
----------------------------------

Before just comparing the 
   iValue - ((ValuedEnum) other).iValue
it should be checked that the classes are the same!

		Enum otherEnum = (Enum) other;
		String thisClassName = this.getClass().getName();
		String otherClassName = otherEnum.getClass().getName();
		if (!otherClassName.equals(thisClassName)) {
			log.warn("enum classes are not equal \"" + thisClassName + "\" <> \"" + otherClassName
+ "\"");
			throw new ClassCastException("enum classes are not equal \"" + thisClassName + "\" <>
\"" + otherClassName
					+ "\"");
		}

Furthermore, it might be recommendable to make some methods of org.apache.commons.lang.enums.Enum
at least "protected" such that ValuedEnum can use them too:

If available, I would love to also have the more sophisticated checks, Enum.compareTo(Object
other) does also used in ValuedEnum.compareTo(Object other):

if (res == 0) {
			String otherName = otherEnum.getName();
			if (other.getClass() != this.getClass()) {
				if (other.getClass().getName().equals(this.getClass().getName())) {
					return iName.compareTo(getNameInOtherClassLoader(other));
				}
			}
			res = iName.compareTo(otherName);
			if (res != 0) {
				log.warn("enum names are not equal \"" + otherName + "\" <> \"" + this.getName()
+ "\"");
				throw new ClassCastException("enum names are not equal \"" + otherName + "\" <>
\"" + this.getName()
						+ "\"");
                       }
}

> ValuedEnum.compareTo(Object other) not typesafe - it easily could be...
> -----------------------------------------------------------------------
>
>          Key: LANG-259
>          URL: http://issues.apache.org/jira/browse/LANG-259
>      Project: Commons Lang
>         Type: Bug

>     Versions: 2.1
>  Environment: all
>     Reporter: Ralf Hauser

>
> int org.apache.commons.lang.enums.ValuedEnum.compareTo(Object other)
>  is not typesafe - if the int-values are the same, it will return "0" even for two totally
different sub-classes of ValuedEnum

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message