KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > enums > AbstractLabeledEnum


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.core.enums;
18
19 import org.springframework.util.Assert;
20
21 /**
22  * Abstract base superclass for LabeledEnum implementations.
23  *
24  * @author Keith Donald
25  * @since 1.2.2
26  */

27 public abstract class AbstractLabeledEnum implements LabeledEnum {
28
29     /**
30      * Create a new AbstractLabeledEnum instance.
31      */

32     protected AbstractLabeledEnum() {
33     }
34
35     public Class JavaDoc getType() {
36         return getClass();
37     }
38
39
40     /*
41      * This abstract method declaration shadows the method in the LabeledEnum interface.
42      * This is necessary to properly work on Sun's JDK 1.3 classic VM in all cases.
43      */

44     public abstract Comparable JavaDoc getCode();
45
46     /*
47      * This abstract method declaration shadows the method in the LabeledEnum interface.
48      * This is necessary to properly work on Sun's JDK 1.3 classic VM in all cases.
49      */

50     public abstract String JavaDoc getLabel();
51
52
53     public int compareTo(Object JavaDoc 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 JavaDoc 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 JavaDoc toString() {
77         return getLabel();
78     }
79
80 }
81
Popular Tags