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