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.util.Comparator;
024    
025    
026    /**
027     * A comparator for CSN SID.
028     *
029     * The SID is supposed to be an hexadecimal number between 0x0 and 0xfff
030     * 
031     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032     * @version $Rev$
033     */
034    public class CsnSidComparator implements Comparator<String>
035    {
036        /** A static instance of this comparator */
037        public static final Comparator<String> INSTANCE = new CsnSidComparator();
038        
039        
040        /**
041         * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
042         */
043        public int compare( String sidStr1, String sidStr2 )
044        {
045            // -------------------------------------------------------------------
046            // Handle some basis cases
047            // -------------------------------------------------------------------
048    
049            if ( sidStr1 == null )
050            {
051                return ( sidStr2 == null ) ? 0 : -1;
052            }
053            
054            if ( sidStr2 == null )
055            {
056                return 1;
057            }
058            
059            int sid1 = 0;
060            int sid2 = 0;
061            
062            try
063            {
064                sid1 = Integer.parseInt( sidStr1, 16 );
065            }
066            catch ( NumberFormatException nfe )
067            {
068                return -1;
069            }
070            
071            try
072            {
073                sid2 = Integer.parseInt( sidStr2, 16 );
074            }
075            catch ( NumberFormatException nfe )
076            {
077                return 1;
078            }
079            
080            if ( sid1 > sid2 )
081            {
082                return 1;
083            }
084            else if ( sid2 > sid1 )
085            {
086                return -1;
087            }
088            
089            return 0;
090        }
091    }