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