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 org.apache.directory.shared.ldap.model.schema.LdapComparator; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026 027 028/** 029 * A class for the bitStringMatch matchingRule (RFC 4517, par. 4.2.1) 030 * 031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 032 */ 033public class BitStringComparator extends LdapComparator<String> 034{ 035 /** The serial version UID */ 036 private static final long serialVersionUID = 2L; 037 038 /** A logger for this class */ 039 private static final Logger LOG = LoggerFactory.getLogger( BitStringComparator.class ); 040 041 /** 042 * The BitStringComparator constructor. Its OID is the IntegerOrderingMatch matching 043 * rule OID. 044 */ 045 public BitStringComparator( String oid ) 046 { 047 super( oid ); 048 } 049 050 051 /** 052 * Implementation of the Compare method 053 */ 054 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "ES_COMPARING_PARAMETER_STRING_WITH_EQ", 055 justification ="false positive") 056 public int compare( String bs1, String bs2 ) 057 { 058 LOG.debug( "comparing BitString objects '{}' with '{}'", bs1, bs2 ); 059 060 // First, shortcut the process by comparing 061 // references. If they are equals, then bs1 and bs2 062 // reference the same object 063 if ( bs1 == bs2 ) 064 { 065 return 0; 066 } 067 068 // Then, deal with one of bs1 or bs2 being null 069 // Both can't be null, because then they would 070 // have been caught by the previous test 071 if ( ( bs1 == null ) || ( bs2 == null ) ) 072 { 073 return ( bs1 == null ? -1 : 1 ); 074 } 075 076 // We have to get rid of 0 from left of each BitString 077 char[] array1 = bs1.toCharArray(); 078 char[] array2 = bs2.toCharArray(); 079 080 int pos1 = bs1.indexOf( '1' ); 081 int pos2 = bs2.indexOf( '1' ); 082 083 if ( pos1 == -1 ) 084 { 085 if ( pos2 == -1 ) 086 { 087 return 0; 088 } 089 else 090 { 091 return -1; 092 } 093 } 094 else if ( pos2 == -1 ) 095 { 096 return 1; 097 } 098 099 int length1 = array1.length - pos1; 100 int length2 = array2.length - pos2; 101 102 if ( length1 == length2 ) 103 { 104 for ( int i = 0; i < length1; i++ ) 105 { 106 int i1 = i + pos1; 107 int i2 = i + pos2; 108 109 if ( array1[i1] < array2[i2] ) 110 { 111 return -1; 112 } 113 else if ( array1[i1] > array2[i2] ) 114 { 115 return 1; 116 } 117 } 118 119 return 0; 120 } 121 122 if ( length1 < length2 ) 123 { 124 return -1; 125 } 126 else 127 { 128 return 1; 129 } 130 } 131}