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