]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.content;\r
13 \r
14 import java.util.Iterator;\r
15 import java.util.NoSuchElementException;\r
16 \r
17 import org.simantics.db.ReadGraph;\r
18 import org.simantics.db.Resource;\r
19 import org.simantics.db.common.utils.NameUtils;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 \r
22 /**\r
23  * @author Tuukka Lehtonen\r
24  */\r
25 public final class EdgeResource /*extends OrderedPair<Resource>*/ implements Iterable<Resource>, Comparable<EdgeResource> {\r
26 \r
27     private final Resource first;\r
28     private final Resource second;\r
29 \r
30     public EdgeResource(Resource first, Resource second) {\r
31         this.first = first;\r
32         this.second = second;\r
33     }\r
34 \r
35     public String toString(ReadGraph g) throws DatabaseException {\r
36         return "(" + NameUtils.getSafeName(g, first()) + ", " + NameUtils.getSafeName(g, second()) + ")";\r
37     }\r
38 \r
39     public Resource first() {\r
40         return first;\r
41     }\r
42 \r
43     public Resource second() {\r
44         return second;\r
45     }\r
46 \r
47     @Override\r
48     public int hashCode() {\r
49         return first.hashCode() + second.hashCode();\r
50     }\r
51 \r
52     @Override\r
53     public boolean equals(Object obj) {\r
54         if (this == obj)\r
55             return true;\r
56         if (!(obj instanceof EdgeResource))\r
57             return false;\r
58         EdgeResource other = (EdgeResource) obj;\r
59         return (first.equals(other.first) && second.equals(other.second)) || (first.equals(other.second) && second.equals(other.first));\r
60     }\r
61 \r
62 \r
63     @Override\r
64     public String toString() {\r
65         //return "(" + first() + ", " + second() + ")";\r
66         StringBuilder sb = new StringBuilder(32);\r
67         sb.append('(');\r
68         sb.append(first.getResourceId());\r
69         sb.append(',');\r
70         sb.append(second.getResourceId());\r
71         sb.append(')');\r
72         return sb.toString();\r
73     }\r
74 \r
75     @Override\r
76     public Iterator<Resource> iterator() {\r
77         return new Iterator<Resource>() {\r
78             int i = 0;\r
79 \r
80             @Override\r
81             public boolean hasNext() {\r
82                 return i == 0;\r
83             }\r
84 \r
85             @Override\r
86             public Resource next() {\r
87                 ++i;\r
88                 switch (i) {\r
89                     case 1: return first();\r
90                     case 2: return second();\r
91                     default:\r
92                         throw new NoSuchElementException("element " + i + "is out of bounds, only two elements exist.");\r
93                 }\r
94             }\r
95 \r
96             @Override\r
97             public void remove() {\r
98                 throw new UnsupportedOperationException();\r
99             }\r
100         };\r
101     }\r
102 \r
103     final private long id() {\r
104         return first.getResourceId() + second.getResourceId();\r
105     }\r
106 \r
107     @Override\r
108     public int compareTo(EdgeResource arg0) {\r
109         long i = id();\r
110         long i2 = arg0.id();\r
111         if(i == i2) return 0;\r
112         if(i < i2) return -1;\r
113         else return 1;\r
114     }\r
115 \r
116 }