001 package com.softnetConsult.utils.collections; 002 003 /** 004 * A convenience memory structure to hold two objects. 005 * 006 * <p style="font-size:smaller;">This product includes software developed by the 007 * <strong>SoftNet-Consult Java Utility Library</strong> project and its contributors.<br /> 008 * (<a href="http://java-tools.sourceforge.net" target="_blank">http://java-tools.sourceforge.net</a>)<br /> 009 * Copyright (c) 2007-2008 SoftNet-Consult.<br /> 010 * Copyright (c) 2007-2008 G. Paperin.<br /> 011 * All rights reserved. 012 * </p> 013 * <p style="font-size:smaller;">File: Pair.java<br /> 014 * Library API version: {@value com.softnetConsult.utils.APIProperties#apiVersion}<br /> 015 * Java compliance version: {@value com.softnetConsult.utils.APIProperties#javaComplianceVersion} 016 * </p> 017 * <p style="font-size:smaller;">Redistribution and use in source and binary forms, with or 018 * without modification, are permitted provided that the following terms and conditions are met: 019 * </p> 020 * <p style="font-size:smaller;">1. Redistributions of source code must retain the above 021 * acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright 022 * notice, this list of conditions and the following disclaimer.<br /> 023 * 2. Redistributions in binary form must reproduce the above acknowledgement of the 024 * SoftNet-Consult Java Utility Library project, the above copyright notice, this list of 025 * conditions and the following disclaimer in the documentation and/or other materials 026 * provided with the distribution.<br /> 027 * 3. All advertising materials mentioning features or use of this software or any derived 028 * software must display the following acknowledgement:<br /> 029 * <em>This product includes software developed by the SoftNet-Consult Java Utility Library 030 * project and its contributors.</em> 031 * </p> 032 * <p style="font-size:smaller;">THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY 033 * OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 034 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 035 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 036 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 037 * IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 038 * </p> 039 * @author Greg Paperin (<a href="http://www.paperin.org" target="_blank">http://www.paperin.org</a>) 040 * @version {@value com.softnetConsult.utils.APIProperties#apiVersion} 041 * 042 * @param <T1> Type of the first object of this {@code Pair}. 043 * @param <T2> Type of the second object of this {@code Pair}. 044 */ 045 public class Pair<T1, T2> { 046 047 /** 048 * The first object of this {@code Pair}. 049 */ 050 public T1 elem1 = null; 051 052 /** 053 * The second object of this {@code Pair}. 054 */ 055 public T2 elem2 = null; 056 057 /** 058 * Creates a new pair of the specified objects. {@code null} objects are permitted. 059 * 060 * @param elem1 The first object for this {@code Pair}. 061 * @param elem2 The second object for this {@code Pair}. 062 */ 063 public Pair(T1 elem1, T2 elem2) { 064 this.elem1 = elem1; 065 this.elem2 = elem2; 066 } 067 068 /** 069 * Returns whether this {@code Pair} is equal to a specified object. 070 * A {@code Pair} {@code p} is equal to an object {@code o} if and only 071 * if {@code o} is of class (of subclass of) {@code Pair} and 072 * {@code p.equals((Pair<?, ?>) o)} returns {@code true}. 073 * 074 * @return {@code true} if this {@code Pair} is equal to the specified object, 075 * {@code false} otherwise. 076 * @see #equals(Pair) 077 */ 078 @Override 079 public boolean equals(Object o) { 080 081 if (! (o instanceof Pair)) 082 return false; 083 084 return this.equals((Pair<?, ?>) o); 085 } 086 087 /** 088 * Returns whether this {@code Pair} is equal to a specified {@code Pair}. 089 * Two {@code Pair}s are equal if and only if their first and second objects are 090 * both respectively equal. The first (or second) objects of two {@code Pair}s are 091 * equal if they are both {@code null} or if they are equal in respect to their 092 * respective {@code equals} methods. 093 * 094 * @param p A {@code Pair} to which the equality must be veryfied 095 * 096 * @return {@code true} if this {@code Pair} is equal to the specified {@code Pair}, 097 * {@code false} otherwise. 098 */ 099 public boolean equals(Pair<?, ?> p) { 100 101 if (null == p) 102 return false; 103 104 boolean e1 = (null == this.elem1 ? null == p.elem1 : this.elem1.equals(p.elem1)); 105 boolean e2 = (null == this.elem2 ? null == p.elem2 : this.elem2.equals(p.elem2)); 106 return e1 && e2; 107 } 108 109 } // public class Pair<T1, T2>