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.apache.directory.api.i18n.I18n;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029
030/**
031 * A comparator for UUID. We simply use the UUID compareTo method.
032 * 
033 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
034 */
035public class UuidComparator extends SerializableComparator<String>
036{
037    /** The serial version UID */
038    private static final long serialVersionUID = 2L;
039
040    /** A logger for this class */
041    private static final Logger LOG = LoggerFactory.getLogger( UuidComparator.class );
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 ( LOG.isDebugEnabled() )
066        {
067            LOG.debug( I18n.msg( I18n.MSG_13751_COMPARING_UUID, uuid1, uuid2 ) );
068        }
069
070        // -------------------------------------------------------------------
071        // Handle some basis cases
072        // -------------------------------------------------------------------
073        if ( uuid1 == null )
074        {
075            return -1;
076        }
077
078        return uuid1.compareTo( uuid2 );
079    }
080
081
082    /**
083     * Compare two UUID.
084     * 
085     * @param uuid1 The first UUID
086     * @param uuid2 he second UUID
087     * @return -1 if the first UUID is lower than the second UUID, 1 if it's higher, 0
088     * if they are equal  
089     */
090    public int compare( UUID uuid1, UUID uuid2 )
091    {
092        if ( LOG.isDebugEnabled() )
093        {
094            LOG.debug( I18n.msg( I18n.MSG_13751_COMPARING_UUID, uuid1, uuid2 ) );
095        }
096
097        // -------------------------------------------------------------------
098        // Handle some basis cases
099        // -------------------------------------------------------------------
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}