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.mavibot.btree; 021 022 023import java.util.Comparator; 024 025 026/** 027 * The Tuple class is used when we browse a btree, it will contain the results 028 * fetched from the btree. 029 * 030 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 031 * 032 * @param <K> The type for the Key 033 * @param <V> The type for the stored value 034 */ 035public class Tuple<K, V> implements Comparable<Tuple<K, V>> 036{ 037 /** The key */ 038 protected K key; 039 040 /** The value */ 041 protected V value; 042 043 /** The key comparator */ 044 protected Comparator<K> keyComparator; 045 046 047 /** 048 * Creates a Tuple with no content 049 */ 050 public Tuple() 051 { 052 } 053 054 055 /** 056 * Creates a Tuple containing a key and its associated value. 057 * @param key The key 058 * @param value The associated value 059 */ 060 public Tuple( K key, V value ) 061 { 062 this.key = key; 063 this.value = value; 064 } 065 066 067 /** 068 * Creates a Tuple containing a key and its associated value. 069 * @param key The key 070 * @param value The associated value 071 */ 072 public Tuple( K key, V value, Comparator<K> keyComparator ) 073 { 074 this.key = key; 075 this.value = value; 076 this.keyComparator = keyComparator; 077 } 078 079 080 /** 081 * @return the key 082 */ 083 public K getKey() 084 { 085 return key; 086 } 087 088 089 /** 090 * @param key the key to set 091 */ 092 public void setKey( K key ) 093 { 094 this.key = key; 095 } 096 097 098 /** 099 * @return the value 100 */ 101 public V getValue() 102 { 103 return value; 104 } 105 106 107 /** 108 * @param value the value to set 109 */ 110 public void setValue( V value ) 111 { 112 this.value = value; 113 } 114 115 116 /** 117 * @see Object#hashCode() 118 */ 119 @Override 120 public int hashCode() 121 { 122 return key.hashCode(); 123 } 124 125 126 /** 127 * @see Object#equals() 128 */ 129 @SuppressWarnings("unchecked") 130 @Override 131 public boolean equals( Object obj ) 132 { 133 if ( this == obj ) 134 { 135 return true; 136 } 137 138 if ( !( obj instanceof Tuple ) ) 139 { 140 return false; 141 } 142 143 if ( this.key == null ) 144 { 145 return ( ( Tuple<K, V> ) obj ).key == null; 146 } 147 148 return this.key.equals( ( ( Tuple<K, V> ) obj ).key ); 149 } 150 151 152 /** 153 * @see java.lang.Comparable#compareTo(java.lang.Object) 154 */ 155 @Override 156 public int compareTo( Tuple<K, V> t ) 157 { 158 if ( keyComparator != null ) 159 { 160 return keyComparator.compare( key, t.key ); 161 } 162 else 163 { 164 return 0; 165 } 166 } 167 168 169 /** 170 * @return the keyComparator 171 */ 172 public Comparator<K> getKeyComparator() 173 { 174 return keyComparator; 175 } 176 177 178 /** 179 * @param keyComparator the keyComparator to set 180 */ 181 public void setKeyComparator( Comparator<K> keyComparator ) 182 { 183 this.keyComparator = keyComparator; 184 } 185 186 187 /** 188 * @see Object#toString() 189 */ 190 public String toString() 191 { 192 return "<" + key + "," + value + ">"; 193 } 194}