001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata groupware may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: MenuItemComparator.java,v $
031: * Revision 1.4 2005/10/03 10:21:15 colinmacleod
032: * Fixed some style and javadoc issues.
033: *
034: * Revision 1.3 2005/10/02 14:08:58 colinmacleod
035: * Added/improved log4j logging.
036: *
037: * Revision 1.2 2005/04/09 17:19:58 colinmacleod
038: * Changed copyright text to GPL v2 explicitly.
039: *
040: * Revision 1.1.1.1 2005/03/10 17:51:32 colinmacleod
041: * Restructured ivata op around Hibernate/PicoContainer.
042: * Renamed ivata groupware.
043: *
044: * -----------------------------------------------------------------------------
045: */
046: package com.ivata.groupware.navigation.menu.item;
047:
048: import org.apache.log4j.Logger;
049:
050: import java.util.Comparator;
051:
052: /**
053: * Sorts menu items by their <code>priority</code> properties.
054: *
055: * @since ivata groupware 0.10 (Feb 14, 2005)
056: * @author Colin MacLeod
057: * <a href="mailto:colin.macleod@ivata.com">colin.macleod@ivata.com</a>
058: * @version $Revision: 1.4 $
059: */
060:
061: public class MenuItemComparator implements Comparator {
062: /**
063: * Logger for this class.
064: */
065: private static final Logger logger = Logger
066: .getLogger(MenuItemComparator.class);
067:
068: /**
069: * Sorts menu items by their <code>priority</code> properties.
070: * <copyDoc>Refer to {@link Comparator}.</copyDoc>
071: *
072: * @param item0Object first menu item to compare.
073: * @param item1Object first menu item to compare.
074: * @return <copyDoc>Refer to {@link Comparator}.</copyDoc>
075: * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
076: */
077: public int compare(Object item0Object, Object item1Object) {
078: if (logger.isDebugEnabled()) {
079: logger.debug("compare(Object item0Object = " + item0Object
080: + ", Object item1Object = " + item1Object
081: + ") - start");
082: }
083:
084: MenuItemDO menuItem0 = (MenuItemDO) item0Object;
085: MenuItemDO menuItem1 = (MenuItemDO) item1Object;
086:
087: // first rule out whichever is null (if one is or both are)
088: Integer compareNull = compareNull(menuItem0, menuItem1);
089: if (compareNull != null) {
090: int returnint = compareNull.intValue();
091: if (logger.isDebugEnabled()) {
092: logger
093: .debug("compare(Object, Object) - end - return value = "
094: + returnint);
095: }
096: return returnint;
097: }
098:
099: // try to use priority - if they are the same, compare id values
100: compareNull = compareNull(menuItem0.getPriority(), menuItem1
101: .getPriority());
102: if (compareNull != null) {
103: int returnint = compareNull.intValue();
104: if (logger.isDebugEnabled()) {
105: logger
106: .debug("compare(Object, Object) - end - return value = "
107: + returnint);
108: }
109: return returnint;
110: }
111: int returnValue = menuItem0.getPriority().compareTo(
112: menuItem1.getPriority());
113: if (returnValue == 0) {
114: compareNull = compareNull(menuItem0.getId(), menuItem1
115: .getId());
116: if (compareNull != null) {
117: int returnint = compareNull.intValue();
118: if (logger.isDebugEnabled()) {
119: logger
120: .debug("compare(Object, Object) - end - return value = "
121: + returnint);
122: }
123: return returnint;
124: }
125: int returnint = menuItem0.getId().compareTo(
126: menuItem1.getId());
127: if (logger.isDebugEnabled()) {
128: logger
129: .debug("compare(Object, Object) - end - return value = "
130: + returnint);
131: }
132: return returnint;
133: }
134:
135: if (logger.isDebugEnabled()) {
136: logger
137: .debug("compare(Object, Object) - end - return value = "
138: + returnValue);
139: }
140: return returnValue;
141: }
142:
143: /**
144: * Private helper. Compare two objects to see if either is null and return
145: * as appropriate, otherwise return <code>null</code>.
146: *
147: * @param object0 first object to compare.
148: * @param object1 second object to compare.
149: * @return <code>0</code> if both are <code>null</code>, <code>1</code> if
150: * only <code>object0</code> is <code>null</code>, <code>-1</code> if only
151: * <code>object1</code> is <code>null</code>, otherwise <code>null</code>.
152: */
153: private Integer compareNull(final Object object0,
154: final Object object1) {
155: if (logger.isDebugEnabled()) {
156: logger.debug("compareNull(Object object0 = " + object0
157: + ", Object object1 = " + object1 + ") - start");
158: }
159:
160: if (object0 == null) {
161: if ((object1 == null)) {
162: Integer returnInteger = new Integer(0);
163: if (logger.isDebugEnabled()) {
164: logger
165: .debug("compareNull(Object, Object) - end - return value = "
166: + returnInteger);
167: }
168: return returnInteger;
169: }
170: Integer returnInteger = new Integer(1);
171: if (logger.isDebugEnabled()) {
172: logger
173: .debug("compareNull(Object, Object) - end - return value = "
174: + returnInteger);
175: }
176: return returnInteger;
177: } else if (object1 == null) {
178: if ((object0 == null)) {
179: Integer returnInteger = new Integer(0);
180: if (logger.isDebugEnabled()) {
181: logger
182: .debug("compareNull(Object, Object) - end - return value = "
183: + returnInteger);
184: }
185: return returnInteger;
186: }
187: Integer returnInteger = new Integer(-1);
188: if (logger.isDebugEnabled()) {
189: logger
190: .debug("compareNull(Object, Object) - end - return value = "
191: + returnInteger);
192: }
193: return returnInteger;
194: }
195:
196: if (logger.isDebugEnabled()) {
197: logger
198: .debug("compareNull(Object, Object) - end - return value = "
199: + null);
200: }
201: return null;
202: }
203: }
|