001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.ldap.schema.comparators;
021
022
023 import java.text.ParseException;
024 import java.util.Comparator;
025
026 import org.apache.directory.shared.ldap.schema.parsers.AbstractSchemaDescription;
027 import org.apache.directory.shared.ldap.schema.parsers.ComparatorDescriptionSchemaParser;
028
029
030 /**
031 * A comparator for Comparators. We compare the OIDs
032 *
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 * @version $Rev$
035 */
036 public class ComparatorComparator implements Comparator<String>
037 {
038 /** A static instance of this comparator */
039 public static final Comparator<String> INSTANCE = new ComparatorComparator();
040
041 /** A parser for the comparators */
042 private static final ComparatorDescriptionSchemaParser comparatorParser =
043 new ComparatorDescriptionSchemaParser();
044
045 /**
046 * {@inheritDoc}
047 */
048 public int compare( String s1, String s2 )
049 {
050 // -------------------------------------------------------------------
051 // Handle some basis cases
052 // -------------------------------------------------------------------
053 if ( s1 == null )
054 {
055 return ( s2 == null ) ? 0 : -1;
056 }
057
058 if ( s2 == null )
059 {
060 return -1;
061 }
062
063 // Let's try to avoid a parse.
064 if ( s1.equals( s2 ) )
065 {
066 return 0;
067 }
068
069 // Parse the comparators now
070 synchronized ( comparatorParser )
071 {
072 AbstractSchemaDescription c1 = null;
073 AbstractSchemaDescription c2 = null;
074
075 try
076 {
077 c1 = comparatorParser.parse( s1 );
078 }
079 catch ( ParseException pe )
080 {
081 return -1;
082 }
083
084 try
085 {
086 c2 = comparatorParser.parse( s2 );
087 }
088 catch ( ParseException pe )
089 {
090 return -1;
091 }
092
093 // The OID is the only criterium for a comparison
094 return ( c1.getNumericOid().compareTo( c2.getNumericOid() ) );
095 }
096 }
097 }