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