View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.schema.comparators;
21  
22  
23  import org.apache.directory.api.i18n.I18n;
24  import org.apache.directory.api.ldap.model.exception.LdapException;
25  import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
26  import org.apache.directory.api.ldap.model.name.Dn;
27  import org.apache.directory.api.ldap.model.schema.LdapComparator;
28  import org.apache.directory.api.ldap.model.schema.SchemaManager;
29  
30  
31  /**
32   * Compare two DNs
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class DnComparator extends LdapComparator<Object>
37  {
38      /** The serial version UID */
39      private static final long serialVersionUID = 2L;
40  
41      /** A reference to the schema manager */
42      private SchemaManager schemaManager;
43  
44      public DnComparator( String oid )
45      {
46          super( oid );
47      }
48  
49  
50      /**
51       * {@inheritDoc}
52       */
53      public int compare( Object obj0, Object obj1 )
54      {
55          Dn dn0 = null;
56          Dn dn1 = null;
57  
58          try
59          {
60              dn0 = getDn( obj0 );
61              dn1 = getDn( obj1 );
62          }
63          catch ( LdapException e )
64          {
65              // -- what do we do here ?
66              return -1;
67          }
68  
69          int dn0Size = dn0.getRdns().size();
70          int dn1Size = dn1.getRdns().size();
71          
72          // check the equality first, cause
73          // when both DNs are equal checking isAncestorOf() returns true
74          if ( dn0.equals( dn1 ) )
75          {
76              return 0;
77          }
78          else if ( dn0Size > dn1Size )
79          {
80              return -1;
81          }
82          else if ( dn1Size > dn0Size )
83          {
84              return 1;
85          }
86  
87          return dn0.getNormName().compareTo( dn1.getNormName() );
88      }
89  
90  
91      private Dn getDn( Object obj ) throws LdapInvalidDnException
92      {
93          Dn dn = null;
94  
95          if ( obj instanceof Dn )
96          {
97              dn = ( Dn ) obj;
98  
99              dn = ( dn.isSchemaAware() ? dn : dn.apply( schemaManager ) );
100         }
101         else if ( obj instanceof String )
102         {
103             dn = new Dn( schemaManager, ( String ) obj );
104         }
105         else
106         {
107             throw new IllegalStateException( I18n.err( I18n.ERR_04218, ( obj == null ? null : obj.getClass() ) ) );
108         }
109 
110         return dn;
111     }
112 
113 
114     /**
115      * {@inheritDoc}
116      */
117     public void setSchemaManager( SchemaManager schemaManager )
118     {
119         this.schemaManager = schemaManager;
120     }
121 }