Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/virtuosotechnologies/asaph/model/opsemantics/SearchFieldPredicateSemantics.java


1   /*
2   ================================================================================
3   
4     FILE:  SearchFieldPredicateSemantics.java
5     
6     PROJECT:
7     
8       Asaph
9     
10    CONTENTS:
11    
12      PredicateSemantics that searches a given field 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.asaph.model.SongIDResultSet;
48  import com.virtuosotechnologies.asaph.model.SongDatabaseFailedException;
49  import com.virtuosotechnologies.asaph.model.Song;
50  import com.virtuosotechnologies.asaph.model.Field;
51  import com.virtuosotechnologies.asaph.model.StringField;
52  import com.virtuosotechnologies.asaph.model.StringListField;
53  import com.virtuosotechnologies.asaph.model.SimpleString;
54  
55  
56  /**
57   * PredicateSemantics that searches a given field for text
58   */
59  public interface SearchFieldPredicateSemantics
60  extends PredicateSemantics
61  {
62    /**
63     * Get the name of the field to search
64     */
65    public String getFieldName();
66    
67    
68    /**
69     * Get the search parameters
70     */
71    public SearchParameters getSearchParameters();
72    
73    
74    /**
75     * The default implementation of TruePredicateSemantics
76     */
77    public static class DefaultImplementation
78    implements SearchFieldPredicateSemantics
79    {
80      private String fieldName_;
81      private SearchParameters params_;
82      
83      
84      /**
85       * Constructor
86       */
87      public DefaultImplementation(
88        String fieldName,
89        SearchParameters params)
90      {
91        fieldName_ = fieldName;
92        params_ = params;
93      }
94      
95      
96      /**
97       * Get the name of the field to search
98       */
99      public String getFieldName()
100     {
101       return fieldName_;
102     }
103     
104     
105     /**
106      * Get the search parameters
107      */
108     public SearchParameters getSearchParameters()
109     {
110       return params_;
111     }
112     
113     
114     /**
115      * Performs the operation on the given result set.
116      *
117      * @param resultSet the SongIDResultSet
118      * @exception SongDatabaseFailedException Catch-all exception for database-related
119      *     problems. This will often have a cause exception, which may be exceptions
120      *     like IOException or SQLException.
121      */
122     public void perform(
123       SongIDResultSet resultSet)
124     throws
125       SongDatabaseFailedException
126     {
127       for (Iterator iter = resultSet.getEntryCollection().iterator(); iter.hasNext(); )
128       {
129         SongIDResultSet.Entry entry = (SongIDResultSet.Entry)iter.next();
130         Song song = entry.getSong();
131         Field field = song.getNamedField(fieldName_);
132         if (field instanceof StringField)
133         {
134           StringField stringField = (StringField)field;
135           if (params_.matches(stringField.getString()))
136           {
137             entry.setData(Boolean.TRUE);
138           }
139           else
140           {
141             entry.setData(Boolean.FALSE);
142           }
143         }
144         else if (field instanceof StringListField)
145         {
146           Boolean result = Boolean.FALSE;
147           StringListField stringListField = (StringListField)field;
148           for (SimpleString str = stringListField.getNextString(null); str != null;
149             str = stringListField.getNextString(str))
150           {
151             if (params_.matches(str.getString()))
152             {
153               result = Boolean.TRUE;
154               break;
155             }
156           }
157           entry.setData(result);
158         }
159         else
160         {
161           entry.setData(Boolean.FALSE);
162         }
163       }
164     }
165     
166     
167     /**
168      * The equals method should return true if the given object is a SongOperation
169      * with the same semantics as this one. (i.e. it would perform the same operation.)
170      * This may be used to optimize performance. It is always safe to return false from
171      * this method, if the semantics of the given object cannot be determined.
172      * (This is similar to the equals method in java.util.Comparator.)
173      * As a corollary, it is safe just to fall back on the default implementation
174      * inherited from java.lang.Object.
175      *
176      * @param obj object to test
177      * @return true if the object is equal
178      */
179     public boolean equals(
180       Object obj)
181     {
182       if (obj instanceof SearchFieldPredicateSemantics)
183       {
184         SearchFieldPredicateSemantics other = (SearchFieldPredicateSemantics)obj;
185         return other.getFieldName().equals(fieldName_) &&
186           other.getSearchParameters().equals(params_);
187       }
188       return false;
189     }
190   }
191 }