Source code: com/virtuosotechnologies/asaph/model/opsemantics/OrPredicateSemantics.java
1 /*
2 ================================================================================
3
4 FILE: OrPredicateSemantics.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 A PredicateSemantics that runs all children and sets values to true
13 if at least one child set to true.
14
15 PROGRAMMERS:
16
17 Daniel Azuma (DA) <dazuma@kagi.com>
18
19 COPYRIGHT:
20
21 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
22
23 This program is free software; you can redistribute it and/or
24 modify it under the terms of the GNU General Public License as
25 published by the Free Software Foundation; either version 2
26 of the License, or (at your option) any later version.
27
28 This program is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
32
33 You should have received a copy of the GNU General Public
34 License along with this program; if not, write to
35 Free Software Foundation, Inc.
36 59 Temple Place, Suite 330
37 Boston, MA 02111-1307 USA
38
39 ================================================================================
40 */
41
42
43 package com.virtuosotechnologies.asaph.model.opsemantics;
44
45
46 import java.util.List;
47 import java.util.ArrayList;
48 import java.util.Iterator;
49 import java.util.Collections;
50
51 import com.virtuosotechnologies.asaph.model.SongDatabase;
52 import com.virtuosotechnologies.asaph.model.SongIDResultSet;
53 import com.virtuosotechnologies.asaph.model.SongDatabaseFailedException;
54 import com.virtuosotechnologies.asaph.model.SongID;
55
56
57 /**
58 * A PredicateSemantics that runs all children and sets values to true
59 * if at least one child set to true.
60 */
61 public interface OrPredicateSemantics
62 extends PredicateSemantics
63 {
64 /**
65 * Get the immutable collection of child predicates.
66 *
67 * @return Child predicates as an immutable List
68 */
69 public List getChildren();
70
71
72 /**
73 * The default implementation of OrPredicateSemantics
74 */
75 public static class DefaultImplementation
76 implements OrPredicateSemantics
77 {
78 private List children_;
79
80
81 /**
82 * Constructor
83 *
84 * @param children iterator over children to add
85 */
86 public DefaultImplementation(
87 Iterator children)
88 {
89 children_ = new ArrayList();
90 while (children.hasNext())
91 {
92 PredicateSemantics predicate = (PredicateSemantics)children.next();
93 children_.add(predicate);
94 }
95 }
96
97
98 /**
99 * Constructor
100 *
101 * @param child1 a child
102 * @param child2 a child
103 */
104 public DefaultImplementation(
105 PredicateSemantics child1,
106 PredicateSemantics child2)
107 {
108 children_ = new ArrayList(2);
109 children_.add(child1);
110 children_.add(child2);
111 }
112
113
114 /**
115 * Constructor
116 *
117 * @param children an array of child predicates
118 */
119 public DefaultImplementation(
120 PredicateSemantics[] children)
121 {
122 children_ = new ArrayList(children.length);
123 for (int i=0; i<children.length; ++i)
124 {
125 children_.add(children[i]);
126 }
127 }
128
129
130 /**
131 * Get the immutable collection of child predicates.
132 *
133 * @return Child predicates as an immutable List
134 */
135 public List getChildren()
136 {
137 return Collections.unmodifiableList(children_);
138 }
139
140
141 /**
142 * Performs the operation on the given result set.
143 *
144 * @param resultSet the SongIDResultSet
145 * @exception SongDatabaseFailedException Catch-all exception for database-related
146 * problems. This will often have a cause exception, which may be exceptions
147 * like IOException or SQLException.
148 */
149 public void perform(
150 SongIDResultSet resultSet)
151 throws
152 SongDatabaseFailedException
153 {
154 SongDatabase database = resultSet.getDatabase();
155 SongIDResultSet[] copyArray = new SongIDResultSet[children_.size()];
156 int size = 0;
157 for (Iterator iter = children_.iterator(); iter.hasNext(); )
158 {
159 PredicateSemantics predicate = (PredicateSemantics)iter.next();
160 copyArray[size] = resultSet.createLinkedCopy();
161 database.performOperation(predicate, copyArray[size]);
162 ++size;
163 }
164 for (Iterator iter = resultSet.getEntryCollection().iterator(); iter.hasNext(); )
165 {
166 SongIDResultSet.Entry resultEntry = (SongIDResultSet.Entry)iter.next();
167 SongID id = resultEntry.getSongID();
168 boolean result = false;
169 for (int i=0; i<size; ++i)
170 {
171 result |= Boolean.TRUE.equals(copyArray[i].getEntryFor(id).getData());
172 }
173 resultEntry.setData(result ? Boolean.TRUE : Boolean.FALSE);
174 }
175 }
176
177
178 /**
179 * The equals method should return true if the given object is a SongOperation
180 * with the same semantics as this one. (i.e. it would perform the same operation.)
181 * This may be used to optimize performance. It is always safe to return false from
182 * this method, if the semantics of the given object cannot be determined.
183 * (This is similar to the equals method in java.util.Comparator.)
184 * As a corollary, it is safe just to fall back on the default implementation
185 * inherited from java.lang.Object.
186 *
187 * @param obj object to test
188 * @return true if the object is equal
189 */
190 public boolean equals(
191 Object obj)
192 {
193 if (obj instanceof OrPredicateSemantics)
194 {
195 return ((OrPredicateSemantics)obj).getChildren().equals(children_);
196 }
197 return false;
198 }
199 }
200 }