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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.content;
\r
14 import java.util.Iterator;
\r
15 import java.util.NoSuchElementException;
\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
23 * @author Tuukka Lehtonen
\r
25 public final class EdgeResource /*extends OrderedPair<Resource>*/ implements Iterable<Resource>, Comparable<EdgeResource> {
\r
27 private final Resource first;
\r
28 private final Resource second;
\r
30 public EdgeResource(Resource first, Resource second) {
\r
32 this.second = second;
\r
35 public String toString(ReadGraph g) throws DatabaseException {
\r
36 return "(" + NameUtils.getSafeName(g, first()) + ", " + NameUtils.getSafeName(g, second()) + ")";
\r
39 public Resource first() {
\r
43 public Resource second() {
\r
48 public int hashCode() {
\r
49 return first.hashCode() + second.hashCode();
\r
53 public boolean equals(Object obj) {
\r
56 if (!(obj instanceof EdgeResource))
\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
64 public String toString() {
\r
65 //return "(" + first() + ", " + second() + ")";
\r
66 StringBuilder sb = new StringBuilder(32);
\r
68 sb.append(first.getResourceId());
\r
70 sb.append(second.getResourceId());
\r
72 return sb.toString();
\r
76 public Iterator<Resource> iterator() {
\r
77 return new Iterator<Resource>() {
\r
81 public boolean hasNext() {
\r
86 public Resource next() {
\r
89 case 1: return first();
\r
90 case 2: return second();
\r
92 throw new NoSuchElementException("element " + i + "is out of bounds, only two elements exist.");
\r
97 public void remove() {
\r
98 throw new UnsupportedOperationException();
\r
103 final private long id() {
\r
104 return first.getResourceId() + second.getResourceId();
\r
108 public int compareTo(EdgeResource arg0) {
\r
110 long i2 = arg0.id();
\r
111 if(i == i2) return 0;
\r
112 if(i < i2) return -1;
\r