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.server.core.partition.impl.btree;
021
022
023import org.apache.directory.api.ldap.model.schema.comparators.SerializableComparator;
024
025
026/**
027 * TupleComparator for index records.
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 */
031public class ForwardIndexComparator<K> implements TupleComparator<K, Long>
032{
033    private static final long serialVersionUID = 3257283621751633459L;
034
035    /** The key comparison to use */
036    private final SerializableComparator<K> keyComparator;
037
038
039    /**
040     * Creates an IndexComparator.
041     *
042     * @param keyComparator the table comparator to use for keys
043     */
044    public ForwardIndexComparator( SerializableComparator<K> keyComparator )
045    {
046        this.keyComparator = keyComparator;
047    }
048
049
050    /**
051     * Gets the comparator used to compare keys.
052     *
053     * @return the comparator for comparing keys.
054     */
055    public SerializableComparator<K> getKeyComparator()
056    {
057        return keyComparator;
058    }
059
060
061    /**
062     * Gets the binary comparator used to compare values which are the indices
063     * into the master table.
064     *
065     * @return the binary comparator for comparing values.
066     */
067    public SerializableComparator<Long> getValueComparator()
068    {
069        return LongComparator.INSTANCE;
070    }
071
072
073    /**
074     * Compares key Object to determine their sorting order returning a
075     * value = to, &lt; or &gt; than 0.
076     *
077     * @param key1 the first key to compare
078     * @param key2 the other key to compare to the first
079     * @return 0 if both are equal, a negative value less than 0 if the first
080     * is less than the second, or a poistive value if the first is greater than
081     * the second byte array.
082     */
083    public int compareKey( K key1, K key2 )
084    {
085        return getKeyComparator().compare( key1, key2 );
086    }
087
088
089    /**
090     * Comparse value Objects to determine their sorting order returning a
091     * value = to, &lt; or &gt; than 0.
092     *
093     * @param l1 the first Long value to compare
094     * @param l2 the other Long value to compare to the first
095     * @return 0 if both are equal, a negative value less than 0 if the first
096     * is less than the second, or a positive value if the first is greater than
097     * the second Object.
098     */
099    public int compareValue( Long l1, Long l2 )
100    {
101        return ( l1 < l2 ? -1 : ( l1.equals( l2 ) ? 0 : 1 ) );
102    }
103}