001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/util/BeanSort.java $
003: * $Id: BeanSort.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.util;
021:
022: import java.util.Arrays;
023: import java.util.Collection;
024:
025: /**
026: * DOCUMENTATION PENDING
027: *
028: * @author $author$
029: * @version $Id: BeanSort.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
030: */
031: public class BeanSort {
032: private Collection collection;
033: private String property;
034: private BeanSortComparator bsc;
035: private boolean string = true;
036: private boolean numeric = false;
037: private boolean date = false;
038:
039: /**
040: * The only public constructor. Requires a valid property name for a a Java
041: * Bean as a sole parameter.
042: *
043: * @param c the property name for Java Bean to sort by
044: * @param onProperty DOCUMENTATION PENDING
045: */
046: public BeanSort(Collection c, String onProperty) {
047: collection = c;
048: property = onProperty;
049: }
050:
051: /**
052: * Creates a new BeanSort object.
053: */
054: private BeanSort() {
055: }
056:
057: /**
058: * DOCUMENTATION PENDING
059: *
060: * @return DOCUMENTATION PENDING
061: */
062: public Object[] arraySort() {
063: Object[] array = collection.toArray();
064: Arrays.sort(array, getBeanSortComparator(property));
065: return array;
066: }
067:
068: /**
069: * DOCUMENTATION PENDING
070: *
071: * @return DOCUMENTATION PENDING
072: */
073: public Collection sort() {
074: Object[] array = arraySort();
075: collection.clear();
076: for (int i = 0; i < array.length; i++) {
077: collection.add(array[i]);
078: }
079:
080: return collection;
081: }
082:
083: /**
084: * DOCUMENTATION PENDING
085: *
086: * @return DOCUMENTATION PENDING
087: */
088: public Collection sortDesc() {
089: Object[] array = arraySort();
090: collection.clear();
091: for (int i = array.length - 1; i >= 0; i--) {
092: collection.add(array[i]);
093: }
094:
095: return collection;
096: }
097:
098: /**
099: * DOCUMENTATION PENDING
100: *
101: * @param property DOCUMENTATION PENDING
102: *
103: * @return DOCUMENTATION PENDING
104: */
105: private BeanSortComparator getBeanSortComparator(String property) {
106: BeanSortComparator bsc = null;
107:
108: if (string) {
109: bsc = new BeanSortComparator(property);
110: } else if (numeric) {
111: bsc = new BeanFloatComparator(property);
112: } else if (date) {
113: bsc = new BeanDateComparator(property);
114: }
115:
116: return bsc;
117: }
118:
119: /**
120: * DOCUMENTATION PENDING
121: */
122: public void toStringSort() {
123: string = true;
124: numeric = false;
125: date = false;
126: }
127:
128: /**
129: * DOCUMENTATION PENDING
130: */
131: public void toNumericSort() {
132: string = false;
133: numeric = true;
134: date = false;
135: }
136:
137: /**
138: * @todo add date support
139: */
140:
141: public void toDateSort() {
142: string = false;
143: numeric = false;
144: date = true;
145: }
146: }
|