001    /*--------------------------------------------------------------------------+
002    $Id: Pair.java 26283 2010-02-18 11:18:57Z juergens $
003    |                                                                          |
004    | Copyright 2005-2010 Technische Universitaet Muenchen                     |
005    |                                                                          |
006    | Licensed under the Apache License, Version 2.0 (the "License");          |
007    | you may not use this file except in compliance with the License.         |
008    | 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, software      |
013    | distributed under the License is distributed on an "AS IS" BASIS,        |
014    | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
015    | See the License for the specific language governing permissions and      |
016    | limitations under the License.                                           |
017    +--------------------------------------------------------------------------*/
018    package edu.tum.cs.commons.collections;
019    
020    import edu.tum.cs.commons.clone.DeepCloneException;
021    
022    /**
023     * Simple pair class.
024     * 
025     * @author hummelb
026     * @author $Author: juergens $
027     * @version $Rev: 26283 $
028     * @levd.rating GREEN Hash: 78B8598462B008191EA7B36314D99571
029     */
030    public class Pair<S, T> extends ImmutablePair<S, T> {
031    
032            /** Constructor. */
033            public Pair(S first, T second) {
034                    super(first, second);
035            }
036    
037            /** Copy constructor. */
038            public Pair(ImmutablePair<S, T> p) {
039                    super(p);
040            }
041    
042            /** Set the first value. */
043            public void setFirst(S first) {
044                    this.first = first;
045            }
046    
047            /** Set the second value. */
048            public void setSecond(T second) {
049                    this.second = second;
050            }
051    
052            /** {@inheritDoc} */
053            @Override
054            protected Pair<S, T> clone() {
055                    return new Pair<S, T>(this);
056            }
057    
058            /** {@inheritDoc} */
059            @Override
060            public Pair<S, T> deepClone() throws DeepCloneException {
061                    return new Pair<S, T>(super.deepClone());
062            }
063    
064            /**
065             * Converts a string comma separated integers to a pair of Integers.
066             * 
067             * @throws NumberFormatException
068             *             if the format does not match
069             */
070            public static Pair<Integer, Integer> parseIntPair(String string) {
071                    String[] tokens = string.split(",\\s*");
072                    if (tokens.length != 2) {
073                            throw new NumberFormatException(
074                                            "Invalid number of comma separated tokens!");
075                    }
076    
077                    return new Pair<Integer, Integer>(Integer.parseInt(tokens[0]), Integer
078                                    .parseInt(tokens[1]));
079            }
080    }