]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/ConnectionImpl.java
Variable optimizations for documents (Simupedia)
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / ConnectionImpl.java
1 /*******************************************************************************
2  * Copyright (c) 2018 Association for Decentralized Information Management in
3  * 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  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.structural2;
13
14 import java.util.Collection;
15 import java.util.Set;
16
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.variable.Variable;
21 import org.simantics.structural2.variables.Connection;
22 import org.simantics.structural2.variables.Connection2;
23 import org.simantics.structural2.variables.ConnectionBrowser;
24 import org.simantics.structural2.variables.VariableConnectionPointDescriptor;
25
26 import gnu.trove.set.hash.THashSet;
27
28 /**
29  * @author Antti Villberg
30  * @since 1.36.0
31  */
32 public class ConnectionImpl implements Connection {
33
34     private final Variable component;
35     private final Resource predicate;
36
37     public ConnectionImpl(Variable component, Resource predicate) {
38         this.component = component;
39         this.predicate = predicate;
40     }
41
42     @Override
43     public Collection<Variable> getConnectionPoints(ReadGraph graph, Resource relationType) throws DatabaseException {
44         Set<Variable> result = new THashSet<Variable>();
45         for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, predicate, relationType)) {
46             result.add(desc.getVariable(graph));
47         }
48         return result;
49     }
50
51     @Override
52     public Collection<String> getConnectionPointURIs(ReadGraph graph, Resource relationType) throws DatabaseException {
53         Set<String> result = new THashSet<String>();
54         for(VariableConnectionPointDescriptor desc : ConnectionBrowser.flatten(graph, component, predicate, relationType)) {
55             result.add(desc.getURI(graph));
56         }
57         return result;
58     }
59
60     @Override
61     public Collection<VariableConnectionPointDescriptor> getConnectionPointDescriptors(ReadGraph graph,
62             Resource relationType) throws DatabaseException {
63         return ConnectionBrowser.flatten(graph, component, predicate, relationType);
64     }
65
66     @Override
67     public Connection2 getConnection2() {
68         return new ConnectionImpl2(predicate);
69     }
70
71     @Override
72     public int hashCode() {
73         final int prime = 31;
74         int result = 1;
75         result = prime * result + ((component == null) ? 0 : component.hashCode());
76         result = prime * result + ((predicate == null) ? 0 : predicate.hashCode());
77         return result;
78     }
79
80     @Override
81     public boolean equals(Object obj) {
82         if (this == obj)
83             return true;
84         if (obj == null)
85             return false;
86         if (getClass() != obj.getClass())
87             return false;
88         ConnectionImpl other = (ConnectionImpl) obj;
89         if (component == null) {
90             if (other.component != null)
91                 return false;
92         } else if (!component.equals(other.component))
93             return false;
94         if (predicate == null) {
95             if (other.predicate != null)
96                 return false;
97         } else if (!predicate.equals(other.predicate))
98             return false;
99         return true;
100     }
101
102 }