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