]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/predicates/Conjunction.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / predicates / Conjunction.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.predicates;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17
18 import org.simantics.db.Resource;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
22
23 public class Conjunction extends UnaryPredicate {
24
25         Collection<IUnaryPredicate> predicates;
26         IUnaryPredicate support;
27
28         public Conjunction(Collection<IUnaryPredicate> predicates) {
29                 for(IUnaryPredicate pred : predicates)
30                         if(pred.supportsUnboundedQuery()) {
31                                 this.support = pred;
32                                 this.predicates = new ArrayList<IUnaryPredicate>(predicates.size()-1);
33                                 for(IUnaryPredicate pred2 : predicates)
34                                         if(pred != pred2)
35                                                 this.predicates.add(pred2);
36                                 return;
37                         }
38                 this.predicates = predicates;
39                 this.support = null;
40         }
41         
42         public Conjunction(IUnaryPredicate ... predicates) {
43         this(Arrays.asList(predicates));
44     }
45
46         @Override
47         public Collection<Resource> getResources(ReadGraph g) throws DatabaseException {
48                 Collection<Resource> result = new ArrayList<Resource>();
49                 ll:     for(Resource r : support.getResources(g)) {                     
50                         for(IUnaryPredicate pred : predicates)
51                                 if(!pred.has(g, r))
52                                         continue ll;
53                         result.add(r);
54                 }
55                 return result;
56         }
57
58         @Override
59         public boolean has(ReadGraph g, Resource resource) throws DatabaseException {
60                 for(IUnaryPredicate pred : predicates)
61                         if(!pred.has(g, resource))
62                                 return false;
63                 return support == null || support.has(g, resource);
64         }
65
66         @Override
67         public boolean supportsUnboundedQuery() {               
68                 return support != null;
69         }       
70         
71         @Override
72         public void add(WriteGraph g, Resource r) {
73                 throw new UnsupportedOperationException();
74         }
75
76         @Override
77         public void remove(WriteGraph g, Resource r) {
78                 throw new UnsupportedOperationException();
79         }
80
81         @Override
82         public boolean supportsAddition() {
83                 return false;
84         }
85
86         @Override
87         public boolean supportsRemoval() {              
88                 return false;
89         }
90
91         @Override
92         public int hashCode() {
93                 final int prime = 31;
94                 int result = 1;
95                 result = prime * result
96                                 + ((predicates == null) ? 0 : predicates.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                 Conjunction other = (Conjunction) obj;
109                 if (predicates == null) {
110                         if (other.predicates != null)
111                                 return false;
112                 } else if (!predicates.equals(other.predicates))
113                         return false;
114                 return true;
115         }
116                 
117 }