Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.util;
027
028import java.io.Serializable;
029
030 /**
031  * <p>A convenience class to represent name-value pairs.</p>
032  */
033public class Pair<K,V> implements Serializable{
034
035    /**
036     * Key of this <code>Pair</code>.
037     */
038    private K key;
039
040    /**
041     * Gets the key for this pair.
042     * @return key for this pair
043     */
044    public K getKey() { return key; }
045
046    /**
047     * Value of this this <code>Pair</code>.
048     */
049    private V value;
050
051    /**
052     * Gets the value for this pair.
053     * @return value for this pair
054     */
055    public V getValue() { return value; }
056
057    /**
058     * Creates a new pair
059     * @param key The key for this pair
060     * @param value The value to use for this pair
061     */
062    public Pair(K key, V value) {
063        this.key = key;
064        this.value = value;
065    }
066
067    /**
068     * <p><code>String</code> representation of this
069     * <code>Pair</code>.</p>
070     *
071     * <p>The default name/value delimiter '=' is always used.</p>
072     *
073     *  @return <code>String</code> representation of this <code>Pair</code>
074     */
075    @Override
076    public String toString() {
077        return key + "=" + value;
078    }
079
080    /**
081     * <p>Generate a hash code for this <code>Pair</code>.</p>
082     *
083     * <p>The hash code is calculated using both the name and
084     * the value of the <code>Pair</code>.</p>
085     *
086     * @return hash code for this <code>Pair</code>
087     */
088    @Override
089    public int hashCode() {
090        // name's hashCode is multiplied by an arbitrary prime number (13)
091        // in order to make sure there is a difference in the hashCode between
092        // these two parameters:
093        //  name: a  value: aa
094        //  name: aa value: a
095        return key.hashCode() * 13 + (value == null ? 0 : value.hashCode());
096    }
097
098     /**
099      * <p>Test this <code>Pair</code> for equality with another
100      * <code>Object</code>.</p>
101      *
102      * <p>If the <code>Object</code> to be tested is not a
103      * <code>Pair</code> or is <code>null</code>, then this method
104      * returns <code>false</code>.</p>
105      *
106      * <p>Two <code>Pair</code>s are considered equal if and only if
107      * both the names and values are equal.</p>
108      *
109      * @param o the <code>Object</code> to test for
110      * equality with this <code>Pair</code>
111      * @return <code>true</code> if the given <code>Object</code> is
112      * equal to this <code>Pair</code> else <code>false</code>
113      */
114     @Override
115     public boolean equals(Object o) {
116         if (this == o) return true;
117         if (o instanceof Pair) {
118             Pair pair = (Pair) o;
119             if (key != null ? !key.equals(pair.key) : pair.key != null) return false;
120             if (value != null ? !value.equals(pair.value) : pair.value != null) return false;
121             return true;
122         }
123         return false;
124     }
125 }
126