01: package org.jacorb.notification.queue;
02:
03: /*
04: * JacORB - a free Java ORB
05: *
06: * Copyright (C) 1999-2004 Gerald Brose
07: *
08: * This library is free software; you can redistribute it and/or
09: * modify it under the terms of the GNU Library General Public
10: * License as published by the Free Software Foundation; either
11: * version 2 of the License, or (at your option) any later version.
12: *
13: * This library is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16: * Library General Public License for more details.
17: *
18: * You should have received a copy of the GNU Library General Public
19: * License along with this library; if not, write to the Free
20: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21: *
22: */
23:
24: import java.util.Comparator;
25:
26: import org.jacorb.notification.interfaces.Message;
27:
28: /**
29: * @author Alphonse Bendt
30: * @version $Id: QueueUtil.java,v 1.6 2006/03/04 19:34:59 alphonse.bendt Exp $
31: */
32:
33: public class QueueUtil {
34: private QueueUtil() {
35: // not intended to be invoked
36: }
37:
38: ////////////////////////////////////////
39:
40: static final Message[] MESSAGE_ARRAY_TEMPLATE = new Message[0];
41:
42: public static Comparator ASCENDING_TIMEOUT_COMPARATOR = new Comparator() {
43: public int compare(Object left, Object right) {
44: final Message _left = (Message) left;
45: final Message _right = (Message) right;
46:
47: if (_left.hasTimeout()) {
48: if (!_right.hasTimeout()) {
49: return -1;
50: }
51:
52: return compareLong(_left.getTimeout(), _right
53: .getTimeout());
54: } else if (_right.hasTimeout()) {
55: return 1;
56: }
57:
58: return 0;
59: }
60: };
61:
62: public static Comparator ASCENDING_INSERT_ORDER_COMPARATOR = new Comparator() {
63: public int compare(Object left, Object right) {
64: final Message _left = (Message) left;
65: final Message _right = (Message) right;
66:
67: return compareLong(_left.getReceiveTimestamp(), _right
68: .getReceiveTimestamp());
69: }
70: };
71:
72: public static Comparator DESCENDING_INSERT_ORDER_COMPARATOR = new Comparator() {
73: public int compare(Object left, Object right) {
74: return -ASCENDING_INSERT_ORDER_COMPARATOR.compare(left,
75: right);
76: }
77: };
78:
79: public static Comparator ASCENDING_PRIORITY_COMPARATOR = new Comparator() {
80: public int compare(Object left, Object right) {
81: final Message _right = (Message) right;
82:
83: final Message _left = (Message) left;
84:
85: return _left.getPriority() - _right.getPriority();
86: }
87: };
88:
89: public static Comparator DESCENDING_PRIORITY_COMPARATOR = new Comparator() {
90: public int compare(Object left, Object right) {
91: return -ASCENDING_PRIORITY_COMPARATOR.compare(left, right);
92: }
93: };
94:
95: private static int compareLong(long left, long right) {
96: return left < right ? -1 : (left == right ? 0 : 1);
97: }
98: }
|