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 ReverseIndexComparator<V> implements TupleComparator<Long,V> 33 { 34 private static final long serialVersionUID = 3257283621751633459L; 35 36 /** The value comparison to use - keys are Longs */ 37 private final SerializableComparator<V> valueComparator; 38 39 40 /** 41 * Creates an IndexComparator. 42 * 43 * @param valueComparator the table comparator to use for values 44 */ 45 public ReverseIndexComparator( SerializableComparator<V> valueComparator ) 46 { 47 this.valueComparator = valueComparator; 48 } 49 50 51 /** 52 * Gets the comparator used to compare keys. 53 * 54 * @return the comparator for comparing keys. 55 */ 56 public SerializableComparator<Long> getKeyComparator() 57 { 58 return LongComparator.INSTANCE; 59 } 60 61 62 /** 63 * Gets the binary comparator used to compare values which are the values 64 * of attributes. 65 * 66 * @return the binary comparator for comparing values. 67 */ 68 public SerializableComparator<V> getValueComparator() 69 { 70 return valueComparator; 71 } 72 73 74 /** 75 * Compares key Object to determine their sorting order returning a 76 * value = to, < or > than 0. 77 * 78 * @param l1 the first long key to compare 79 * @param l2 the other long 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( Long l1, Long l2 ) 85 { 86 return ( l1 < l2 ? -1 : ( l1.equals( l2 ) ? 0 : 1 ) ); 87 } 88 89 90 /** 91 * Comparse value Objects to determine their sorting order returning a 92 * value = to, < or > than 0. 93 * 94 * @param v1 the first value to compare 95 * @param v2 the other 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( V v1, V v2 ) 101 { 102 return valueComparator.compare( v1, v2 ); 103 } 104 }