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 java.util.UUID;
24  
25  import org.apache.directory.api.i18n.I18n;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  
30  /**
31   * A comparator for UUID. We simply use the UUID compareTo method.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public class UuidComparator extends SerializableComparator<String>
36  {
37      /** The serial version UID */
38      private static final long serialVersionUID = 2L;
39  
40      /** A logger for this class */
41      private static final Logger LOG = LoggerFactory.getLogger( UuidComparator.class );
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 ( LOG.isDebugEnabled() )
66          {
67              LOG.debug( I18n.msg( I18n.MSG_13751_COMPARING_UUID, uuid1, uuid2 ) );
68          }
69  
70          // -------------------------------------------------------------------
71          // Handle some basis cases
72          // -------------------------------------------------------------------
73          if ( uuid1 == null )
74          {
75              return -1;
76          }
77  
78          return uuid1.compareTo( uuid2 );
79      }
80  
81  
82      /**
83       * Compare two UUID.
84       * 
85       * @param uuid1 The first UUID
86       * @param uuid2 he second UUID
87       * @return -1 if the first UUID is lower than the second UUID, 1 if it's higher, 0
88       * if they are equal  
89       */
90      public int compare( UUID uuid1, UUID uuid2 )
91      {
92          if ( LOG.isDebugEnabled() )
93          {
94              LOG.debug( I18n.msg( I18n.MSG_13751_COMPARING_UUID, uuid1, uuid2 ) );
95          }
96  
97          // -------------------------------------------------------------------
98          // Handle some basis cases
99          // -------------------------------------------------------------------
100         if ( uuid1 == null )
101         {
102             return ( uuid2 == null ) ? 0 : -1;
103         }
104 
105         if ( uuid2 == null )
106         {
107             return 1;
108         }
109 
110         return uuid1.compareTo( uuid2 );
111     }
112 }