]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/content/EdgeResource.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / content / EdgeResource.java
diff --git a/bundles/org.simantics.diagram/src/org/simantics/diagram/content/EdgeResource.java b/bundles/org.simantics.diagram/src/org/simantics/diagram/content/EdgeResource.java
new file mode 100644 (file)
index 0000000..4ff53a7
--- /dev/null
@@ -0,0 +1,116 @@
+/*******************************************************************************\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.content;\r
+\r
+import java.util.Iterator;\r
+import java.util.NoSuchElementException;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public final class EdgeResource /*extends OrderedPair<Resource>*/ implements Iterable<Resource>, Comparable<EdgeResource> {\r
+\r
+    private final Resource first;\r
+    private final Resource second;\r
+\r
+    public EdgeResource(Resource first, Resource second) {\r
+        this.first = first;\r
+        this.second = second;\r
+    }\r
+\r
+    public String toString(ReadGraph g) throws DatabaseException {\r
+        return "(" + NameUtils.getSafeName(g, first()) + ", " + NameUtils.getSafeName(g, second()) + ")";\r
+    }\r
+\r
+    public Resource first() {\r
+        return first;\r
+    }\r
+\r
+    public Resource second() {\r
+        return second;\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return first.hashCode() + second.hashCode();\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if (this == obj)\r
+            return true;\r
+        if (!(obj instanceof EdgeResource))\r
+            return false;\r
+        EdgeResource other = (EdgeResource) obj;\r
+        return (first.equals(other.first) && second.equals(other.second)) || (first.equals(other.second) && second.equals(other.first));\r
+    }\r
+\r
+\r
+    @Override\r
+    public String toString() {\r
+        //return "(" + first() + ", " + second() + ")";\r
+        StringBuilder sb = new StringBuilder(32);\r
+        sb.append('(');\r
+        sb.append(first.getResourceId());\r
+        sb.append(',');\r
+        sb.append(second.getResourceId());\r
+        sb.append(')');\r
+        return sb.toString();\r
+    }\r
+\r
+    @Override\r
+    public Iterator<Resource> iterator() {\r
+        return new Iterator<Resource>() {\r
+            int i = 0;\r
+\r
+            @Override\r
+            public boolean hasNext() {\r
+                return i == 0;\r
+            }\r
+\r
+            @Override\r
+            public Resource next() {\r
+                ++i;\r
+                switch (i) {\r
+                    case 1: return first();\r
+                    case 2: return second();\r
+                    default:\r
+                        throw new NoSuchElementException("element " + i + "is out of bounds, only two elements exist.");\r
+                }\r
+            }\r
+\r
+            @Override\r
+            public void remove() {\r
+                throw new UnsupportedOperationException();\r
+            }\r
+        };\r
+    }\r
+\r
+    final private long id() {\r
+        return first.getResourceId() + second.getResourceId();\r
+    }\r
+\r
+    @Override\r
+    public int compareTo(EdgeResource arg0) {\r
+        long i = id();\r
+        long i2 = arg0.id();\r
+        if(i == i2) return 0;\r
+        if(i < i2) return -1;\r
+        else return 1;\r
+    }\r
+\r
+}
\ No newline at end of file