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 java.util.UUID;
24  
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  
29  /**
30   * A comparator for UUID. We simply use the UUID compareTo method.
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public class UuidComparator extends SerializableComparator<String>
35  {
36      /** The serial version UID */
37      private static final long serialVersionUID = 2L;
38  
39      /** A logger for this class */
40      private static final Logger LOG = LoggerFactory.getLogger( UuidComparator.class );
41      private static final boolean IS_DEBUG = LOG.isDebugEnabled();
42  
43      /** A static instance of the UuidComparator */
44      public static final UuidComparator INSTANCE = new UuidComparator( "1.3.6.1.1.16.4" );
45  
46  
47      /**
48       * The UUIDComparator constructor. Its OID is the UUIDMatch matching
49       * rule OID.
50       * 
51       * @param oid The Comparator's OID
52       */
53      public UuidComparator( String oid )
54      {
55          super( oid );
56      }
57  
58  
59      /**
60       * {@inheritDoc}
61       */
62      @Override
63      public int compare( String uuid1, String uuid2 )
64      {
65          if ( IS_DEBUG )
66          {
67              LOG.debug( "comparing UUID objects '{}' with '{}'", uuid1, uuid2 );
68          }
69  
70          // -------------------------------------------------------------------
71          // Handle some basis cases
72          // -------------------------------------------------------------------
73          if ( uuid1 == null )
74          {
75              return ( uuid2 == null ) ? 0 : -1;
76          }
77  
78          if ( uuid2 == null )
79          {
80              return 1;
81          }
82  
83          return uuid1.compareTo( uuid2 );
84      }
85  
86  
87      /**
88       * Compare two UUID.
89       * 
90       * @param uuid1 The first UUID
91       * @param uuid2 he second UUID
92       * @return -1 if the first UUID is lower than the second UUID, 1 if it's higher, 0
93       * if they are equal  
94       */
95      public int compare( UUID uuid1, UUID uuid2 )
96      {
97          if ( IS_DEBUG )
98          {
99              LOG.debug( "comparing UUID objects '{}' with '{}'", uuid1, uuid2 );
100         }
101 
102         // -------------------------------------------------------------------
103         // Handle some basis cases
104         // -------------------------------------------------------------------
105         if ( uuid1 == null )
106         {
107             return ( uuid2 == null ) ? 0 : -1;
108         }
109 
110         if ( uuid2 == null )
111         {
112             return 1;
113         }
114 
115         return uuid1.compareTo( uuid2 );
116     }
117 }