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 ForwardIndexComparator<K> implements TupleComparator<K, Long>
033 {
034 private static final long serialVersionUID = 3257283621751633459L;
035
036 /** The key comparison to use */
037 private final SerializableComparator<K> keyComparator;
038
039
040 /**
041 * Creates an IndexComparator.
042 *
043 * @param keyComparator the table comparator to use for keys
044 */
045 public ForwardIndexComparator( SerializableComparator<K> keyComparator )
046 {
047 this.keyComparator = keyComparator;
048 }
049
050
051 /**
052 * Gets the comparator used to compare keys.
053 *
054 * @return the comparator for comparing keys.
055 */
056 public SerializableComparator<K> getKeyComparator()
057 {
058 return keyComparator;
059 }
060
061
062 /**
063 * Gets the binary comparator used to compare values which are the indices
064 * into the master table.
065 *
066 * @return the binary comparator for comparing values.
067 */
068 public SerializableComparator<Long> getValueComparator()
069 {
070 return LongComparator.INSTANCE;
071 }
072
073
074 /**
075 * Compares key Object to determine their sorting order returning a
076 * value = to, < or > than 0.
077 *
078 * @param key1 the first key to compare
079 * @param key2 the other 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( K key1, K key2 )
085 {
086 return getKeyComparator().compare( key1, key2 );
087 }
088
089
090 /**
091 * Comparse value Objects to determine their sorting order returning a
092 * value = to, < or > than 0.
093 *
094 * @param l1 the first Long value to compare
095 * @param l2 the other Long 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( Long l1, Long l2 )
101 {
102 return ( l1 < l2 ? -1 : ( l1.equals( l2 ) ? 0 : 1 ) );
103 }
104 }