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