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 * https://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.i18n.I18n; 024import org.apache.directory.api.ldap.model.exception.LdapException; 025import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException; 026import org.apache.directory.api.ldap.model.name.Dn; 027import org.apache.directory.api.ldap.model.schema.LdapComparator; 028import org.apache.directory.api.ldap.model.schema.SchemaManager; 029 030 031/** 032 * Compare two DNs 033 * 034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 035 */ 036public class DnComparator extends LdapComparator<Object> 037{ 038 /** The serial version UID */ 039 private static final long serialVersionUID = 2L; 040 041 /** A reference to the schema manager */ 042 private transient SchemaManager schemaManager; 043 044 /** 045 * Creates a new instance of DnComparator. 046 * 047 * @param oid The Comparator's OID 048 */ 049 public DnComparator( String oid ) 050 { 051 super( oid ); 052 } 053 054 055 /** 056 * Compare two String DNs 057 * 058 * @param dn1 The first DN 059 * @param dn2 The second DN 060 * 061 * @return -1 i the first DN is inferior to the second DN, 1 if the second DN is superior, 0 of they are equal 062 */ 063 public int compare( String dn1, String dn2 ) 064 { 065 if ( dn1 == null ) 066 { 067 if ( dn2 == null ) 068 { 069 return 0; 070 } 071 else 072 { 073 return -1; 074 } 075 } 076 077 return dn1.compareTo( dn2 ); 078 } 079 080 081 /** 082 * {@inheritDoc} 083 */ 084 @Override 085 public int compare( Object obj0, Object obj1 ) 086 { 087 if ( ( obj0 instanceof String ) && ( obj1 instanceof String ) ) 088 { 089 return compare( ( String ) obj0, ( String ) obj1 ); 090 } 091 092 Dn dn0 = null; 093 Dn dn1 = null; 094 095 try 096 { 097 dn0 = getDn( obj0 ); 098 dn1 = getDn( obj1 ); 099 } 100 catch ( LdapException e ) 101 { 102 // -- what do we do here ? 103 return -1; 104 } 105 106 int dn0Size = dn0.getRdns().size(); 107 int dn1Size = dn1.getRdns().size(); 108 109 // check the equality first, cause 110 // when both DNs are equal checking isAncestorOf() returns true 111 if ( dn0.equals( dn1 ) ) 112 { 113 return 0; 114 } 115 else if ( dn0Size > dn1Size ) 116 { 117 return -1; 118 } 119 else if ( dn1Size > dn0Size ) 120 { 121 return 1; 122 } 123 124 for ( int i = dn0Size - 1; i >= 0; i-- ) 125 { 126 int comp = dn0.getRdn( i ).compareTo( dn1.getRdn( i ) ); 127 128 if ( comp != 0 ) 129 { 130 return comp; 131 } 132 } 133 134 return 0; 135 } 136 137 138 private Dn getDn( Object obj ) throws LdapInvalidDnException 139 { 140 Dn dn; 141 142 if ( obj instanceof Dn ) 143 { 144 dn = ( Dn ) obj; 145 146 dn = dn.isSchemaAware() ? dn : new Dn( schemaManager, dn ); 147 } 148 else if ( obj instanceof String ) 149 { 150 dn = new Dn( schemaManager, ( String ) obj ); 151 } 152 else 153 { 154 throw new IllegalStateException( I18n.err( I18n.ERR_13720_CANNOT_HANDLE_DN_COMPARISONS, obj == null ? null : obj.getClass() ) ); 155 } 156 157 return dn; 158 } 159 160 161 /** 162 * {@inheritDoc} 163 */ 164 @Override 165 public void setSchemaManager( SchemaManager schemaManager ) 166 { 167 this.schemaManager = schemaManager; 168 } 169}