Source code: com/clra/rowing/DefaultRowingSessionStateComparator.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: DefaultRowingSessionStateComparator.java,v $
5 * $Date: 2003/02/26 03:38:45 $
6 * $Revision: 1.3 $
7 */
8
9 package com.clra.rowing;
10
11 import java.io.Serializable;
12 import java.util.Comparator;
13 import java.util.HashMap;
14 import java.util.Map;
15 import org.apache.log4j.Category;
16
17 /**
18 * Compares RowingSessionState instances as well as Strings. The default
19 * order is NEW, TENATIVE, OPEN, LOCKED, BOATING1, BOATING2, COMPLETE,
20 * INVOICING, CLOSED, CANCELED.
21 *
22 * @version $Revision: 1.3 $ $Date: 2003/02/26 03:38:45 $
23 * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
24 */
25 public class DefaultRowingSessionStateComparator
26 implements Comparator, Serializable {
27
28 private final static String base =
29 DefaultRowingSessionStateComparator.class.getName();
30 private final static Category theLog = Category.getInstance( base );
31
32 private final static String makeKey( String s ) {
33 return s.trim().toUpperCase();
34 }
35
36 private final static String makeKey( RowingSessionState rsl ) {
37 return makeKey( rsl.getName() );
38 }
39
40 private final static Map ordinalMap = new HashMap();
41 static {
42
43 String key = makeKey( RowingSessionState.NAME_NEW );
44 Integer val = new Integer( 1 );
45 ordinalMap.put( key, val );
46
47 key = makeKey( RowingSessionState.NAME_TENATIVE );
48 val = new Integer( 2 );
49 ordinalMap.put( key, val );
50
51 key = makeKey( RowingSessionState.NAME_OPEN );
52 val = new Integer( 3 );
53 ordinalMap.put( key, val );
54
55 key = makeKey( RowingSessionState.NAME_LOCKED );
56 val = new Integer( 4 );
57 ordinalMap.put( key, val );
58
59 key = makeKey( RowingSessionState.NAME_BOATING1 );
60 val = new Integer( 5 );
61 ordinalMap.put( key, val );
62
63 key = makeKey( RowingSessionState.NAME_BOATING2 );
64 val = new Integer( 6 );
65 ordinalMap.put( key, val );
66
67 key = makeKey( RowingSessionState.NAME_COMPLETE );
68 val = new Integer( 7 );
69 ordinalMap.put( key, val );
70
71 key = makeKey( RowingSessionState.NAME_INVOICING );
72 val = new Integer( 8 );
73 ordinalMap.put( key, val );
74
75 key = makeKey( RowingSessionState.NAME_CLOSED );
76 val = new Integer( 9 );
77 ordinalMap.put( key, val );
78
79 key = makeKey( RowingSessionState.NAME_CANCELLED );
80 val = new Integer( 10 );
81 ordinalMap.put( key, val );
82
83 } // static
84
85 private final static Integer getOrdinal( String s ) {
86 return (Integer) ordinalMap.get( makeKey(s) );
87 }
88
89 private final static Integer getOrdinal( RowingSessionState rsl ) {
90 return (Integer) ordinalMap.get( makeKey(rsl) );
91 }
92
93 private final static Integer getOrdinal( Object o ) {
94
95 Integer retVal = null;
96 if ( o instanceof RowingSessionState ) {
97 retVal = getOrdinal( ((RowingSessionState) o) );
98 }
99 else if ( o instanceof String ) {
100 retVal = getOrdinal( ((String) o) );
101 }
102
103 return retVal;
104 } // getOrdinal(Object)
105
106 /**
107 * Compares a level to other levels and valid level names. The default
108 * order is LTR then REGULAR.
109 * @param o a non-null RowingSessionState or a String NAME of a level
110 */
111 public static int staticCompare( Object o1, Object o2 ) {
112
113 Integer rank1 = getOrdinal( o1 );
114 Integer rank2 = getOrdinal( o2 );
115
116 if ( o1 == null || o2 == null ) {
117 String msg = "cannot compare '" + o1 + "' and '" + o2 + "'";
118 String err = "DefaultRowingSessionStateComparator.staticCompare: " + msg;
119 theLog.error( err );
120 throw new ClassCastException( msg );
121 }
122
123 return rank1.compareTo( rank2 );
124 } // staticCompare(Object,Object)
125
126 /** @see #staticCompare(Object,Object) */
127 public int compare( Object o1, Object o2 ) {
128 return staticCompare( o1, o2 );
129 }
130
131 } // DefaultRowingSessionStateComparator
132
133 /*
134 * $Log: DefaultRowingSessionStateComparator.java,v $
135 * Revision 1.3 2003/02/26 03:38:45 rphall
136 * Added copyright and GPL license
137 *
138 * Revision 1.2 2002/02/18 18:04:04 rphall
139 * Ran dos2unix to remove ^M (carriage return) from end of lines
140 *
141 * Revision 1.1.1.1 2002/01/03 21:57:28 rphall
142 * Initial load, 5th try, Jan-03-2002 4:57 PM
143 *
144 * Revision 1.2 2001/12/07 01:07:24 rphall
145 * Checkpt: before debugging slow perf
146 *
147 * Revision 1.1 2001/12/06 21:21:35 rphall
148 * Comparator for default ordering
149 *
150 */
151