View Javadoc

1   /*
2    * Copyright 2004-2005, 2007 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.composite.util;
17  
18  /***
19   * A pair of objects. This object is used for creating a key value in a Maps
20   * used for caching purposes. Note that the order of the two objects in the pair
21   * <em>is</em> important.  Also note that object1 and object2 may not be null.
22   * 
23   * @author Matt Sgarlata
24   * @since Mar 14, 2005
25   */
26  public class ObjectPair {
27  	
28  	private Object object1;
29  	private Object object2;
30  	
31  	public ObjectPair() { }
32  	
33  	public ObjectPair(Object object1, Object object2) {
34  		this.object1 = object1;
35  		this.object2 = object2;
36  	}
37  	
38  	public Object getObject1() {
39  		return object1;
40  	}
41  	public void setObject1(Object object1) {
42  		this.object1 = object1;
43  	}
44  	public Object getObject2() {
45  		return object2;
46  	}
47  	public void setObject2(Object object2) {
48  		this.object2 = object2;
49  	}
50  	
51  	public boolean equals(Object object) {
52  		if (object == this) {
53  			return true;
54  		}
55  		if (!(object instanceof ObjectPair)) {
56  			return false;
57  		}
58  		ObjectPair pair = (ObjectPair) object;
59  		return nullSafeEquals(object1, pair.object1)
60  				&& nullSafeEquals(object2, pair.object2);
61  	}
62  
63  	private boolean nullSafeEquals(Object o1, Object o2) {
64  		return o1 == o2 || o1 != null && o1.equals(o2);
65  	}
66  
67  	public int hashCode() {
68  		return (object1 == null ? 0 : object1.hashCode())
69  				+ (object2 == null ? 0 : object2.hashCode());
70  	}
71  
72  	public String toString() {
73  		return "(" + getObject1() + "," + getObject2() + ")";
74  	}
75  	
76  }