1 16 17 package org.springframework.core.enums; 18 19 import org.springframework.util.Assert; 20 21 27 public abstract class AbstractLabeledEnum implements LabeledEnum { 28 29 32 protected AbstractLabeledEnum() { 33 } 34 35 public Class getType() { 36 return getClass(); 37 } 38 39 40 44 public abstract Comparable getCode(); 45 46 50 public abstract String getLabel(); 51 52 53 public int compareTo(Object obj) { 54 Assert.isTrue(obj instanceof AbstractLabeledEnum, "You may only compare LabeledEnums"); 55 LabeledEnum other = (LabeledEnum) obj; 56 Assert.isTrue(this.getType().equals(other.getType()), 57 "You may only compare LabeledEnums of the same type"); 58 return this.getCode().compareTo(other.getCode()); 59 } 60 61 public boolean equals(Object obj) { 62 if (this == obj) { 63 return true; 64 } 65 if (!(obj instanceof LabeledEnum)) { 66 return false; 67 } 68 LabeledEnum other = (LabeledEnum) obj; 69 return this.getType().equals(other.getType()) && this.getCode().equals(other.getCode()); 70 } 71 72 public int hashCode() { 73 return (getType().hashCode() * 29 + getCode().hashCode()); 74 } 75 76 public String toString() { 77 return getLabel(); 78 } 79 80 } 81 | Popular Tags |