KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > syncclient > demo > ContactComparator


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.syncclient.demo;
20
21 import java.lang.reflect.Method JavaDoc;
22
23 import java.util.Comparator JavaDoc;
24
25 import sync4j.foundation.pdi.contact.Contact;
26
27 /**
28  * Contact comparator.
29  * <br/>
30  * Compares contacts using displayName
31  * <br/>
32  *
33  * @author Fabio Maggi
34  * @author Stefano Nichele
35  * @version $Id: ContactComparator.java,v 1.5 2005/02/01 10:22:31 fabius Exp $
36  */

37
38 public class ContactComparator implements Comparator JavaDoc {
39
40     // ------------------------------------------------------------ Private data
41

42     // ------------------------------------------------------------- Constructor
43
/**
44      * Creates a new ContactComparator.
45      */

46     public ContactComparator() {
47
48     }
49
50     // --------------------------------------------------------- Public methods
51

52     /**
53      * Compares its two arguments for order.
54      *
55      * @param o1 the first object to be compared.
56      * @param o2 the second object to be compared.
57      * @return a negative integer, zero, or a positive integer as the first
58      * argument is less than, equal to, or greater than the second.
59      */

60     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
61
62         String JavaDoc value1 = null;
63         String JavaDoc value2 = null;
64
65         String JavaDoc firstName = null;
66         String JavaDoc lastName = null;
67
68
69
70         value1 = (String JavaDoc) ((DemoContact) o1).getContact().getName().
71                                         getDisplayName().getPropertyValue();
72
73         value2 = (String JavaDoc) ((DemoContact) o2).getContact().getName().
74                                         getDisplayName().getPropertyValue();
75
76
77         if (value1 == null) {
78
79             value1 = (String JavaDoc) ((DemoContact) o1).getContact().getName().
80                         getLastName().getPropertyValue();
81
82             if (value1 != null && !value1.equals("")) {
83                 value1 += ",";
84             }
85
86             value1 += (String JavaDoc) ((DemoContact) o1).getContact().getName().
87                         getFirstName().getPropertyValue();
88
89             if (value1 == null) {
90                 value1 = "";
91             }
92
93         }
94
95         if (value2 == null) {
96
97             value2 =(String JavaDoc) ((DemoContact) o2).getContact().getName().
98                         getLastName().getPropertyValue();
99
100             if (value2 != null && !value1.equals("")) {
101                 value2 += ",";
102             }
103
104             value2 += (String JavaDoc) ((DemoContact) o2).getContact().getName().
105                         getFirstName().getPropertyValue();
106
107             if (value2 == null) {
108                 value2 = "";
109             }
110
111         }
112
113         return (value1.toUpperCase()).compareTo(value2.toUpperCase());
114
115     }
116
117     // --------------------------------------------------------- Private methods
118

119 }
Popular Tags