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