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 java.util.Objects;
  30 import javafx.beans.NamedArg;
  31 
  32  /**
  33   * <p>A convenience class to represent name-value pairs.</p>
  34   * @since JavaFX 2.0
  35   */
  36 public class Pair<K,V> implements Serializable{
  37 
  38     /**
  39      * Key of this <code>Pair</code>.
  40      */
  41     private K key;
  42 
  43     /**
  44      * Gets the key for this pair.
  45      * @return key for this pair
  46      */
  47     public K getKey() { return key; }
  48 
  49     /**
  50      * Value of this this <code>Pair</code>.
  51      */
  52     private V value;
  53 
  54     /**
  55      * Gets the value for this pair.
  56      * @return value for this pair
  57      */
  58     public V getValue() { return value; }
  59 
  60     /**
  61      * Creates a new pair
  62      * @param key The key for this pair
  63      * @param value The value to use for this pair
  64      */
  65     public Pair(@NamedArg("key") K key, @NamedArg("value") V value) {
  66         this.key = key;
  67         this.value = value;
  68     }
  69 
  70     /**
  71      * <p><code>String</code> representation of this
  72      * <code>Pair</code>.</p>
  73      *
  74      * <p>The default name/value delimiter '=' is always used.</p>
  75      *
  76      *  @return <code>String</code> representation of this <code>Pair</code>
  77      */
  78     @Override
  79     public String toString() {
  80         return key + "=" + value;
  81     }
  82 
  83     /**
  84      * <p>Generate a hash code for this <code>Pair</code>.</p>
  85      *
  86      * <p>The hash code is calculated using both the name and
  87      * the value of the <code>Pair</code>.</p>
  88      *
  89      * @return hash code for this <code>Pair</code>
  90      */
  91     @Override
  92     public int hashCode() {
  93         return Objects.hash(key, value);
  94     }
  95 
  96      /**
  97       * <p>Test this <code>Pair</code> for equality with another
  98       * <code>Object</code>.</p>
  99       *
 100       * <p>If the <code>Object</code> to be tested is not a
 101       * <code>Pair</code> or is <code>null</code>, then this method
 102       * returns <code>false</code>.</p>
 103       *
 104       * <p>Two <code>Pair</code>s are considered equal if and only if
 105       * both the names and values are equal.</p>
 106       *
 107       * @param o the <code>Object</code> to test for
 108       * equality with this <code>Pair</code>
 109       * @return <code>true</code> if the given <code>Object</code> is
 110       * equal to this <code>Pair</code> else <code>false</code>
 111       */
 112      @Override
 113      public boolean equals(Object o) {
 114          if (this == o) return true;
 115          if (o instanceof Pair) {
 116              Pair pair = (Pair) o;
 117              if (key != null ? !key.equals(pair.key) : pair.key != null) return false;
 118              if (value != null ? !value.equals(pair.value) : pair.value != null) return false;
 119              return true;
 120          }
 121          return false;
 122      }
 123  }
 124