01: /**
02: * $Id: CustomerComparator.java,v 1.1 2005/08/12 07:46:04 nk137934 Exp $
03: * Copyright 2005 Sun Microsystems, Inc. All
04: * rights reserved. Use of this product is subject
05: * to license terms. Federal Acquisitions:
06: * Commercial Software -- Government Users
07: * Subject to Standard License Terms and
08: * Conditions.
09: *
10: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
11: * are trademarks or registered trademarks of Sun Microsystems,
12: * Inc. in the United States and other countries.
13: */package com.sun.portal.sapportlet.customer;
14:
15: import java.util.Comparator;
16:
17: public class CustomerComparator implements Comparator {
18:
19: /* Possible values for sort order*/
20: public static final int SORT_ORDER_ASCENDING = 1;
21: public static final int SORT_ORDER_DESCENDING = -1;
22:
23: /* Possible values for sort field*/
24: public static final int SORT_FIELD_CUST_NUMBER = 1000;
25: public static final int SORT_FIELD_CUST_NAME = 2000;
26: public static final int SORT_FIELD_CUST_CITY = 3000;
27:
28: private int sortField;
29: private int sortOrder;
30:
31: private CustomerComparator(int sortField, int sortOrder) {
32:
33: this .sortField = sortField;
34: this .sortOrder = sortOrder;
35: }
36:
37: public static CustomerComparator getComparator(int sortField,
38: int sortOrder) {
39:
40: CustomerComparator comparator = new CustomerComparator(
41: sortField, sortOrder);
42: return comparator;
43: }
44:
45: public int compare(Object obj1, Object obj2) {
46: //We assume only customer objects are compared
47: Customer customer1 = (Customer) obj1;
48: Customer customer2 = (Customer) obj2;
49: int result = 0;
50: switch (sortField) {
51:
52: case SORT_FIELD_CUST_NUMBER:
53: result = compareCustomerNumbers(customer1, customer2);
54: break;
55: case SORT_FIELD_CUST_NAME:
56: result = compareCustomerNames(customer1, customer2);
57: break;
58: case SORT_FIELD_CUST_CITY:
59: result = compareCustomerCity(customer1, customer2);
60: break;
61: default:
62: break;
63: }
64: return result;
65: }
66:
67: private int compareCustomerNumbers(Customer customer1,
68: Customer customer2) {
69:
70: String custNumber1 = customer1.getCustomerNumber()
71: .toLowerCase();
72: String custNumber2 = customer2.getCustomerNumber()
73: .toLowerCase();
74: int compareResult = custNumber1.toLowerCase().compareTo(
75: custNumber2.toLowerCase());
76: return compareResult * sortOrder;
77: }
78:
79: private int compareCustomerNames(Customer customer1,
80: Customer customer2) {
81:
82: String custName1 = customer1.getCustomerName().toLowerCase();
83: String custName2 = customer2.getCustomerName().toLowerCase();
84: int compareResult = custName1.toLowerCase().compareTo(
85: custName2.toLowerCase());
86: return compareResult * sortOrder;
87: }
88:
89: private int compareCustomerCity(Customer customer1,
90: Customer customer2) {
91:
92: String custCity1 = customer1.getCustomerCity().toLowerCase();
93: String custCity2 = customer2.getCustomerCity().toLowerCase();
94: int compareResult = custCity1.toLowerCase().compareTo(
95: custCity2.toLowerCase());
96: return compareResult * sortOrder;
97: }
98:
99: }
|