View Javadoc
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.mavibot.btree;
21  
22  
23  import java.util.Comparator;
24  
25  
26  /**
27   * The Tuple class is used when we browse a btree, it will contain the results
28   * fetched from the btree.
29   * 
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   *
32   * @param <K> The type for the Key
33   * @param <V> The type for the stored value
34   */
35  public class Tuple<K, V> implements Comparable<Tuple<K, V>>
36  {
37      /** The key */
38      protected K key;
39  
40      /** The value */
41      protected V value;
42  
43      /** The key comparator */
44      protected Comparator<K> keyComparator;
45  
46  
47      /**
48       * Creates a Tuple with no content
49       */
50      public Tuple()
51      {
52      }
53  
54  
55      /**
56       * Creates a Tuple containing a key and its associated value.
57       * @param key The key
58       * @param value The associated value
59       */
60      public Tuple( K key, V value )
61      {
62          this.key = key;
63          this.value = value;
64      }
65  
66  
67      /**
68       * Creates a Tuple containing a key and its associated value.
69       * @param key The key
70       * @param value The associated value
71       */
72      public Tuple( K key, V value, Comparator<K> keyComparator )
73      {
74          this.key = key;
75          this.value = value;
76          this.keyComparator = keyComparator;
77      }
78  
79  
80      /**
81       * @return the key
82       */
83      public K getKey()
84      {
85          return key;
86      }
87  
88  
89      /**
90       * @param key the key to set
91       */
92      public void setKey( K key )
93      {
94          this.key = key;
95      }
96  
97  
98      /**
99       * @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 }