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.api.ldap.model.schema.comparators; 021 022 023import org.apache.directory.api.ldap.model.schema.LdapComparator; 024import org.apache.directory.api.util.Strings; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028 029/** 030 * A comparator for byte[]s. 031 * 032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 033 */ 034public class ByteArrayComparator extends LdapComparator<byte[]> 035{ 036 /** The serial version UID */ 037 private static final long serialVersionUID = 2L; 038 039 /** A logger for this class */ 040 private static final Logger LOG = LoggerFactory.getLogger( ByteArrayComparator.class ); 041 042 043 /** 044 * The ByteArrayComparator constructor. Its OID is the OctetStringMatch matching 045 * rule OID. 046 * 047 * @param oid The Comparator's OID 048 */ 049 public ByteArrayComparator( String oid ) 050 { 051 super( oid ); 052 } 053 054 055 /** 056 * {@inheritDoc} 057 */ 058 public int compare( byte[] b1, byte[] b2 ) 059 { 060 LOG.debug( "comparing OctetString objects '{}' with '{}'", 061 Strings.dumpBytes( b1 ), Strings.dumpBytes( b2 ) ); 062 063 // ------------------------------------------------------------------- 064 // Handle some basis cases 065 // ------------------------------------------------------------------- 066 067 if ( b1 == null ) 068 { 069 return ( b2 == null ) ? 0 : -1; 070 } 071 072 if ( b2 == null ) 073 { 074 return 1; 075 } 076 077 if ( b1.length == b2.length ) 078 { 079 for ( int i = 0; i < b1.length; i++ ) 080 { 081 if ( b1[i] > b2[i] ) 082 { 083 return 1; 084 } 085 else if ( b1[i] < b2[i] ) 086 { 087 return -1; 088 } 089 } 090 091 return 0; 092 } 093 094 int minLength = Math.min( b1.length, b2.length ); 095 096 for ( int i = 0; i < minLength; i++ ) 097 { 098 if ( b1[i] > b2[i] ) 099 { 100 return 1; 101 } 102 else if ( b1[i] < b2[i] ) 103 { 104 return -1; 105 } 106 } 107 108 // b2 is longer w/ b1 as prefix 109 if ( b1.length == minLength ) 110 { 111 return -1; 112 } 113 114 // b1 is longer w/ b2 as prefix 115 if ( b2.length == minLength ) 116 { 117 return 1; 118 } 119 120 return 0; 121 } 122}