Source code: com/clra/rowing/DefaultRowingSessionTypeComparator.java
1 /*
2 * Copyright (c) Carnegie Lake Rowing Association 2002. All rights reserved.
3 * Distributed under the GPL license. See doc/COPYING.
4 * $RCSfile: DefaultRowingSessionTypeComparator.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 RowingSessionLevel instances as well as Strings. The default
19 * order is PRACTICE, REGATTA.
20 *
21 * @version $Revision: 1.3 $ $Date: 2003/02/26 03:38:45 $
22 * @author <a href="mailto:rphall@pluto.njcc.com">Rick Hall</a>
23 */
24 public class DefaultRowingSessionTypeComparator
25 implements Comparator, Serializable {
26
27 private final static String base =
28 DefaultRowingSessionTypeComparator.class.getName();
29 private final static Category theLog = Category.getInstance( base );
30
31 private final static String makeKey( String s ) {
32 return s.trim().toUpperCase();
33 }
34
35 private final static String makeKey( RowingSessionType rst ) {
36 return makeKey( rst.getName() );
37 }
38
39 private final static Map ordinalMap = new HashMap();
40 static {
41
42 String key = makeKey( RowingSessionType.NAME_PRACTICE );
43 Integer val = new Integer( 1 );
44 ordinalMap.put( key, val );
45
46 key = makeKey( RowingSessionType.NAME_REGATTA );
47 val = new Integer( 2 );
48 ordinalMap.put( key, val );
49
50 } // static
51
52 private final static Integer getOrdinal( String s ) {
53 return (Integer) ordinalMap.get( makeKey(s) );
54 }
55
56 private final static Integer getOrdinal( RowingSessionType rst ) {
57 return (Integer) ordinalMap.get( makeKey(rst) );
58 }
59
60 private final static Integer getOrdinal( Object o ) {
61
62 Integer retVal = null;
63 if ( o instanceof RowingSessionType ) {
64 retVal = getOrdinal( ((RowingSessionType) o) );
65 }
66 else if ( o instanceof String ) {
67 retVal = getOrdinal( ((String) o) );
68 }
69
70 return retVal;
71 } // getOrdinal(Object)
72
73 /**
74 * Compares a level to other levels and valid level names. The default
75 * order is LTR then REGULAR.
76 * @param o a non-null RowingSessionType or a String NAME of a level
77 */
78 public static int staticCompare( Object o1, Object o2 ) {
79
80 Integer rank1 = getOrdinal( o1 );
81 Integer rank2 = getOrdinal( o2 );
82
83 if ( o1 == null || o2 == null ) {
84 String msg = "cannot compare '" + o1 + "' and '" + o2 + "'";
85 String err = "DefaultRowingSessionTypeComparator.staticCompare: " + msg;
86 theLog.error( err );
87 throw new ClassCastException( msg );
88 }
89
90 return rank1.compareTo( rank2 );
91 } // staticCompare(Object,Object)
92
93 /** @see #staticCompare(Object,Object) */
94 public int compare( Object o1, Object o2 ) {
95 return staticCompare( o1, o2 );
96 }
97
98 } // DefaultRowingSessionTypeComparator
99
100 /*
101 * $Log: DefaultRowingSessionTypeComparator.java,v $
102 * Revision 1.3 2003/02/26 03:38:45 rphall
103 * Added copyright and GPL license
104 *
105 * Revision 1.2 2002/02/18 18:04:07 rphall
106 * Ran dos2unix to remove ^M (carriage return) from end of lines
107 *
108 * Revision 1.1.1.1 2002/01/03 21:57:28 rphall
109 * Initial load, 5th try, Jan-03-2002 4:57 PM
110 *
111 * Revision 1.2 2001/12/07 01:07:24 rphall
112 * Checkpt: before debugging slow perf
113 *
114 * Revision 1.1 2001/12/06 21:21:35 rphall
115 * Comparator for default ordering
116 *
117 */
118