]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.g2d/src/org/simantics/g2d/connection/TerminalKeyOf.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / connection / TerminalKeyOf.java
diff --git a/bundles/org.simantics.g2d/src/org/simantics/g2d/connection/TerminalKeyOf.java b/bundles/org.simantics.g2d/src/org/simantics/g2d/connection/TerminalKeyOf.java
new file mode 100644 (file)
index 0000000..8a2d7bf
--- /dev/null
@@ -0,0 +1,88 @@
+/*******************************************************************************\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.g2d.connection;\r
+\r
+import org.simantics.g2d.diagram.handler.Topology.Terminal;\r
+import org.simantics.g2d.element.handler.EdgeVisuals.EdgeEnd;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+\r
+/**\r
+ * This hint key is used for representing a terminal within an element that is\r
+ * connected to an edge. The key is identified by two things: the connected\r
+ * terminal and a secondary data object that can be used to make the key unique\r
+ * for an edge in cases where several connections to the same terminal are\r
+ * possible. A good option is to use the back-end object of the edge element\r
+ * itself, which naturally makes the key unique for that particular edge.\r
+ * \r
+ * <p>\r
+ * This key has a counterpart, {@link EndKeyOf} that is used in edge elements to\r
+ * connect both the {@link EdgeEnd#Begin} and {@link EdgeEnd#End} ends of the\r
+ * edge to element terminals. To actually represent the connection, you should\r
+ * give the same value object for this hint pair.\r
+ * </p>\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class TerminalKeyOf extends KeyOf {\r
+\r
+    private final Terminal terminal;\r
+    private final Object   data;\r
+    private int            hash;\r
+\r
+    /**\r
+     * @param t the terminal this connector hint represents\r
+     * @param data a secondary data object for making the key unique for a\r
+     *        single edge connected to the specified terminal. Use a\r
+     *        <code>null</code> value only if you consider the terminal to allow\r
+     *        only one connected edge.\r
+     * @param clazz the allowed class of the value object.\r
+     */\r
+    public TerminalKeyOf(Terminal t, Object data, Class<?> clazz) {\r
+        super(clazz);\r
+        this.terminal = t;\r
+        this.data = data;\r
+        this.hash = t.hashCode() ^ clazz.hashCode();\r
+        if (data != null)\r
+            this.hash = this.hash * 31 + data.hashCode();\r
+    }\r
+\r
+    public Terminal getTerminal() {\r
+        return terminal;\r
+    }\r
+\r
+    public Object getData() {\r
+        return data;\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return hash;\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if (this == obj)\r
+            return true;\r
+        if (!(obj instanceof TerminalKeyOf))\r
+            return false;\r
+        TerminalKeyOf other = (TerminalKeyOf) obj;\r
+        if (!terminal.equals(other.terminal))\r
+            return false;\r
+        return (data == null) ? other.data == null : data.equals(other.data);\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return getClass().getSimpleName() + "[terminal=" + terminal + ", data=" + data + "]";\r
+    }\r
+\r
+}
\ No newline at end of file