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      /**
45       * Creates a new instance of DnComparator.
46       *
47       * @param oid The Comparator's OID
48       */
49      public DnComparator( String oid )
50      {
51          super( oid );
52      }
53  
54  
55      /**
56       * {@inheritDoc}
57       */
58      @Override
59      public int compare( Object obj0, Object obj1 )
60      {
61          Dn dn0 = null;
62          Dn dn1 = null;
63  
64          try
65          {
66              dn0 = getDn( obj0 );
67              dn1 = getDn( obj1 );
68          }
69          catch ( LdapException e )
70          {
71              // -- what do we do here ?
72              return -1;
73          }
74  
75          int dn0Size = dn0.getRdns().size();
76          int dn1Size = dn1.getRdns().size();
77          
78          // check the equality first, cause
79          // when both DNs are equal checking isAncestorOf() returns true
80          if ( dn0.equals( dn1 ) )
81          {
82              return 0;
83          }
84          else if ( dn0Size > dn1Size )
85          {
86              return -1;
87          }
88          else if ( dn1Size > dn0Size )
89          {
90              return 1;
91          }
92  
93          return dn0.getNormName().compareTo( dn1.getNormName() );
94      }
95  
96  
97      private Dn getDn( Object obj ) throws LdapInvalidDnException
98      {
99          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 }