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.api.ldap.model.cursor;
021
022
023/**
024 * A key/value tuple for simple two column persistent Tables with sorted keys.
025 * 
026 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
027 * @param <K> The key type for the Tuple
028 * @param <V> The associated Value type
029 */
030public class Tuple<K, V>
031{
032    /** the key for this Tuple */
033    private K key;
034
035    /** the value for this Tuple */
036    private V value;
037
038
039    /**
040     * Do nothing default that has a null key and null value.
041     */
042    public Tuple()
043    {
044        // does nothing!
045    }
046
047
048    /**
049     * Creates a Tuple using a key and a value.
050     * 
051     * @param key the key to set
052     * @param value the value to set
053     */
054    public Tuple( K key, V value )
055    {
056        this.key = key;
057        this.value = value;
058    }
059
060
061    /**
062     * Gets the key for this Tuple.
063     *
064     * @return the Tuple's key
065     */
066    public K getKey()
067    {
068        return key;
069    }
070
071
072    /**
073     * Sets the key for this Tuple.
074     *
075     * @param key the new key to set
076     * @return this Tuple itself to set and return
077     */
078    public Tuple<K, V> setKey( K key )
079    {
080        this.key = key;
081
082        return this;
083    }
084
085
086    /**
087     * Gets the value for this Tuple.
088     *
089     * @return the Tuple's value
090     */
091    public V getValue()
092    {
093        return value;
094    }
095
096
097    /**
098     * Sets the value for this Tuple.
099     *
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}