]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.common/src/org/simantics/db/common/request/Adapt.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / request / Adapt.java
diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/request/Adapt.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/request/Adapt.java
new file mode 100644 (file)
index 0000000..3ca5a73
--- /dev/null
@@ -0,0 +1,78 @@
+package org.simantics.db.common.request;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.AdaptionException;\r
+import org.simantics.db.exception.ServiceException;\r
+import org.simantics.db.exception.ValidationException;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ * \r
+ * @param <T> the adaption target interface\r
+ */\r
+public class Adapt<T> extends ResourceRead<T> {\r
+\r
+    final protected Class<T> target;\r
+    final protected boolean  allowNull;\r
+    final protected boolean  uniqueResult;\r
+\r
+    public Adapt(Resource resource, Class<T> target) {\r
+        this(resource, target, false, false);\r
+    }\r
+\r
+    public Adapt(Resource resource, Class<T> target, boolean allowNull) {\r
+        this(resource, target, allowNull, false);\r
+    }\r
+\r
+    public Adapt(Resource resource, Class<T> target, boolean allowNull, boolean uniqueResult) {\r
+        super(resource);\r
+        assert target != null;\r
+        this.target = target;\r
+        this.allowNull = allowNull;\r
+        this.uniqueResult = uniqueResult;\r
+    }\r
+\r
+    @Override\r
+    public T perform(ReadGraph graph) throws AdaptionException, ValidationException, ServiceException {\r
+        if (allowNull) {\r
+            if (uniqueResult)\r
+                return graph.getPossibleUniqueAdapter(resource, target);\r
+            return graph.getPossibleAdapter(resource, target);\r
+        }\r
+        if (uniqueResult)\r
+            return graph.adaptUnique(resource, target);\r
+        return graph.adapt(resource, target);\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        final int prime = 31;\r
+        int result = super.hashCode();\r
+        result = prime * result + (allowNull ? 1231 : 1237);\r
+        result = prime * result + (target.hashCode());\r
+        result = prime * result + (uniqueResult ? 1303 : 1319);\r
+        return result;\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if (obj == null)\r
+            return false;\r
+        if (this == obj)\r
+            return true;\r
+        if (getClass() != obj.getClass())\r
+            return false;\r
+        Adapt<?> other = (Adapt<?>) obj;\r
+        if (!resource.equals(other.resource))\r
+            return false;\r
+        if (allowNull != other.allowNull)\r
+            return false;\r
+        if (!target.equals(other.target))\r
+            return false;\r
+        if (uniqueResult != other.uniqueResult)\r
+            return false;\r
+        return true;\r
+    }\r
+\r
+}
\ No newline at end of file