KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > notification > queue > QueueUtil


1 package org.jacorb.notification.queue;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2004 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * 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 JavaDoc;
25
26 import org.jacorb.notification.interfaces.Message;
27
28 /**
29  * @author Alphonse Bendt
30  * @version $Id: QueueUtil.java,v 1.4 2005/02/14 00:11:10 alphonse.bendt Exp $
31  */

32
33 class QueueUtil
34 {
35     private QueueUtil()
36     {
37         // not intended to be invoked
38
}
39
40     ////////////////////////////////////////
41

42     static final Message[] MESSAGE_ARRAY_TEMPLATE = new Message[0];
43
44     static final HeapEntry[] HEAP_ENTRY_ARRAY_TEMPLATE = new HeapEntry[0];
45
46     static Comparator JavaDoc ASCENDING_TIMEOUT_COMPARATOR = new Comparator JavaDoc()
47     {
48         public int compare(Object JavaDoc left, Object JavaDoc right)
49         {
50             Message _left = toMessage(left);
51             Message _right = toMessage(right);
52
53             if (_left.hasTimeout())
54             {
55                 if (!_right.hasTimeout())
56                 {
57                     return -1;
58                 }
59
60                 return (int) (_left.getTimeout() - _right.getTimeout());
61             }
62             else if (_right.hasTimeout())
63             {
64                 return 1;
65             }
66
67             return 0;
68         }
69     };
70
71     static Comparator JavaDoc ASCENDING_AGE_COMPARATOR = new Comparator JavaDoc()
72     {
73         public int compare(Object JavaDoc left, Object JavaDoc right)
74         {
75             HeapEntry _left = (HeapEntry) left;
76             HeapEntry _right = (HeapEntry) right;
77
78             return (int)(_left.order_ - _right.order_);
79         }
80     };
81
82     static Comparator JavaDoc DESCENDING_AGE_COMPARATOR = new Comparator JavaDoc()
83     {
84         public int compare(Object JavaDoc left, Object JavaDoc right)
85         {
86             return -ASCENDING_AGE_COMPARATOR.compare(left, right);
87         }
88     };
89
90     static Comparator JavaDoc ASCENDING_PRIORITY_COMPARATOR = new Comparator JavaDoc()
91     {
92         public int compare(Object JavaDoc left, Object JavaDoc right)
93         {
94             Message _right = toMessage(right);
95
96             Message _left = toMessage(left);
97             
98             return _left.getPriority() - _right.getPriority();
99         }
100     };
101     
102     static Comparator JavaDoc DESCENDING_PRIORITY_COMPARATOR = new Comparator JavaDoc()
103     {
104         public int compare(Object JavaDoc left, Object JavaDoc right)
105         {
106             return -ASCENDING_PRIORITY_COMPARATOR.compare(left, right);
107         }
108     };
109     
110     static Message toMessage(Object JavaDoc object)
111     {
112         final Message _message;
113         
114         if (object instanceof HeapEntry)
115         {
116             _message = ((HeapEntry) object).event_;
117         }
118         else if (object instanceof Message)
119         {
120             _message = (Message) object;
121         }
122         else
123         {
124             throw new IllegalArgumentException JavaDoc();
125         }
126         return _message;
127     }
128 }
Popular Tags