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 org.apache.directory.shared.ldap.schema.LdapComparator;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    
027    
028    /**
029     * A comparator for CSN SID.
030     *
031     * The SID is supposed to be an hexadecimal number between 0x0 and 0xfff
032     * 
033     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034     * @version $Rev$
035     */
036    public class CsnSidComparator extends LdapComparator<String>
037    {
038        /** A logger for this class */
039        private static final Logger LOG = LoggerFactory.getLogger( CsnSidComparator.class );
040    
041        /** The serialVersionUID */
042        private static final long serialVersionUID = 1L;
043    
044        /**
045         * The CsnSidComparator constructor. Its OID is the CsnSidMatch matching
046         * rule OID.
047         */
048        public CsnSidComparator( String oid )
049        {
050            super( oid );
051        }
052        
053        
054        /**
055         * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
056         */
057        public int compare( String sidStr1, String sidStr2 )
058        {
059            LOG.debug( "comparing CSN SID objects '{}' with '{}'", sidStr1, sidStr2 );
060    
061            // -------------------------------------------------------------------
062            // Handle some basis cases
063            // -------------------------------------------------------------------
064            if ( sidStr1 == null )
065            {
066                return ( sidStr2 == null ) ? 0 : -1;
067            }
068            
069            if ( sidStr2 == null )
070            {
071                return 1;
072            }
073            
074            int sid1 = 0;
075            int sid2 = 0;
076            
077            try
078            {
079                sid1 = Integer.parseInt( sidStr1, 16 );
080            }
081            catch ( NumberFormatException nfe )
082            {
083                return -1;
084            }
085            
086            try
087            {
088                sid2 = Integer.parseInt( sidStr2, 16 );
089            }
090            catch ( NumberFormatException nfe )
091            {
092                return 1;
093            }
094            
095            if ( sid1 > sid2 )
096            {
097                return 1;
098            }
099            else if ( sid2 > sid1 )
100            {
101                return -1;
102            }
103            
104            return 0;
105        }
106    }