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;
024import java.io.Serializable;
025import java.math.BigInteger;
026
027import org.apache.directory.shared.i18n.I18n;
028import org.apache.directory.shared.ldap.model.schema.LdapComparator;
029import org.apache.directory.shared.ldap.model.schema.PrepareString;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033
034/**
035 * A class for the integerOrderingMatch matchingRule (RFC 4517, par. 4.2.20)
036 * 
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class IntegerComparator extends LdapComparator<Object> implements Serializable
040{
041    /** The serial version UID */
042    private static final long serialVersionUID = 2L;
043
044    /** A logger for this class */
045    private static final Logger LOG = LoggerFactory.getLogger( IntegerComparator.class );
046
047    /**
048     * The IntegerComparator constructor. Its OID is the IntegerOrderingMatch matching
049     * rule OID.
050     */
051    public IntegerComparator( String oid )
052    {
053        super( oid );
054    }
055
056
057    /**
058     * Implementation of the Compare method
059     */
060    public int compare( Object v1, Object v2 )
061    {
062        if ( v1 instanceof String )
063        {
064            return compare( ( String ) v1, ( String ) v2 );
065        }
066        else
067        {
068            return compare( ( Long ) v1, ( Long ) v2 );
069        }
070    }
071
072
073    /**
074     * Implementation of the Compare method
075     */
076    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RC_REF_COMPARISON",
077                justification ="false positive")
078    private int compare( Long backendValue, Long assertValue )
079    {
080        LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue );
081
082        // First, shortcut the process by comparing
083        // references. If they are equals, then o1 and o2
084        // reference the same object
085        if ( backendValue == assertValue )
086        {
087            return 0;
088        }
089
090        // Then, deal with one of o1 or o2 being null
091        // Both can't be null, because then they would 
092        // have been caught by the previous test
093        if ( ( backendValue == null ) || ( assertValue == null ) )
094        {
095            return ( backendValue == null ? -1 : 1 );
096        }
097
098        return backendValue.compareTo( assertValue );
099    }
100
101
102    /**
103     * Implementation of the Compare method
104     */
105    @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ",
106                justification ="false positive")
107    private int compare( String backendValue, String assertValue )
108    {
109        LOG.debug( "comparing Integer objects '{}' with '{}'", backendValue, assertValue );
110
111        // First, shortcut the process by comparing
112        // references. If they are equals, then o1 and o2
113        // reference the same object
114        if ( backendValue == assertValue )
115        {
116            return 0;
117        }
118
119        // Then, deal with one of o1 or o2 being null
120        // Both can't be null, because then they would 
121        // have been caught by the previous test
122        if ( ( backendValue == null ) || ( assertValue == null ) )
123        {
124            return ( backendValue == null ? -1 : 1 );
125        }
126
127        // Both objects must be stored as String for numeric.
128        // But we need to normalize the values first.
129        try
130        {
131            backendValue = PrepareString.normalize( backendValue, PrepareString.StringType.NUMERIC_STRING );
132        }
133        catch ( IOException e )
134        {
135            throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, backendValue ) );
136        }
137        try
138        {
139            assertValue = PrepareString.normalize( assertValue, PrepareString.StringType.NUMERIC_STRING );
140        }
141        catch ( IOException e )
142        {
143            throw new IllegalArgumentException( I18n.err( I18n.ERR_04224, assertValue ) );
144        }
145
146        BigInteger b1 = new BigInteger( ( String ) backendValue );
147        BigInteger b2 = new BigInteger( ( String ) assertValue );
148
149        return b1.compareTo( b2 );
150    }
151}