01: /**********************************************************************************
02: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/util/BeanIntegerComparator.java $
03: * $Id: BeanIntegerComparator.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.util.Map;
23:
24: /**
25: * DOCUMENTATION PENDING
26: *
27: * @author $author$
28: * @version $Id: BeanIntegerComparator.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
29: */
30: public class BeanIntegerComparator extends BeanSortComparator {
31: private String propertyName;
32:
33: /**
34: * The only public constructor. Requires a valid property name for a a Java
35: * Bean as a sole parameter.
36: *
37: * @param propertyName the property name for Java Bean to sort by
38: */
39: public BeanIntegerComparator(String propertyName) {
40: this .propertyName = propertyName;
41: }
42:
43: /**
44: * Creates a new BeanIntegerComparator object.
45: */
46: protected BeanIntegerComparator() {
47: };
48:
49: /**
50: * standard compare method
51: *
52: * @param o1 object
53: * @param o2 object
54: *
55: * @return lt, eq, gt zero depending on whether o1 numerically lt,eq,gt o2
56: *
57: * @throws java.lang.UnsupportedOperationException DOCUMENTATION PENDING
58: */
59: public int compare(Object o1, Object o2) {
60: Map m1 = describeBean(o1);
61: Map m2 = describeBean(o2);
62: String s1 = (String) m1.get(propertyName);
63: String s2 = (String) m2.get(propertyName);
64: Integer i1 = null;
65: Integer i2 = null;
66: try {
67: i1 = new Integer(s1);
68: i2 = new Integer(s2);
69: } catch (Throwable t) {
70: throw new java.lang.UnsupportedOperationException(
71: "Error in comparing integer objects:" + t);
72: }
73:
74: return i1.compareTo(i2);
75: }
76: }
|