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.api.ldap.model.cursor;
21  
22  
23  /**
24   * A key/value tuple for simple two column persistent Tables with sorted keys.
25   * 
26   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
27   * @param <K> The key type for the Tuple
28   * @param <V> The associated Value type
29   */
30  public class Tuple<K, V>
31  {
32      /** the key for this Tuple */
33      private K key;
34  
35      /** the value for this Tuple */
36      private V value;
37  
38  
39      /**
40       * Do nothing default that has a null key and null value.
41       */
42      public Tuple()
43      {
44          // does nothing!
45      }
46  
47  
48      /**
49       * Creates a Tuple using a key and a value.
50       * 
51       * @param key the key to set
52       * @param value the value to set
53       */
54      public Tuple( K key, V value )
55      {
56          this.key = key;
57          this.value = value;
58      }
59  
60  
61      /**
62       * Gets the key for this Tuple.
63       *
64       * @return the Tuple's key
65       */
66      public K getKey()
67      {
68          return key;
69      }
70  
71  
72      /**
73       * Sets the key for this Tuple.
74       *
75       * @param key the new key to set
76       * @return this Tuple itself to set and return
77       */
78      public Tuple<K, V> setKey( K key )
79      {
80          this.key = key;
81  
82          return this;
83      }
84  
85  
86      /**
87       * Gets the value for this Tuple.
88       *
89       * @return the Tuple's value
90       */
91      public V getValue()
92      {
93          return value;
94      }
95  
96  
97      /**
98       * Sets the value for this Tuple.
99       *
100      * @param value the new value to set
101      * @return this Tuple itself to set and return
102      */
103     public Tuple<K, V> setValue( V value )
104     {
105         this.value = value;
106 
107         return this;
108     }
109 
110 
111     /**
112      * Sets both the key and the value for this Tuple in one call and returns
113      * this Tuple object.  This is useful for setting the tuples key and value
114      * then returning it.
115      *
116      * @param key the new key to set
117      * @param value the new value to set
118      * @return this Tuple itself to set and return
119      */
120     public Tuple<K, V> setBoth( K key, V value )
121     {
122         this.key = key;
123         this.value = value;
124 
125         return this;
126     }
127 
128 
129     /**
130      * Sets both the key and the value for this Tuple in one call and returns
131      * this Tuple object.  This is useful for setting the tuples key and value
132      * then returning it.
133      *
134      * @param tupleToCopy the tuple to copy
135      * @return this Tuple itself to set and return
136      */
137     public Tuple<K, V> setBoth( Tuple<K, V> tupleToCopy )
138     {
139         this.key = tupleToCopy.key;
140         this.value = tupleToCopy.value;
141 
142         return this;
143     }
144 
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public int hashCode()
151     {
152         int prime = 31;
153         int result = 1;
154 
155         if ( key == null )
156         {
157             result = prime * result;
158         }
159         else
160         {
161             result = prime * result + key.hashCode();
162         }
163 
164         if ( value == null )
165         {
166             result = prime * result;
167         }
168         else
169         {
170             result = prime * result + value.hashCode();
171         }
172 
173         return result;
174     }
175 
176 
177     /**
178      * {@inheritDoc}
179      */
180     @Override
181     public boolean equals( Object obj )
182     {
183         if ( this == obj )
184         {
185             return true;
186         }
187 
188         if ( obj == null )
189         {
190             return false;
191         }
192 
193         if ( getClass() != obj.getClass() )
194         {
195             return false;
196         }
197 
198         Tuple<?, ?> other = ( Tuple<?, ?> ) obj;
199 
200         if ( key == null )
201         {
202             if ( other.key != null )
203             {
204                 return false;
205             }
206         }
207         else if ( !key.equals( other.key ) )
208         {
209             return false;
210         }
211 
212         if ( value == null )
213         {
214             if ( other.value != null )
215             {
216                 return false;
217             }
218         }
219         else if ( !value.equals( other.value ) )
220         {
221             return false;
222         }
223 
224         return true;
225     }
226 
227 
228     /**
229      * {@inheritDoc}
230      */
231     @Override
232     public String toString()
233     {
234         StringBuilder buf = new StringBuilder();
235 
236         buf.append( "Tuple( '" );
237         buf.append( key );
238         buf.append( "', '" );
239         buf.append( value );
240         buf.append( "' )" );
241 
242         return buf.toString();
243     }
244 }