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   *    https://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 ParsedDnComparator 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 transient SchemaManager schemaManager;
43  
44      /**
45       * The ParsedDnComparator constructor.
46       * 
47       * @param oid The Comparator's OID
48       */
49      public ParsedDnComparator( 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          for ( int i = dn0Size - 1; i >= 0; i-- )
94          {
95              int comp = dn0.getRdn( i ).compareTo( dn1.getRdn( i ) );
96              
97              if ( comp != 0 )
98              {
99                  return comp;
100             }
101         }
102         
103         return 0;
104     }
105 
106 
107     private Dn getDn( Object obj ) throws LdapInvalidDnException
108     {
109         Dn dn;
110 
111         if ( obj instanceof Dn )
112         {
113             dn = ( Dn ) obj;
114 
115             dn = dn.isSchemaAware() ? dn : new Dn( schemaManager, dn );
116         }
117         else if ( obj instanceof String )
118         {
119             dn = new Dn( schemaManager, ( String ) obj );
120         }
121         else
122         {
123             throw new IllegalStateException( I18n.err( I18n.ERR_13720_CANNOT_HANDLE_DN_COMPARISONS, obj == null ? null : obj.getClass() ) );
124         }
125 
126         return dn;
127     }
128 
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public void setSchemaManager( SchemaManager schemaManager )
135     {
136         this.schemaManager = schemaManager;
137     }
138 }