]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbolcontribution/AdaptableTriple.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbolcontribution / AdaptableTriple.java
1 /*******************************************************************************\r
2  * Copyright (c) 2013 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  *     Semantum Oy - 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 triple structure that implements IAdaptable. It tries to adapt the\r
18  * objects to the requested class in order (first, second, third).\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  * @param <T3> type of third\r
25  */\r
26 final class AdaptableTriple<T1, T2, T3> implements IAdaptable {\r
27     public final T1 first;\r
28     public final T2 second;\r
29     public final T3 third;\r
30     private final int hash;\r
31 \r
32     public static <T1, T2, T3> AdaptableTriple<T1, T2, T3> make(T1 t1, T2 t2, T3 t3) {\r
33         return new AdaptableTriple<T1, T2, T3>(t1, t2, t3);\r
34     }\r
35 \r
36     public AdaptableTriple(T1 first, T2 second, T3 third) {\r
37         this.first = first;\r
38         this.second = second;\r
39         this.third = third;\r
40         this.hash = makeHash();\r
41     }\r
42 \r
43     @Override\r
44     public boolean equals(Object obj) {\r
45         if (obj == null)\r
46             return false;\r
47         if (!(obj.getClass().equals(this.getClass())))\r
48             return false;\r
49         AdaptableTriple<?, ?, ?> other = (AdaptableTriple<?, ?, ?>) obj;\r
50         if (other.first != first && (other.first == null || !other.first.equals(first)))\r
51             return false;\r
52         if (other.second != second && (other.second == null || !other.second.equals(second)))\r
53             return false;\r
54         if (other.third != third && (other.third == null || !other.third.equals(third)))\r
55             return false;\r
56         return true;\r
57     }\r
58 \r
59     @Override\r
60     public int hashCode() {\r
61         return hash;\r
62     }\r
63 \r
64     @Override\r
65     public String toString() {\r
66         return "<"+first+", "+second+", "+third+">";\r
67     }\r
68 \r
69     private int makeHash() {\r
70         return (first == null ? 0 : first.hashCode())\r
71                 + (second == null ? 0 : second.hashCode())*31\r
72                 + (third == null ? 0 : third.hashCode())*31*31;\r
73     }\r
74 \r
75     private Object adapt(Object o, Class<?> clazz) {\r
76         if (clazz.isInstance(o))\r
77             return o;\r
78         if (o instanceof IAdaptable)\r
79             return ((IAdaptable) o).getAdapter(clazz);\r
80         return null;\r
81     }\r
82 \r
83     @SuppressWarnings("rawtypes")\r
84     @Override\r
85     public Object getAdapter(Class adapter) {\r
86         Object o = adapt(first, adapter);\r
87         if (o == null)\r
88             o = adapt(second, adapter);\r
89         if (o == null)\r
90             o = adapt(third, adapter);\r
91         return o;\r
92     }\r
93 \r
94 }\r