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;
020
021import net.automatalib.commons.util.strings.AbstractPrintable;
022import net.automatalib.commons.util.strings.StringUtil;
023
024public class Triple<T1, T2, T3> extends AbstractPrintable {
025        
026        protected T1 first;
027        protected T2 second;
028        protected T3 third;
029        
030        public Triple() {
031                
032        }
033        
034        public Triple(T1 first, T2 second, T3 third) {
035                this.first = first;
036                this.second = second;
037                this.third = third;
038        }
039        
040        
041        
042        
043        public T1 getFirst() {
044                return first;
045        }
046
047        public void setFirst(T1 first) {
048                this.first = first;
049        }
050
051        public T2 getSecond() {
052                return second;
053        }
054
055        public void setSecond(T2 second) {
056                this.second = second;
057        }
058
059        public T3 getThird() {
060                return third;
061        }
062
063        public void setThird(T3 third) {
064                this.third = third;
065        }
066
067        @Override
068        public void print(Appendable a) throws IOException {
069                StringUtil.appendObject(a, first);
070                a.append(", ");
071                StringUtil.appendObject(a, second);
072                a.append(", ");
073                StringUtil.appendObject(a, third);
074        }
075        
076        
077        
078        
079        @Override
080        public int hashCode() {
081                final int prime = 31;
082                int result = 1;
083                result = prime * result + ((first == null) ? 0 : first.hashCode());
084                result = prime * result + ((second == null) ? 0 : second.hashCode());
085                result = prime * result + ((third == null) ? 0 : third.hashCode());
086                return result;
087        }
088
089        @Override
090        public boolean equals(Object obj) {
091                if (this == obj)
092                        return true;
093                if (obj == null)
094                        return false;
095                if (getClass() != obj.getClass())
096                        return false;
097                Triple<?,?,?> other = (Triple<?,?,?>) obj;
098                if (first == null) {
099                        if (other.first != null)
100                                return false;
101                } else if (!first.equals(other.first))
102                        return false;
103                if (second == null) {
104                        if (other.second != null)
105                                return false;
106                } else if (!second.equals(other.second))
107                        return false;
108                if (third == null) {
109                        if (other.third != null)
110                                return false;
111                } else if (!third.equals(other.third))
112                        return false;
113                return true;
114        }
115
116        public Pair<T1,Pair<T2,T3>> asPair1() {
117                return Pair.make(first, Pair.make(second, third));
118        }
119        
120        public Pair<Pair<T1,T2>,T3> asPair2() {
121                return Pair.make(Pair.make(first, second), third);
122        }
123        
124        
125        
126        public static <T1,T2,T3> Triple<T1,T2,T3> make(T1 first, T2 second, T3 third) {
127                return new Triple<T1,T2,T3>(first, second, third);
128        }
129        
130        public static <T1,T2,T3> Triple<T1,T2,T3> fromPair1(Pair<T1,Pair<T2,T3>> pair) {
131                T1 first = null;
132                Pair<T2,T3> sndPair = null;
133                if(pair != null) {
134                        first = pair.getFirst();
135                        sndPair = pair.getSecond();
136                }
137                T2 second = null;
138                T3 third = null;
139                if(sndPair != null) {
140                        second = sndPair.getFirst();
141                        third = sndPair.getSecond();
142                }
143                
144                return make(first, second, third);
145        }
146        
147        public static <T1,T2,T3> Triple<T1,T2,T3> fromPair2(Pair<Pair<T1,T2>,T3> pair) {
148                Pair<T1,T2> fstPair = null;
149                T3 third = null;
150                if(pair != null) {
151                        fstPair = pair.getFirst();
152                        third = pair.getSecond();
153                }
154                T1 first = null;
155                T2 second = null;
156                if(fstPair != null) {
157                        first = fstPair.getFirst();
158                        second = fstPair.getSecond();
159                }
160                
161                return make(first, second, third);
162        }
163}