1 /*
   2  * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javafx.util;
  27 
  28 import java.io.Serializable;
  29 import javafx.beans.NamedArg;
  30 
  31  /**
  32   * <p>A convenience class to represent name-value pairs.</p>
  33   * @since JavaFX 2.0
  34   */
  35 public class Pair<K,V> implements Serializable{
  36 
  37     /**
  38      * Key of this <code>Pair</code>.
  39      */
  40     private K key;
  41 
  42     /**
  43      * Gets the key for this pair.
  44      * @return key for this pair
  45      */
  46     public K getKey() { return key; }
  47 
  48     /**
  49      * Value of this this <code>Pair</code>.
  50      */
  51     private V value;
  52 
  53     /**
  54      * Gets the value for this pair.
  55      * @return value for this pair
  56      */
  57     public V getValue() { return value; }
  58 
  59     /**
  60      * Creates a new pair
  61      * @param key The key for this pair
  62      * @param value The value to use for this pair
  63      */
  64     public Pair(@NamedArg("key") K key, @NamedArg("value") V value) {
  65         this.key = key;
  66         this.value = value;
  67     }
  68 
  69     /**
  70      * <p><code>String</code> representation of this
  71      * <code>Pair</code>.</p>
  72      *
  73      * <p>The default name/value delimiter '=' is always used.</p>
  74      *
  75      *  @return <code>String</code> representation of this <code>Pair</code>
  76      */
  77     @Override
  78     public String toString() {
  79         return key + "=" + value;
  80     }
  81 
  82     /**
  83      * <p>Generate a hash code for this <code>Pair</code>.</p>
  84      *
  85      * <p>The hash code is calculated using both the name and
  86      * the value of the <code>Pair</code>.</p>
  87      *
  88      * @return hash code for this <code>Pair</code>
  89      */
  90     @Override
  91     public int hashCode() {
  92         // name's hashCode is multiplied by an arbitrary prime number (13)
  93         // in order to make sure there is a difference in the hashCode between
  94         // these two parameters:
  95         //  name: a  value: aa
  96         //  name: aa value: a
  97         return key.hashCode() * 13 + (value == null ? 0 : value.hashCode());
  98     }
  99 
 100      /**
 101       * <p>Test this <code>Pair</code> for equality with another
 102       * <code>Object</code>.</p>
 103       *
 104       * <p>If the <code>Object</code> to be tested is not a
 105       * <code>Pair</code> or is <code>null</code>, then this method
 106       * returns <code>false</code>.</p>
 107       *
 108       * <p>Two <code>Pair</code>s are considered equal if and only if
 109       * both the names and values are equal.</p>
 110       *
 111       * @param o the <code>Object</code> to test for
 112       * equality with this <code>Pair</code>
 113       * @return <code>true</code> if the given <code>Object</code> is
 114       * equal to this <code>Pair</code> else <code>false</code>
 115       */
 116      @Override
 117      public boolean equals(Object o) {
 118          if (this == o) return true;
 119          if (o instanceof Pair) {
 120              Pair pair = (Pair) o;
 121              if (key != null ? !key.equals(pair.key) : pair.key != null) return false;
 122              if (value != null ? !value.equals(pair.value) : pair.value != null) return false;
 123              return true;
 124          }
 125          return false;
 126      }
 127  }
 128