01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/util/BeanFloatComparator.java $
03: * $Id: BeanFloatComparator.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
04: ***********************************************************************************
05: *
06: * Copyright (c) 2005, 2006 The Sakai Foundation.
07: *
08: * Licensed under the Educational Community License, Version 1.0 (the"License");
09: * you may not use this file except in compliance with the License.
10: * You may obtain a copy of the License at
11: *
12: * http://www.opensource.org/licenses/ecl1.php
13: *
14: * Unless required by applicable law or agreed to in writing, software
15: * distributed under the License is distributed on an "AS IS" BASIS,
16: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17: * See the License for the specific language governing permissions and
18: * limitations under the License.
19: *
20: **********************************************************************************/package org.sakaiproject.tool.assessment.util;
21:
22: import java.text.DateFormat;
23: import java.text.SimpleDateFormat;
24: import java.util.Date;
25: import java.util.Map;
26:
27: /**
28: * DOCUMENTATION PENDING
29: *
30: * @author $author$
31: * @version $Id: BeanFloatComparator.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
32: */
33: public class BeanFloatComparator extends BeanSortComparator {
34: private String propertyName;
35:
36: /**
37: * The only public constructor. Requires a valid property name for a a Java
38: * Bean as a sole parameter.
39: *
40: * @param propertyName the property name for Java Bean to sort by
41: */
42: public BeanFloatComparator(String propertyName) {
43: this .propertyName = propertyName;
44: }
45:
46: /**
47: * Creates a new BeanFloatComparator object.
48: */
49: protected BeanFloatComparator() {
50: };
51:
52: /**
53: * standard compare method
54: *
55: * @param o1 object
56: * @param o2 object
57: *
58: * @return lt, eq, gt zero depending on whether o1 numerically lt,eq,gt o2
59: *
60: * @throws java.lang.UnsupportedOperationException DOCUMENTATION PENDING
61: */
62: public int compare(Object o1, Object o2) {
63: Map m1 = describeBean(o1);
64: Map m2 = describeBean(o2);
65: String s1 = (String) m1.get(propertyName);
66: String s2 = (String) m2.get(propertyName);
67: Float i1 = null;
68: Float i2 = null;
69: boolean firstFloatValid = true;
70: boolean secondFloatValid = true;
71:
72: try {
73: i1 = new Float(s1);
74: } catch (NumberFormatException e) {
75: firstFloatValid = false;
76: }
77:
78: try {
79: i2 = new Float(s2);
80: } catch (NumberFormatException e) {
81: secondFloatValid = false;
82: }
83:
84: int returnValue = 0;
85: if (firstFloatValid && secondFloatValid) {
86: if (i1 != null) {
87: returnValue = i1.compareTo(i2);
88: }
89: }
90: if (firstFloatValid && !secondFloatValid)
91: returnValue = 1;
92: if (!firstFloatValid && secondFloatValid)
93: returnValue = -1;
94: if (!firstFloatValid && !secondFloatValid)
95: returnValue = 0;
96:
97: return returnValue;
98: }
99: }
|