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