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