001/* Copyright (C) 2013 TU Dortmund 002 * This file is part of AutomataLib, http://www.automatalib.net/. 003 * 004 * AutomataLib is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License version 3.0 as published by the Free Software Foundation. 007 * 008 * AutomataLib is distributed in the hope that it will be useful, 009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 011 * Lesser General Public License for more details. 012 * 013 * You should have received a copy of the GNU Lesser General Public 014 * License along with AutomataLib; if not, see 015 * http://www.gnu.de/documents/lgpl.en.html. 016 */ 017package net.automatalib.commons.util; 018 019import java.io.IOException; 020import java.io.Serializable; 021 022import net.automatalib.commons.util.strings.AbstractPrintable; 023import net.automatalib.commons.util.strings.StringUtil; 024 025/** 026 * Mutable pair class. 027 * 028 * @author Malte Isberner <malte.isberner@gmail.com> 029 * 030 * @param <T1> type of the pair's first component. 031 * @param <T2> type of the pair's second component. 032 */ 033@Deprecated 034public class Pair<T1, T2> extends AbstractPrintable implements Serializable { 035 private static final long serialVersionUID = -1L; 036 037 /* 038 * Components 039 */ 040 protected T1 first; 041 protected T2 second; 042 043 /** 044 * Constructs a pair with the given members. 045 * 046 * @param first first member. 047 * @param second second member. 048 */ 049 public Pair(T1 first, T2 second) { 050 this.first = first; 051 this.second = second; 052 } 053 054 /** 055 * Getter for the first member. 056 * @return the first member. 057 */ 058 public T1 getFirst() { return first; } 059 060 /** 061 * Getter for the second member. 062 * @return the second member. 063 */ 064 public T2 getSecond() { return second; } 065 066 /** 067 * Setter for the first member. 068 * @param first the new value for the first member. 069 */ 070 public void setFirst(T1 first) { this.first = first; } 071 072 /** 073 * Setter for the second member. 074 * @param second the new value for the second member. 075 */ 076 public void setSecond(T2 second) { this.second = second; } 077 078 /** 079 * Convenience function for creating a pair, allowing the user to omit 080 * the type parameters. 081 * 082 * @see #Pair(Object, Object) 083 */ 084 public static <T1,T2> Pair<T1,T2> make(T1 first, T2 second) { 085 return new Pair<T1,T2>(first, second); 086 } 087 088 089 090 @Override 091 public int hashCode() { 092 final int prime = 31; 093 int result = 1; 094 result = prime * result + ((first == null) ? 0 : first.hashCode()); 095 result = prime * result + ((second == null) ? 0 : second.hashCode()); 096 return result; 097 } 098 099 @Override 100 public boolean equals(Object obj) { 101 if (this == obj) 102 return true; 103 if (obj == null) 104 return false; 105 if (getClass() != obj.getClass()) 106 return false; 107 Pair<?,?> other = (Pair<?,?>) obj; 108 if (first == null) { 109 if (other.first != null) 110 return false; 111 } else if (!first.equals(other.first)) 112 return false; 113 if (second == null) { 114 if (other.second != null) 115 return false; 116 } else if (!second.equals(other.second)) 117 return false; 118 return true; 119 } 120 121 122 /* 123 * (non-Javadoc) 124 * @see de.ls5.util.Printable#print(java.lang.Appendable) 125 */ 126 @Override 127 public void print(Appendable a) throws IOException { 128 StringUtil.appendObject(a, first); 129 a.append(", "); 130 StringUtil.appendObject(a, second); 131 } 132}