commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Samuel Fleischle" <sfleisc...@key-software.com>
Subject [lang] ClassCastException in Enum.compareTo
Date Tue, 11 Sep 2007 12:42:23 GMT
Hi,
I updated from Commons Lang 2.1 to Commons Lang 2.3 and got some issues with the changed compareTo()
method of Enum.
 
Here is my simplified Enum:
 
public class ItemStatus extends Enum {
 
      public static final ItemStatus CANCEL = new ItemStatus("CANCEL");
public static final ItemStatus SHIPPED = new ItemStatus("SHIPPED");
      public static final ItemStatus MOVED = new ItemStatus("MOVED");
 
/** Contains each status. */
      private static Set statuses;
 
// -------------------------------------------------------- Constructors
 
      protected ItemStatus(String name) {
            super(name);
            System.out.println(this.getClass().getName());
            buildStatusSet();
      }
 
      // -------------------------------------------------------- Static Methods
 
      /**
       * Returns the item status enumeration object for the given status name.
       * 
       * @param itemStatus
       *            name of the item status
       * @return the enumeration object, or <code>null</code> if it does not
       *         exist
       */
      public static ItemStatus valueOf(String itemStatus) {
            return (ItemStatus) getEnum(ItemStatus.class, itemStatus);
      }
 
      // -------------------------------------------------------- Public Methods
 
      /**
       * {@inheritDoc}
       */
      // @Override
      public final Class getEnumClass() {
            return ItemStatus.class;
      }
 
      /**
       * Gets all defined ItemStatus.
       * 
       * @return the enum object List
       * @see org.apache.commons.lang.enums.Enum#getEnumList(Class)
       */
      public static List getEnumList() {
            return getEnumList(ItemStatus.class);
      }
 
      // -------------------------------------------------------- Private Methods
 
      /**
       * Adds the status name to a Set of status names.
       */
      private void buildStatusSet() {
            if (statuses == null) {
                  statuses = new TreeSet();
            }
            statuses.add(this);
      }
}
 
And I got the ClassCastExcetion in the private "buildStatusSet()-Method if I try to add the
status to the TreeSet. The TreeSet calls the compareTo-Method of the Enum. In Commons Lang
2.2 this method was changed with an additional comparison of the classes.
In my constructor I added a System.out.println of the instantiated classes and got the following
output:
 
com.myapp.common.model.order.ItemStatus$1
com.myapp.common.model.order.ItemStatus$2
com.myapp.common.model.order.ItemStatus$3
 
The compareTo-Method now tries to compare ItemStatus$1 with ItemStatus$2 and says to me, that
my ItemStatus-Enums are different classes.
 
I saw in JIRA there are some other issues in ValuedEnum.compareTo(). Is there a bug in the
Enum.compareTo() implementation which got changed in Lang 2.2?
 
Thanks in advance for any help or comment on this issue.
 
Regards,
Sam
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message