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