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