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.NormalizerDescriptionSchemaParser;
028    
029    
030    /**
031     * A comparator for Normalizers. We compare the OIDs.
032     * 
033     * 
034     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035     * @version $Rev$
036     */
037    public class NormalizerComparator implements Comparator<String>
038    {
039        /** A static instance of this comparator */
040        public static final Comparator<String> INSTANCE = new NormalizerComparator();
041    
042        /** A parser for the Normalizers */
043        private static final NormalizerDescriptionSchemaParser normalizerParser =
044            new NormalizerDescriptionSchemaParser();
045        
046        /**
047         * {@inheritDoc}
048         */
049        public int compare( String s1, String s2 )
050        {
051            // -------------------------------------------------------------------
052            // Handle some basis cases
053            // -------------------------------------------------------------------
054            if ( s1 == null )
055            {
056                return ( s2 == null ) ? 0 : -1;
057            }
058            
059            if ( s2 == null )
060            {
061                return -1;
062            }
063    
064            // Let's try to avoid a parse.
065            if ( s1.equals( s2 ) )
066            {
067                return 0;
068            }
069            
070            // Parse the comparators now
071            synchronized ( normalizerParser )
072            {
073                AbstractSchemaDescription n1 = null;
074                AbstractSchemaDescription n2 = null;
075                
076                try
077                {
078                    n1 = normalizerParser.parse( s1 );
079                }
080                catch ( ParseException pe )
081                {
082                    return -1;
083                }
084                
085                try
086                {
087                    n2 = normalizerParser.parse( s2 );
088                }
089                catch ( ParseException pe )
090                {
091                    return -1;
092                }
093                
094                // The OID is the only criterium for a comparison
095                return ( n1.getNumericOid().compareTo( n2.getNumericOid() ) );
096            }
097        }
098    }