]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/binaryPredicates/OrderedSetElementsPredicate.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / binaryPredicates / OrderedSetElementsPredicate.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.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Statement;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.common.utils.OrderedSetUtils;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.utils.datastructures.Pair;
26
27 public class OrderedSetElementsPredicate extends BinaryPredicate {
28
29         public static final OrderedSetElementsPredicate INSTANCE = 
30                 new OrderedSetElementsPredicate(); 
31         
32         @Override
33         public Collection<Resource> getObjects(ReadGraph g, Resource subject) throws DatabaseException {
34                 Layer0 l0 = Layer0.getInstance(g);
35                 if(g.isInstanceOf(subject, l0.OrderedSet))
36                         return OrderedSetUtils.toList(g, subject);
37                 else
38                         return Collections.emptyList();
39         }
40
41         @Override
42         public Collection<Pair<Resource, Resource>> getStatements(ReadGraph g) {
43                 throw new UnsupportedOperationException();
44         }
45
46         @Override
47         public Collection<Resource> getSubjects(ReadGraph g, Resource object) throws DatabaseException {
48                 Collection<Resource> result = new ArrayList<Resource>(1);
49                 Layer0 l0 = Layer0.getInstance(g);
50                 for(Statement stat : g.getStatements(object, l0.IsWeaklyRelatedTo)) {
51                         Resource pred = stat.getPredicate();
52                         if(g.isInstanceOf(pred, l0.OrderedSet) && !pred.equals(object))
53                                 result.add(pred);
54                 }
55                 return result;
56         }
57
58         @Override
59         public boolean has(ReadGraph g, Resource subject, Resource object) throws DatabaseException {
60                 return OrderedSetUtils.contains(g, subject, object) &&
61                         g.isInstanceOf(subject, Layer0.getInstance(g).OrderedSet);
62         }
63
64         @Override
65         public boolean supportsGetObjects() {
66                 return true;
67         }
68
69         @Override
70         public boolean supportsGetStatements() {                
71                 return false;
72         }
73
74         @Override
75         public boolean supportsGetSubjects() {
76                 return true;
77         }
78
79         @Override
80         public void add(WriteGraph g, Resource subject, Resource object) throws DatabaseException {
81                 OrderedSetUtils.add(g, subject, object);
82         }
83
84         @Override
85         public void remove(WriteGraph g, Resource subject, Resource object) throws DatabaseException {
86                 OrderedSetUtils.remove(g, subject, object);
87         }
88
89         @Override
90         public boolean supportsAdditions() {
91                 return true;
92         }
93
94         @Override
95         public boolean supportsRemovals() {
96                 return true;
97         }
98         
99 }