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 */ 033public class Pair<T1, T2> extends AbstractPrintable implements Serializable { 034 private static final long serialVersionUID = -1L; 035 036 /* 037 * Components 038 */ 039 protected T1 first; 040 protected T2 second; 041 042 /** 043 * Constructs a pair with the given members. 044 * 045 * @param first first member. 046 * @param second second member. 047 */ 048 public Pair(T1 first, T2 second) { 049 this.first = first; 050 this.second = second; 051 } 052 053 /** 054 * Getter for the first member. 055 * @return the first member. 056 */ 057 public T1 getFirst() { return first; } 058 059 /** 060 * Getter for the second member. 061 * @return the second member. 062 */ 063 public T2 getSecond() { return second; } 064 065 /** 066 * Setter for the first member. 067 * @param first the new value for the first member. 068 */ 069 public void setFirst(T1 first) { this.first = first; } 070 071 /** 072 * Setter for the second member. 073 * @param second the new value for the second member. 074 */ 075 public void setSecond(T2 second) { this.second = second; } 076 077 /** 078 * Convenience function for creating a pair, allowing the user to omit 079 * the type parameters. 080 * 081 * @see #Pair(Object, Object) 082 */ 083 public static <T1,T2> Pair<T1,T2> make(T1 first, T2 second) { 084 return new Pair<T1,T2>(first, second); 085 } 086 087 088 089 @Override 090 public int hashCode() { 091 final int prime = 31; 092 int result = 1; 093 result = prime * result + ((first == null) ? 0 : first.hashCode()); 094 result = prime * result + ((second == null) ? 0 : second.hashCode()); 095 return result; 096 } 097 098 @Override 099 public boolean equals(Object obj) { 100 if (this == obj) 101 return true; 102 if (obj == null) 103 return false; 104 if (getClass() != obj.getClass()) 105 return false; 106 Pair<?,?> other = (Pair<?,?>) obj; 107 if (first == null) { 108 if (other.first != null) 109 return false; 110 } else if (!first.equals(other.first)) 111 return false; 112 if (second == null) { 113 if (other.second != null) 114 return false; 115 } else if (!second.equals(other.second)) 116 return false; 117 return true; 118 } 119 120 121 /* 122 * (non-Javadoc) 123 * @see de.ls5.util.Printable#print(java.lang.Appendable) 124 */ 125 @Override 126 public void print(Appendable a) throws IOException { 127 StringUtil.appendObject(a, first); 128 a.append(", "); 129 StringUtil.appendObject(a, second); 130 } 131}