]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/binaryPredicates/Relation.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / binaryPredicates / Relation.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.layer0.utils.binaryPredicates;
13
14 import java.util.Collection;
15
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.utils.NameUtils;
20 import org.simantics.db.exception.DatabaseException;
21 import org.simantics.utils.datastructures.Pair;
22
23 public class Relation extends BinaryPredicate {
24
25         Resource relation;
26         
27         public Relation(Resource relation) {
28                 this.relation = relation;
29         }
30
31         @Override
32         public Collection<Resource> getObjects(ReadGraph g, Resource subject) throws DatabaseException {
33                 return g.getObjects(subject, relation);
34         }
35
36         @Override
37         public Collection<Pair<Resource, Resource>> getStatements(ReadGraph g) {
38                 throw new UnsupportedOperationException();
39         }
40
41         @Override
42         public Collection<Resource> getSubjects(ReadGraph g, Resource object) throws DatabaseException {
43                 return g.getObjects(object, g.getInverse(relation));
44         }
45
46         @Override
47         public boolean has(ReadGraph g, Resource subject, Resource object) throws DatabaseException {
48                 return g.hasStatement(subject, relation, object);
49         }
50
51         @Override
52         public boolean supportsGetObjects() {
53                 return true;
54         }
55
56         @Override
57         public boolean supportsGetStatements() {
58                 return false;
59         }
60
61         @Override
62         public boolean supportsGetSubjects() {
63                 return true;
64         }
65
66         @Override
67         public void add(WriteGraph g, Resource subject, Resource object) throws DatabaseException {
68                 g.claim(subject, relation, object);
69         }
70
71         @Override
72         public void remove(WriteGraph g, Resource subject, Resource object) throws DatabaseException {
73                 g.denyStatement(subject, relation, object);
74         }
75
76         @Override
77         public boolean supportsAdditions() {
78                 return true;
79         }
80
81         @Override
82         public boolean supportsRemovals() {
83                 return false;
84         }
85
86         @Override
87         public int hashCode() {
88                 final int prime = 31;
89                 int result = 1;
90                 result = prime * result
91                                 + ((relation == null) ? 0 : relation.hashCode());
92                 return result;
93         }
94
95         @Override
96         public boolean equals(Object obj) {
97                 if (this == obj)
98                         return true;
99                 if (obj == null)
100                         return false;
101                 if (getClass() != obj.getClass())
102                         return false;
103                 Relation other = (Relation) obj;
104                 if (relation == null) {
105                         if (other.relation != null)
106                                 return false;
107                 } else if (!relation.equals(other.relation))
108                         return false;
109                 return true;
110         }
111         
112         @Override
113         public IBinaryPredicate inverse(ReadGraph g) throws DatabaseException {
114                 return new Relation(g.getInverse(relation));
115         }
116         
117         @Override
118         public String toString(ReadGraph g) throws DatabaseException {
119                 return NameUtils.getSafeName(g, relation);
120         }
121         
122 }