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