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    package org.apache.directory.server.core.partition.impl.btree;
021    
022    
023    import org.apache.directory.shared.ldap.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     * @version $Rev: 539571 $
031     */
032    public class ReverseIndexComparator<V> implements TupleComparator<Long, V>
033    {
034        private static final long serialVersionUID = 3257283621751633459L;
035    
036        /** The value comparison to use - keys are Longs */
037        private final SerializableComparator<V> valueComparator;
038    
039    
040        /**
041         * Creates an IndexComparator.
042         *
043         * @param valueComparator the table comparator to use for values
044         */
045        public ReverseIndexComparator( SerializableComparator<V> valueComparator )
046        {
047            this.valueComparator = valueComparator;
048        }
049    
050    
051        /**
052         * Gets the comparator used to compare keys.
053         *
054         * @return the comparator for comparing keys.
055         */
056        public SerializableComparator<Long> getKeyComparator()
057        {
058            return LongComparator.INSTANCE;
059        }
060    
061    
062        /**
063         * Gets the binary comparator used to compare values which are the values
064         * of attributes.
065         *
066         * @return the binary comparator for comparing values.
067         */
068        public SerializableComparator<V> getValueComparator()
069        {
070            return valueComparator;
071        }
072    
073    
074        /**
075         * Compares key Object to determine their sorting order returning a
076         * value = to, < or > than 0.
077         *
078         * @param l1 the first long key to compare
079         * @param l2 the other long key to compare to the first
080         * @return 0 if both are equal, a negative value less than 0 if the first
081         * is less than the second, or a postive value if the first is greater than
082         * the second byte array.
083         */
084        public int compareKey( Long l1, Long l2 )
085        {
086            return ( l1 < l2 ? -1 : ( l1.equals( l2 ) ? 0 : 1 ) );
087        }
088    
089    
090        /**
091         * Comparse value Objects to determine their sorting order returning a
092         * value = to, < or > than 0.
093         *
094         * @param v1 the first value to compare
095         * @param v2 the other value to compare to the first
096         * @return 0 if both are equal, a negative value less than 0 if the first
097         * is less than the second, or a postive value if the first is greater than
098         * the second Object.
099         */
100        public int compareValue( V v1, V v2 )
101        {
102            return valueComparator.compare( v1, v2 );
103        }
104    }