]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbolcontribution/AdaptablePair.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbolcontribution / AdaptablePair.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.symbolcontribution;\r
13 \r
14 import org.eclipse.core.runtime.IAdaptable;\r
15 \r
16 /**\r
17  * A basic pair structure that implements IAdaptable. It tries to adapt the\r
18  * paired objects to the requested class in order (first, second).\r
19  * \r
20  * @author Tuukka Lehtonen\r
21  * \r
22  * @param <T1> type of first\r
23  * @param <T2> type of second\r
24  */\r
25 final class AdaptablePair<T1, T2> implements IAdaptable {\r
26     public final T1 first;\r
27     public final T2 second;\r
28     private final int hash;\r
29 \r
30     public static <T1, T2> AdaptablePair<T1, T2> make(T1 t1, T2 t2) {\r
31         return new AdaptablePair<T1, T2>(t1, t2);\r
32     }\r
33 \r
34     public AdaptablePair(T1 first, T2 second) {\r
35         this.first = first;\r
36         this.second = second;\r
37         this.hash = makeHash();\r
38     }\r
39 \r
40     @Override\r
41     public boolean equals(Object obj) {\r
42         if (obj == null)\r
43             return false;\r
44         if (!(obj.getClass().equals(this.getClass())))\r
45             return false;\r
46         AdaptablePair<?, ?> other = (AdaptablePair<?, ?>) obj;\r
47         if (other.first != first && (other.first == null || !other.first.equals(first)))\r
48             return false;\r
49         if (other.second != second && (other.second == null || !other.second.equals(second)))\r
50             return false;\r
51         return true;\r
52     }\r
53 \r
54     @Override\r
55     public int hashCode() {\r
56         return hash;\r
57     }\r
58 \r
59     @Override\r
60     public String toString() {\r
61         return "<"+first+", "+second+">";\r
62     }\r
63 \r
64     private int makeHash() {\r
65         return (first == null ? 0 : first.hashCode()) + (second == null ? 0 : second.hashCode())*31;\r
66     }\r
67 \r
68     private Object adapt(Object o, Class<?> clazz) {\r
69         if (clazz.isInstance(o))\r
70             return o;\r
71         if (o instanceof IAdaptable)\r
72             return ((IAdaptable) o).getAdapter(clazz);\r
73         return null;\r
74     }\r
75 \r
76     @SuppressWarnings("rawtypes")\r
77     @Override\r
78     public Object getAdapter(Class adapter) {\r
79         Object o = adapt(first, adapter);\r
80         return (o == null) ? adapt(second, adapter) : o;\r
81     }\r
82 \r
83 }\r