Home » geronimo-2.2-source-release » org.apache.geronimo.console.web.util » [javadoc | source]

    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;
   21   import java.util.StringTokenizer;
   22   import javax.management.ObjectInstance;
   23   import javax.management.ObjectName;
   24   
   25   /**
   26    * This class sort ObjectNames 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    * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
   31    */
   32   public class ObjectInstanceComparator implements Comparator {
   33       private static final int LEFT_GREATER = 1;
   34       private static final int RIGHT_GREATER = -1;
   35       private static final int EQUAL = 0;
   36   
   37       public int compare(Object o1, Object o2) {
   38           ObjectName left = ((ObjectInstance) o1).getObjectName();
   39           ObjectName right = ((ObjectInstance) o2).getObjectName();
   40           String leftName = left.getCanonicalName();
   41           String rightName = right.getCanonicalName();
   42   
   43           StringTokenizer leftDomainTokenizer =
   44                   new StringTokenizer(leftName, ".");
   45   
   46           StringTokenizer rightDomainTokenizer =
   47                   new StringTokenizer(rightName, ".");
   48   
   49           while (leftDomainTokenizer.hasMoreTokens()) {
   50               if (!rightDomainTokenizer.hasMoreTokens()) {
   51                   return RIGHT_GREATER;
   52               }
   53               String leftToken = leftDomainTokenizer.nextToken();
   54               String 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   }

Home » geronimo-2.2-source-release » org.apache.geronimo.console.web.util » [javadoc | source]