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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.layer0.utils.binaryPredicates;
\r
14 import java.util.Arrays;
\r
15 import java.util.Collection;
\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
23 public class InversePredicate extends BinaryPredicate {
\r
25 IBinaryPredicate base;
\r
27 public InversePredicate(IBinaryPredicate base) {
\r
32 public Collection<Resource> getObjects(ReadGraph g, Resource subject) throws DatabaseException {
\r
33 return base.getSubjects(g, subject);
\r
36 @SuppressWarnings("unchecked")
\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
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
48 public Collection<Resource> getSubjects(ReadGraph g, Resource object) throws DatabaseException {
\r
49 return base.getObjects(g, object);
\r
53 public boolean has(ReadGraph g, Resource subject, Resource object) {
\r
54 return has(g, object, subject);
\r
58 public boolean supportsGetObjects() {
\r
59 return base.supportsGetSubjects();
\r
63 public boolean supportsGetStatements() {
\r
64 return base.supportsGetStatements();
\r
68 public boolean supportsGetSubjects() {
\r
69 return base.supportsGetObjects();
\r
73 public void add(WriteGraph g, Resource subject, Resource object) throws DatabaseException {
\r
74 base.add(g, object, subject);
\r
78 public void remove(WriteGraph g, Resource subject, Resource object) throws DatabaseException {
\r
79 base.remove(g, object, subject);
\r
83 public boolean supportsAdditions() {
\r
84 return base.supportsAdditions();
\r
88 public boolean supportsRemovals() {
\r
89 return base.supportsRemovals();
\r
93 public int hashCode() {
\r
94 final int prime = 31;
\r
96 result = prime * result + ((base == null) ? 0 : base.hashCode());
\r
101 public boolean equals(Object obj) {
\r
106 if (getClass() != obj.getClass())
\r
108 InversePredicate other = (InversePredicate) obj;
\r
109 if (base == null) {
\r
110 if (other.base != null)
\r
112 } else if (!base.equals(other.base))
\r
118 public IBinaryPredicate inverse(ReadGraph g) {
\r