Source code: com/virtuosotechnologies/asaph/model/opsemantics/SearchBodyPredicateSemantics.java
1 /*
2 ================================================================================
3
4 FILE: SearchBodyPredicateSemantics.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 PredicateSemantics that searches the song body for text
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.asaph.model.opsemantics;
43
44
45 import java.util.Iterator;
46
47 import com.virtuosotechnologies.lib.base.UniqueObject;
48
49 import com.virtuosotechnologies.asaph.modelutils.SongUtils;
50 import com.virtuosotechnologies.asaph.model.SongIDResultSet;
51 import com.virtuosotechnologies.asaph.model.SongDatabaseFailedException;
52 import com.virtuosotechnologies.asaph.model.Song;
53
54
55 /**
56 * PredicateSemantics that searches the song body for text
57 */
58 public interface SearchBodyPredicateSemantics
59 extends PredicateSemantics
60 {
61 /**
62 * PartConstraint
63 */
64 public static final class PartConstraint
65 extends UniqueObject
66 {
67 /*package*/ PartConstraint(
68 String description)
69 {
70 super(description);
71 }
72 }
73
74
75 /**
76 * Search the entire body
77 */
78 public static final PartConstraint ENTIRE_BODY = new PartConstraint("entire_body");
79
80 /**
81 * Search the first line only
82 */
83 public static final PartConstraint FIRST_LINE = new PartConstraint("first_line");
84
85
86 /**
87 * Get the part constraint
88 */
89 public PartConstraint getPartConstraint();
90
91
92 /**
93 * Get the search parameters
94 */
95 public SearchParameters getSearchParameters();
96
97
98 /**
99 * The default implementation of TruePredicateSemantics
100 */
101 public static class DefaultImplementation
102 implements SearchBodyPredicateSemantics
103 {
104 private SongUtils songUtils_;
105 private PartConstraint part_;
106 private SearchParameters params_;
107
108
109 /**
110 * Constructor
111 */
112 public DefaultImplementation(
113 SongUtils songUtils,
114 PartConstraint part,
115 SearchParameters params)
116 {
117 songUtils_ = songUtils;
118 part_ = part;
119 params_ = params;
120 }
121
122
123 /**
124 * Get the part constraint
125 */
126 public PartConstraint getPartConstraint()
127 {
128 return part_;
129 }
130
131
132 /**
133 * Get the search parameters
134 */
135 public SearchParameters getSearchParameters()
136 {
137 return params_;
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 for (Iterator iter = resultSet.getEntryCollection().iterator(); iter.hasNext(); )
155 {
156 SongIDResultSet.Entry entry = (SongIDResultSet.Entry)iter.next();
157 Song song = entry.getSong();
158 String str = "";
159 if (part_ == ENTIRE_BODY)
160 {
161 str = songUtils_.getSongTextAsString(song, false, null);
162 }
163 else if (part_ == FIRST_LINE)
164 {
165 str = songUtils_.getFirstLineAsString(song, false, null);
166 }
167 if (params_.matches(str))
168 {
169 entry.setData(Boolean.TRUE);
170 }
171 else
172 {
173 entry.setData(Boolean.FALSE);
174 }
175 }
176 }
177
178
179 /**
180 * The equals method should return true if the given object is a SongOperation
181 * with the same semantics as this one. (i.e. it would perform the same operation.)
182 * This may be used to optimize performance. It is always safe to return false from
183 * this method, if the semantics of the given object cannot be determined.
184 * (This is similar to the equals method in java.util.Comparator.)
185 * As a corollary, it is safe just to fall back on the default implementation
186 * inherited from java.lang.Object.
187 *
188 * @param obj object to test
189 * @return true if the object is equal
190 */
191 public boolean equals(
192 Object obj)
193 {
194 if (obj instanceof SearchBodyPredicateSemantics)
195 {
196 SearchBodyPredicateSemantics other = (SearchBodyPredicateSemantics)obj;
197 return other.getPartConstraint().equals(part_) &&
198 other.getSearchParameters().equals(params_);
199 }
200 return false;
201 }
202 }
203 }