]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/symbolcontribution/IdentifiedObject.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/symbolcontribution/IdentifiedObject.java
new file mode 100644 (file)
index 0000000..f098f48
--- /dev/null
@@ -0,0 +1,74 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.symbolcontribution;\r
+\r
+import org.eclipse.core.runtime.IAdaptable;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public abstract class IdentifiedObject implements IAdaptable, IIdentifiedObject {\r
+    Object identification;\r
+\r
+    public IdentifiedObject(Object identification) {\r
+        if (identification == null)\r
+            throw new NullPointerException("null identification");\r
+        this.identification = identification;\r
+    }\r
+\r
+    @Override\r
+    public Object getId() {\r
+        return identification;\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return identification.hashCode();\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if (this == obj)\r
+            return true;\r
+        if (obj == null)\r
+            return false;\r
+        if (!(obj instanceof IdentifiedObject))\r
+            return false;\r
+        IdentifiedObject other = (IdentifiedObject) obj;\r
+        return identification.equals(other.identification);\r
+    }\r
+\r
+    @SuppressWarnings("unchecked")\r
+    protected <T> T adapt(Class<T> clazz) {\r
+        if (clazz.isInstance(identification))\r
+            return (T) identification;\r
+        if (identification instanceof IAdaptable)\r
+            return (T) ((IAdaptable) identification).getAdapter(clazz);\r
+        return null;\r
+    }\r
+\r
+    @SuppressWarnings("rawtypes")\r
+    @Override\r
+    public Object getAdapter(Class adapter) {\r
+        if (adapter.isInstance(identification))\r
+            return identification;\r
+        if (identification instanceof IAdaptable)\r
+            return ((IAdaptable) identification).getAdapter(adapter);\r
+        return null;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return getClass().getSimpleName() + "[id=" + identification + "]";\r
+    }\r
+\r
+}\r