KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > jmxdebug > util > ObjectNameComparator


1 /**
2  *
3  * Copyright 2004 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.jmxdebug.util;
19
20 import java.util.Comparator JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23
24 /**
25  * Taken from orignial console-web module
26  *
27  * This class sort ObjectNames by canonical name. Unfortunately, it
28  * will not place single token domains before multiple token domains of
29  * the same type (foo.bar > foo at the moment).
30  *
31  * @version $Rev: 45928 $ $Date: 2004-09-11 17:03:39 -0700 (Sat, 11 Sep 2004) $
32  */

33 public class ObjectNameComparator implements Comparator JavaDoc {
34     public static final ObjectNameComparator INSTANCE = new ObjectNameComparator();
35
36     private static final int LEFT_GREATER = 1;
37     private static final int RIGHT_GREATER = -1;
38     private static final int EQUAL = 0;
39
40     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
41         String JavaDoc leftName = ((ObjectName JavaDoc) o1).getCanonicalName();
42         String JavaDoc rightName = ((ObjectName JavaDoc) o2).getCanonicalName();
43
44         StringTokenizer JavaDoc leftDomainTokenizer = new StringTokenizer JavaDoc(leftName, ".");
45
46         StringTokenizer JavaDoc rightDomainTokenizer = new StringTokenizer JavaDoc(rightName, ".");
47
48         while (leftDomainTokenizer.hasMoreTokens()) {
49             if (!rightDomainTokenizer.hasMoreTokens()) {
50                 return RIGHT_GREATER;
51             }
52             String JavaDoc leftToken = leftDomainTokenizer.nextToken();
53             String JavaDoc rightToken = rightDomainTokenizer.nextToken();
54             int comparison = leftToken.compareToIgnoreCase(rightToken);
55             if (comparison != 0) {
56                 return comparison;
57             }
58         }
59
60         // left has no more tokens
61
if (rightDomainTokenizer.hasMoreTokens()) {
62             return LEFT_GREATER;
63         }
64
65         // both ran out of tokens so they are equal
66
return EQUAL;
67     }
68 }
69
Popular Tags