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