1 /*
2  * JasperReports - Free Java Reporting Library.
3  * Copyright (C) 2001 - 2019 TIBCO Software Inc. All rights reserved.
4  * http://www.jaspersoft.com
5  *
6  * Unless you have purchased a commercial license agreement from Jaspersoft,
7  * the following license terms apply:
8  *
9  * This program is part of JasperReports.
10  *
11  * JasperReports is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * JasperReports is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with JasperReports. If not, see <http://www.gnu.org/licenses/>.
23  */

24 package net.sf.jasperreports.engine.util;
25
26 import java.io.Serializable;
27
28
29 /**
30  * Utility class used to pair two objects.
31  * 
32  * @author Lucian Chirita (lucianc@users.sourceforge.net)
33  */

34 //FIXME use generics everywhere
35 public class Pair<T, U> implements Serializable
36 {
37     private static final long serialVersionUID = 1; //too late to replace this now
38     
39     private final T o1;
40     private final U o2;
41     private final int hash;
42
43     
44     /**
45      * Create a pair instance.
46      * 
47      * @param o1 the first member of the pair
48      * @param o2 the second member of the pair
49      */

50     public Pair(T o1, U o2)
51     {
52         this.o1 = o1;
53         this.o2 = o2;
54         this.hash = computeHash();
55     }
56     
57     public T first()
58     {
59         return o1;
60     }
61     
62     public U second()
63     {
64         return o2;
65     }
66
67     private int computeHash()
68     {
69         int hashCode = o1 == null ? 0 : o1.hashCode();
70         hashCode *= 31;
71         hashCode += o2 == null ? 0 : o2.hashCode();
72         return hashCode;
73     }
74
75     @Override
76     public boolean equals(Object o)
77     {
78         if (o == this)
79         {
80             return true;
81         }
82         
83         if (o == null || !(o instanceof Pair))
84         {
85             return false;
86         }
87         
88         Pair<?, ?> p = (Pair<?, ?>) o;
89         
90         return (p.o1 == null ? o1 == null : (o1 != null && p.o1.equals(o1))) &&
91             (p.o2 == null ? o2 == null : (o2 != null && p.o2.equals(o2)));
92     }
93
94     @Override
95     public int hashCode()
96     {
97         return hash;
98     }
99     
100     @Override
101     public String toString()
102     {
103         return "(" + String.valueOf(o1) + ", " + String.valueOf(o2) + ")";
104     }
105
106 }
107