001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.api.ldap.model.schema.comparators;
021
022
023import java.util.UUID;
024
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028
029/**
030 * A comparator for UUID. We simply use the UUID compareTo method.
031 * 
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 */
034public class UuidComparator extends SerializableComparator<String>
035{
036    /** The serial version UID */
037    private static final long serialVersionUID = 2L;
038
039    /** A logger for this class */
040    private static final Logger LOG = LoggerFactory.getLogger( UuidComparator.class );
041    private static final boolean IS_DEBUG = LOG.isDebugEnabled();
042
043    /** A static instance of the UuidComparator */
044    public static final UuidComparator INSTANCE = new UuidComparator( "1.3.6.1.1.16.4" );
045
046
047    /**
048     * The UUIDComparator constructor. Its OID is the UUIDMatch matching
049     * rule OID.
050     * 
051     * @param oid The Comparator's OID
052     */
053    public UuidComparator( String oid )
054    {
055        super( oid );
056    }
057
058
059    /**
060     * {@inheritDoc}
061     */
062    @Override
063    public int compare( String uuid1, String uuid2 )
064    {
065        if ( IS_DEBUG )
066        {
067            LOG.debug( "comparing UUID objects '{}' with '{}'", uuid1, uuid2 );
068        }
069
070        // -------------------------------------------------------------------
071        // Handle some basis cases
072        // -------------------------------------------------------------------
073        if ( uuid1 == null )
074        {
075            return ( uuid2 == null ) ? 0 : -1;
076        }
077
078        if ( uuid2 == null )
079        {
080            return 1;
081        }
082
083        return uuid1.compareTo( uuid2 );
084    }
085
086
087    /**
088     * Compare two UUID.
089     * 
090     * @param uuid1 The first UUID
091     * @param uuid2 he second UUID
092     * @return -1 if the first UUID is lower than the second UUID, 1 if it's higher, 0
093     * if they are equal  
094     */
095    public int compare( UUID uuid1, UUID uuid2 )
096    {
097        if ( IS_DEBUG )
098        {
099            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}