KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > console > web > util > MBeanComparator


1 /**
2  *
3  * Copyright 2003-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.console.web.util;
19
20 import java.util.Comparator JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22 import javax.management.ObjectInstance JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24
25 /*
26  * This class is supposed to sort by Canonical Name. Unfortunately, it
27  * will not place single token domains before multiple token domains of
28  * the same type (foo.bar > foo at the moment).
29  */

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