1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 * 19 */ 20 package org.apache.directory.server.core.partition.impl.btree; 21 22 23 import org.apache.directory.server.schema.SerializableComparator; 24 25 26 /** 27 * TupleComparator for index records. 28 * 29 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 30 * @version $Rev: 539571 $ 31 */ 32 public class ForwardIndexComparator<K> implements TupleComparator<K,Long> 33 { 34 private static final long serialVersionUID = 3257283621751633459L; 35 36 /** The key comparison to use */ 37 private final SerializableComparator<K> keyComparator; 38 39 40 /** 41 * Creates an IndexComparator. 42 * 43 * @param keyComparator the table comparator to use for keys 44 */ 45 public ForwardIndexComparator( SerializableComparator<K> keyComparator ) 46 { 47 this.keyComparator = keyComparator; 48 } 49 50 51 /** 52 * Gets the comparator used to compare keys. 53 * 54 * @return the comparator for comparing keys. 55 */ 56 public SerializableComparator<K> getKeyComparator() 57 { 58 return keyComparator; 59 } 60 61 62 /** 63 * Gets the binary comparator used to compare values which are the indices 64 * into the master table. 65 * 66 * @return the binary comparator for comparing values. 67 */ 68 public SerializableComparator<Long> getValueComparator() 69 { 70 return LongComparator.INSTANCE; 71 } 72 73 74 /** 75 * Compares key Object to determine their sorting order returning a 76 * value = to, < or > than 0. 77 * 78 * @param key1 the first key to compare 79 * @param key2 the other key to compare to the first 80 * @return 0 if both are equal, a negative value less than 0 if the first 81 * is less than the second, or a postive value if the first is greater than 82 * the second byte array. 83 */ 84 public int compareKey( K key1, K key2 ) 85 { 86 return getKeyComparator().compare( key1, key2 ); 87 } 88 89 90 /** 91 * Comparse value Objects to determine their sorting order returning a 92 * value = to, < or > than 0. 93 * 94 * @param l1 the first Long value to compare 95 * @param l2 the other Long value to compare to the first 96 * @return 0 if both are equal, a negative value less than 0 if the first 97 * is less than the second, or a postive value if the first is greater than 98 * the second Object. 99 */ 100 public int compareValue( Long l1, Long l2 ) 101 { 102 return ( l1 < l2 ? -1 : ( l1.equals( l2 ) ? 0 : 1 ) ); 103 } 104 }