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         int hash = 7;
  93         hash = 31 * hash + (key != null ? key.hashCode() : 0);
  94         hash = 31 * hash + (value != null ? value.hashCode() : 0);
  95         return hash;
  96     }
  97 
  98      /**
  99       * <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