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 */
020package org.apache.directory.shared.ldap.model.schema.comparators;
021
022
023import java.io.IOException;
024
025import org.apache.directory.shared.i18n.I18n;
026import org.apache.directory.shared.ldap.model.schema.LdapComparator;
027import org.apache.directory.shared.ldap.model.schema.PrepareString;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031
032/**
033 * A class for the numericStringOrderingMatch matchingRule (RFC 4517, par. 4.2.23)
034 * 
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public class NumericStringComparator extends LdapComparator<String>
038{
039    /** The serial version UID */
040    private static final long serialVersionUID = 2L;
041
042    /** A logger for this class */
043    private static final Logger LOG = LoggerFactory.getLogger( NumericStringComparator.class );
044
045    /**
046     * The IntegerComparator constructor. Its OID is the numericStringOrderingMatch matching
047     * rule OID.
048     */
049    public NumericStringComparator( String oid )
050    {
051        super( oid );
052    }
053
054
055    /**
056     * Implementation of the Compare method
057     */
058    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ",
059                justification ="false positive")
060    public int compare( String backendValue, String assertValue )
061    {
062        LOG.debug( "comparing numericStringOrdering objects '{}' with '{}'", backendValue, assertValue );
063
064        // First, shortcut the process by comparing
065        // references. If they are equals, then o1 and o2
066        // reference the same object
067        if ( backendValue == assertValue )
068        {
069            return 0;
070        }
071
072        // Then, deal with one of o1 or o2 being null
073        // Both can't be null, because then they would 
074        // have been caught by the previous test
075        if ( ( backendValue == null ) || ( assertValue == null ) )
076        {
077            return ( backendValue == null ? -1 : 1 );
078        }
079
080        // Both objects must be stored as String for numeric.
081        // But we need to normalize the values first.
082        try
083        {
084            backendValue = PrepareString.normalize( backendValue, PrepareString.StringType.NUMERIC_STRING );
085        }
086        catch ( IOException e )
087        {
088            throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue ) );
089        }
090        try
091        {
092            assertValue = PrepareString.normalize( assertValue, PrepareString.StringType.NUMERIC_STRING );
093        }
094        catch ( IOException e )
095        {
096            throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ) );
097        }
098
099        return backendValue.compareTo( assertValue );
100    }
101}