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.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 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 * {@inheritDoc} 057 */ 058 @Override 059 public int compare( Object obj0, Object obj1 ) 060 { 061 Dn dn0 = null; 062 Dn dn1 = null; 063 064 try 065 { 066 dn0 = getDn( obj0 ); 067 dn1 = getDn( obj1 ); 068 } 069 catch ( LdapException e ) 070 { 071 // -- what do we do here ? 072 return -1; 073 } 074 075 int dn0Size = dn0.getRdns().size(); 076 int dn1Size = dn1.getRdns().size(); 077 078 // check the equality first, cause 079 // when both DNs are equal checking isAncestorOf() returns true 080 if ( dn0.equals( dn1 ) ) 081 { 082 return 0; 083 } 084 else if ( dn0Size > dn1Size ) 085 { 086 return -1; 087 } 088 else if ( dn1Size > dn0Size ) 089 { 090 return 1; 091 } 092 093 return dn0.getNormName().compareTo( dn1.getNormName() ); 094 } 095 096 097 private Dn getDn( Object obj ) throws LdapInvalidDnException 098 { 099 Dn dn; 100 101 if ( obj instanceof Dn ) 102 { 103 dn = ( Dn ) obj; 104 105 dn = dn.isSchemaAware() ? dn : dn.apply( schemaManager ); 106 } 107 else if ( obj instanceof String ) 108 { 109 dn = new Dn( schemaManager, ( String ) obj ); 110 } 111 else 112 { 113 throw new IllegalStateException( I18n.err( I18n.ERR_04218, obj == null ? null : obj.getClass() ) ); 114 } 115 116 return dn; 117 } 118 119 120 /** 121 * {@inheritDoc} 122 */ 123 @Override 124 public void setSchemaManager( SchemaManager schemaManager ) 125 { 126 this.schemaManager = schemaManager; 127 } 128}