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     */
020    package org.apache.directory.server.xdbm;
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     * @version $Rev: 917231 $
028     */
029    public class Tuple<K, V>
030    {
031        /** the key for this Tuple */
032        private K key;
033        /** the value for this Tuple */
034        private V value;
035    
036    
037        /**
038         * Do nothing default that has a null key and null value.
039         */
040        public Tuple()
041        {
042            // does nothing!
043        }
044    
045    
046        /**
047         * Creates a Tuple using a key and a value.
048         * 
049         * @param key the key to set
050         * @param value the value to set
051         */
052        public Tuple( K key, V value )
053        {
054            this.key = key;
055            this.value = value;
056        }
057    
058    
059        /**
060         * Gets the key for this Tuple.
061         *
062         * @return the Tuple's key
063         */
064        public K getKey()
065        {
066            return key;
067        }
068    
069    
070        /**
071         * Sets the key for this Tuple.
072         *
073         * @param key the new key to set
074         * @return this Tuple itself to set and return
075         */
076        public Tuple<K, V> setKey( K key )
077        {
078            this.key = key;
079            return this;
080        }
081    
082    
083        /**
084         * Gets the value for this Tuple.
085         *
086         * @return the Tuple's value
087         */
088        public V getValue()
089        {
090            return value;
091        }
092    
093    
094        /**
095         * Sets the value for this Tuple.
096         *
097         * @param value the new value to set
098         * @return this Tuple itself to set and return
099         */
100        public Tuple<K, V> setValue( V value )
101        {
102            this.value = value;
103            return this;
104        }
105    
106    
107        /**
108         * Sets both the key and the value for this Tuple in one call and returns
109         * this Tuple object.  This is useful for setting the tuples key and value
110         * then returning it.
111         *
112         * @param key the new key to set
113         * @param value the new value to set
114         * @return this Tuple itself to set and return
115         */
116        public Tuple<K, V> setBoth( K key, V value )
117        {
118            this.key = key;
119            this.value = value;
120            return this;
121        }
122    
123    
124        /**
125         * Sets both the key and the value for this Tuple in one call and returns
126         * this Tuple object.  This is useful for setting the tuples key and value
127         * then returning it.
128         *
129         * @param tupleToCopy the tuple to copy
130         * @return this Tuple itself to set and return
131         */
132        public Tuple<K, V> setBoth( Tuple<K, V> tupleToCopy )
133        {
134            this.key = tupleToCopy.key;
135            this.value = tupleToCopy.value;
136            return this;
137        }
138    
139    
140        public String toString()
141        {
142            StringBuilder buf = new StringBuilder();
143            buf.append( "Tuple( '" );
144            buf.append( key );
145            buf.append( "', '" );
146            buf.append( value );
147            buf.append( "' )" );
148            return buf.toString();
149        }
150    }