]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/BaseRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / adapter / BaseRequest.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/BaseRequest.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/adapter/BaseRequest.java
new file mode 100644 (file)
index 0000000..ea3ad0f
--- /dev/null
@@ -0,0 +1,66 @@
+/*******************************************************************************\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.adapter;\r
+\r
+import org.simantics.db.request.Read;\r
+import org.simantics.g2d.canvas.ICanvasContext;\r
+\r
+/**\r
+ * A base for synchronous requests performed by\r
+ * {@link GraphToDiagramSynchronizer}.\r
+ * \r
+ * <p>\r
+ * Takes care of making this request unique for this\r
+ * {@link GraphToDiagramSynchronizer} instances by making the active\r
+ * {@link ICanvasContext} a part of the requests identity.\r
+ * </p>\r
+ * \r
+ * @param <T> type of stored data element\r
+ * @param <Result> query result type\r
+ */\r
+abstract class BaseRequest<T, Result> implements Read<Result> {\r
+\r
+    final protected T    data;\r
+\r
+    final int            hash;\r
+\r
+    final ICanvasContext canvas;\r
+\r
+    public BaseRequest(ICanvasContext canvas, T data) {\r
+        this.canvas = canvas;\r
+        assert canvas != null;\r
+        assert data != null;\r
+        this.data = data;\r
+        this.hash = data.hashCode() + 31 * canvas.hashCode();\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return hash;\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object other) {\r
+        if (this == other)\r
+            return true;\r
+        if (other == null || getClass() != other.getClass())\r
+            return false;\r
+        BaseRequest<?, ?> o = (BaseRequest<?, ?>) other;\r
+        return data.equals(o.data) && canvas.equals(o.canvas);\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return getClass().getSimpleName() + "[" + data + " - " + canvas.hashCode() + "]";\r
+    }\r
+\r
+}
\ No newline at end of file