001    /* JAPI - (Yet another (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License as
007     * published by the Free Software Foundation; either version 2 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful, but
011     * WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.util;
022    
023    /** Lightweight class for data pairs.
024     * The hashcode of a pair is the combined hashcode of its values.
025     * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
026     */
027    public class Pair<T1,T2> {
028    
029        /** First data element. */
030        private final T1 first;
031    
032        /** Second data element. */
033        private final T2 second;
034    
035        /** Create a Pair.
036         * @param first First data element
037         * @param second Second data element
038         */
039        public Pair(final T1 first, final T2 second) {
040            this.first = first;
041            this.second = second;
042        }
043    
044        /** Get first member of this pair.
045         * @return first member of this pair
046         */
047        public T1 getFirst() {
048            return first;
049        }
050    
051        /** Get second member of this pair.
052         * @return second member of this pair
053         */
054        public T2 getSecond() {
055            return second;
056        }
057    
058        /** {@inheritDoc} */
059        @Override public boolean equals(final Object obj) {
060            if (obj == null || !(obj instanceof Pair)) {
061                return false;
062            }
063            final Pair<?,?> other = (Pair<?,?>) obj;
064            return first.equals(other.first) && second.equals(other.second);
065        }
066    
067        /** {@inheritDoc} */
068        @Override public int hashCode() {
069            return first.hashCode() ^ second.hashCode();
070        }
071    
072    } // class Pair