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 */
020
021package org.apache.directory.mavibot.btree;
022
023
024import java.util.Comparator;
025
026
027/**
028 * An comparator that encapsulate a Comparator to compare two tuples
029 * using their key.
030 *
031 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
032 */
033public class TupleComparator<K, V> implements Comparator<Tuple<K, V>>
034{
035    /** the embedded Comparator to use for the key comparison */
036    Comparator<K> keyComparator;
037
038    /** the embedded Comparator to use for the value comparison */
039    Comparator<V> valueComparator;
040
041
042    /**
043     * Creates a new instance of TupleComparator.
044     *
045     * @param keyComparator The inner key comparator
046     * @param valueComparator The inner value comparator
047     */
048    public TupleComparator( Comparator<K> keyComparator, Comparator<V> valueComparator )
049    {
050        this.keyComparator = keyComparator;
051    }
052
053
054    /**
055     * Compare two tuples. We compare the keys only. 
056     * 
057     * @param t1 The first tuple
058     * @param t2 The second tuple
059     * @return There are 5 possible results :
060     * <ul>
061     * <li>-1 : the first key is below the second key </li>
062     * <li>0 : the two keys are equals, keys and values</li>
063     * <li>1 : the first key is above the second key</li>
064     * </ul>
065     */
066    @Override
067    public int compare( Tuple<K, V> t1, Tuple<K, V> t2 )
068    {
069        return keyComparator.compare( t1.key, t2.key );
070    }
071}