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 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 transient 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       * Compare two String DNs
57       *  
58       * @param dn1 The first DN
59       * @param dn2 The second DN
60       * 
61       * @return -1 i the first DN is inferior to the second DN, 1 if the second DN is superior, 0 of they are equal
62       */
63      public int compare( String dn1, String dn2 )
64      {
65          if ( dn1 == null )
66          {
67              if ( dn2 == null )
68              {
69                  return 0;
70              }
71              else
72              {
73                  return -1;
74              }
75          }
76          
77          return dn1.compareTo( dn2 );
78      }
79  
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public int compare( Object obj0, Object obj1 )
86      {
87          if ( ( obj0 instanceof String ) && ( obj1 instanceof String ) )
88          {
89              return compare( ( String ) obj0, ( String ) obj1 );
90          }
91          
92          Dn dn0 = null;
93          Dn dn1 = null;
94  
95          try
96          {
97              dn0 = getDn( obj0 );
98              dn1 = getDn( obj1 );
99          }
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 }