]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbolcontribution/AdaptablePair.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbolcontribution / AdaptablePair.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.symbolcontribution;
13
14 import org.eclipse.core.runtime.IAdaptable;
15
16 /**
17  * A basic pair structure that implements IAdaptable. It tries to adapt the
18  * paired objects to the requested class in order (first, second).
19  * 
20  * @author Tuukka Lehtonen
21  * 
22  * @param <T1> type of first
23  * @param <T2> type of second
24  */
25 final class AdaptablePair<T1, T2> implements IAdaptable {
26     public final T1 first;
27     public final T2 second;
28     private final int hash;
29
30     public static <T1, T2> AdaptablePair<T1, T2> make(T1 t1, T2 t2) {
31         return new AdaptablePair<T1, T2>(t1, t2);
32     }
33
34     public AdaptablePair(T1 first, T2 second) {
35         this.first = first;
36         this.second = second;
37         this.hash = makeHash();
38     }
39
40     @Override
41     public boolean equals(Object obj) {
42         if (obj == null)
43             return false;
44         if (!(obj.getClass().equals(this.getClass())))
45             return false;
46         AdaptablePair<?, ?> other = (AdaptablePair<?, ?>) obj;
47         if (other.first != first && (other.first == null || !other.first.equals(first)))
48             return false;
49         if (other.second != second && (other.second == null || !other.second.equals(second)))
50             return false;
51         return true;
52     }
53
54     @Override
55     public int hashCode() {
56         return hash;
57     }
58
59     @Override
60     public String toString() {
61         return "<"+first+", "+second+">";
62     }
63
64     private int makeHash() {
65         return (first == null ? 0 : first.hashCode()) + (second == null ? 0 : second.hashCode())*31;
66     }
67
68     private Object adapt(Object o, Class<?> clazz) {
69         if (clazz.isInstance(o))
70             return o;
71         if (o instanceof IAdaptable)
72             return ((IAdaptable) o).getAdapter(clazz);
73         return null;
74     }
75
76     @SuppressWarnings("rawtypes")
77     @Override
78     public Object getAdapter(Class adapter) {
79         Object o = adapt(first, adapter);
80         return (o == null) ? adapt(second, adapter) : o;
81     }
82
83 }