KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > navigation > menu > item > MenuItemComparator


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: MenuItemComparator.java,v $
31  * Revision 1.2 2005/04/09 17:19:58 colinmacleod
32  * Changed copyright text to GPL v2 explicitly.
33  *
34  * Revision 1.1.1.1 2005/03/10 17:51:32 colinmacleod
35  * Restructured ivata op around Hibernate/PicoContainer.
36  * Renamed ivata groupware.
37  *
38  * -----------------------------------------------------------------------------
39  */

40 package com.ivata.groupware.navigation.menu.item;
41
42 import java.util.Comparator JavaDoc;
43
44 /**
45  * Sorts menu items by their <code>priority</code> properties.
46  *
47  * @since ivata groupware 0.10 (Feb 14, 2005)
48  * @author Colin MacLeod
49  * <a HREF="mailto:colin.macleod@ivata.com">colin.macleod@ivata.com</a>
50  * @version $Revision: 1.2 $
51  */

52
53 public class MenuItemComparator implements Comparator JavaDoc {
54     /**
55      * Sorts menu items by their <code>priority</code> properties.
56      * Refer to {@link Comparator}.
57      *
58      * @param item0Object first menu item to compare.
59      * @param item1Object first menu item to compare.
60      * @return Refer to {@link Comparator}.
61      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
62      */

63     public int compare(Object JavaDoc item0Object, Object JavaDoc item1Object) {
64         MenuItemDO menuItem0 = (MenuItemDO)item0Object;
65         MenuItemDO menuItem1 = (MenuItemDO)item1Object;
66
67         // first rule out whichever is null (if one is or both are)
68
Integer JavaDoc compareNull = compareNull(menuItem0, menuItem1);
69         if (compareNull != null) {
70             return compareNull.intValue();
71         }
72
73         // try to use priority - if they are the same, compare id values
74
compareNull = compareNull(menuItem0.getPriority(),
75                 menuItem1.getPriority());
76         if (compareNull != null) {
77             return compareNull.intValue();
78         }
79         int returnValue = menuItem0.getPriority().compareTo(
80                 menuItem1.getPriority());
81         if (returnValue == 0) {
82             compareNull = compareNull(menuItem0.getId(),
83                     menuItem1.getId());
84             if (compareNull != null) {
85                 return compareNull.intValue();
86             }
87             return menuItem0.getId().compareTo(menuItem1.getId());
88         }
89         return returnValue;
90     }
91     /**
92      * Private helper. Compare two objects to see if either is null and return
93      * as appropriate, otherwise return <code>null</code>.
94      *
95      * @param object0 first object to compare.
96      * @param object1 second object to compare.
97      * @return <code>0</code> if both are <code>null</code>, <code>1</code> if
98      * only <code>object0</code> is <code>null</code>, <code>-1</code> if only
99      * <code>object1</code> is <code>null</code>, otherwise <code>null</code>.
100      */

101     private Integer JavaDoc compareNull(final Object JavaDoc object0, final Object JavaDoc object1) {
102         if (object0 == null) {
103             if ((object1 == null)) {
104                 return new Integer JavaDoc(0);
105             }
106             return new Integer JavaDoc(1);
107         } else if (object1 == null) {
108             if ((object0 == null)) {
109                 return new Integer JavaDoc(0);
110             }
111             return new Integer JavaDoc(-1);
112         }
113         return null;
114     }
115 }
116
Popular Tags