]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/symbolcontribution/IdentifiedObject.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / symbolcontribution / IdentifiedObject.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  * @author Tuukka Lehtonen
18  */
19 public abstract class IdentifiedObject implements IAdaptable, IIdentifiedObject {
20     Object identification;
21
22     public IdentifiedObject(Object identification) {
23         if (identification == null)
24             throw new NullPointerException("null identification");
25         this.identification = identification;
26     }
27
28     @Override
29     public Object getId() {
30         return identification;
31     }
32
33     @Override
34     public int hashCode() {
35         return identification.hashCode();
36     }
37
38     @Override
39     public boolean equals(Object obj) {
40         if (this == obj)
41             return true;
42         if (obj == null)
43             return false;
44         if (!(obj instanceof IdentifiedObject))
45             return false;
46         IdentifiedObject other = (IdentifiedObject) obj;
47         return identification.equals(other.identification);
48     }
49
50     @SuppressWarnings("unchecked")
51     protected <T> T adapt(Class<T> clazz) {
52         if (clazz.isInstance(identification))
53             return (T) identification;
54         if (identification instanceof IAdaptable)
55             return (T) ((IAdaptable) identification).getAdapter(clazz);
56         return null;
57     }
58
59     @SuppressWarnings("rawtypes")
60     @Override
61     public Object getAdapter(Class adapter) {
62         if (adapter.isInstance(identification))
63             return identification;
64         if (identification instanceof IAdaptable)
65             return ((IAdaptable) identification).getAdapter(adapter);
66         return null;
67     }
68
69     @Override
70     public String toString() {
71         return getClass().getSimpleName() + "[id=" + identification + "]";
72     }
73
74 }