~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to test/org/hibernate/test/hql/Classification.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hibernate.test.hql;
 
2
 
 
3
import java.io.Serializable;
 
4
import java.util.HashMap;
 
5
 
 
6
/**
 
7
 * Mimic a JDK 5 enum.
 
8
 *
 
9
 * @author Steve Ebersole
 
10
 */
 
11
public class Classification implements Serializable, Comparable {
 
12
 
 
13
        public static final Classification COOL = new Classification( "COOL", 0 );
 
14
        public static final Classification LAME = new Classification( "LAME", 1 );
 
15
 
 
16
        private static final HashMap INSTANCES = new HashMap();
 
17
        static {
 
18
                INSTANCES.put( COOL.name, COOL );
 
19
                INSTANCES.put( LAME.name, LAME );
 
20
        }
 
21
 
 
22
        private final String name;
 
23
        private final int ordinal;
 
24
        private final int hashCode;
 
25
 
 
26
        private Classification(String name, int ordinal) {
 
27
                this.name = name;
 
28
                this.ordinal = ordinal;
 
29
 
 
30
                int hashCode = name.hashCode();
 
31
                hashCode = 29 * hashCode + ordinal;
 
32
                this.hashCode = hashCode;
 
33
        }
 
34
 
 
35
        public String name() {
 
36
                return name;
 
37
        }
 
38
 
 
39
        public int ordinal() {
 
40
                return ordinal;
 
41
        }
 
42
 
 
43
        public boolean equals(Object obj) {
 
44
                return compareTo( obj ) == 0;
 
45
        }
 
46
 
 
47
        public int compareTo(Object o) {
 
48
                int otherOrdinal = ( ( Classification ) o ).ordinal;
 
49
                if ( ordinal == otherOrdinal ) {
 
50
                        return 0;
 
51
                }
 
52
                else if ( ordinal > otherOrdinal ) {
 
53
                        return 1;
 
54
                }
 
55
                else {
 
56
                        return -1;
 
57
                }
 
58
        }
 
59
 
 
60
        public int hashCode() {
 
61
                return hashCode;
 
62
        }
 
63
 
 
64
        public static Classification valueOf(String name) {
 
65
                return ( Classification ) INSTANCES.get( name );
 
66
        }
 
67
 
 
68
        public static Classification valueOf(Integer ordinal) {
 
69
                if ( ordinal == null ) {
 
70
                        return null;
 
71
                }
 
72
                switch ( ordinal.intValue() ) {
 
73
                        case 0: return COOL;
 
74
                        case 1: return LAME;
 
75
                        default: throw new IllegalArgumentException( "unknown classification ordinal [" + ordinal + "]" );
 
76
                }
 
77
        }
 
78
}