001    /*-----------------------------------------------------------------------
002     *  Copyright (C) 2006
003     *  Associated Universities, Inc. Washington DC, USA.
004     *  This program is free software; you can redistribute it and/or
005     *  modify it under the terms of the GNU General Public License as
006     *  published by the Free Software Foundation; either version 2 of
007     *  the License, or (at your option) any later version.
008     *
009     *  This program is distributed in the hope that it will be useful,
010     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *  GNU General Public License for more details.
013     *
014     *  Correspondence concerning this software should be addressed as follows:
015     *         Internet email: switz@nrao.edu
016     *         Postal address: SSS Software
017     *                         National Radio Astronomy Observatory
018     *                         Post Office Box 0
019     *                         Socorro, NM 87801  USA
020     *-----------------------------------------------------------------------*/
021    
022    package edu.nrao.sss.model.parameter;
023    
024    import java.util.regex.Pattern;
025    
026    //import org.apache.log4j.Logger;
027    
028    import edu.nrao.sss.util.Filter;
029    
030    /**
031     * A filter that operates on parameters.
032     * <p>
033     * <b>Revision Info:</b> <table style="margin-left:2em">
034     * <tr>
035     * <td>$Revision: 230 $</td>
036     * </tr>
037     * <tr>
038     * <td>$Date: 2007-01-02 14:37:07 -0700 (Tue, 02 Jan 2007) $</td>
039     * </tr>
040     * <tr>
041     * <td>$Author: dharland $</td>
042     * </tr>
043     * </table>
044     * </p>
045     * 
046     * @author David M. Harland
047     * @since 2006-06-27
048     */
049    public class ParameterFilter implements Filter<Parameter> {
050    
051      //private static final Logger log = Logger.getLogger(ParameterFilter.class);
052    
053        private static final Pattern ANY_PATTERN = null;
054    
055        public static final Pattern BLANK_PATTERN = Pattern.compile("");
056    
057        private Pattern bandPattern;
058    
059        private Pattern entityPattern;
060    
061        private Pattern modifiedByPattern;
062    
063        private Pattern parameterPattern;
064    
065        private Pattern valuePattern;
066    
067        /**
068         * Creates a new wide-open filter that allows all parameters to pass.
069         */
070        public ParameterFilter() {
071            clearAll();
072        }
073    
074        /**
075         * Returns <i>true</i> if this filter allows the given parameter to pass
076         * through it. <i>Null</i> parameters are always blocked.
077         * <p>
078         * The logic for allowing or blocking a parameter is somewhat complex. See
079         * {@link #blocks(Parameter)} for details.
080         * </p>
081         * 
082         * @param param
083         *                the parameter to be filtered.
084         * 
085         * @return <i>true</i> if this filter allows {@code param} to pass through
086         *         it.
087         */
088        public boolean allows(Parameter param) {
089            return !blocks(param);
090        }
091    
092        /**
093         * Returns <i>true</i> if this filter blocks the given parameter. <i>Null</i>
094         * parameters are always blocked.
095         * 
096         * @param param
097         *                the parameter to be filtered.
098         * 
099         * @return <i>true</i> if this filter blocks {@code param}.
100         */
101        public boolean blocks(Parameter param) {
102            // Filter blocks all null parameters
103            if (param == null)
104                return true;
105    
106            // Block parameter if its entity doesn't match the pattern
107            // (unless we're
108            // not
109            // filtering on entitys)
110            if ((entityPattern != ANY_PATTERN) && !entityPattern.matcher(param.getEntity()).matches())
111                return true;
112    
113            // Block parameter if its modifiedBy doesn't match the pattern
114            // (unless
115            // we're not
116            // filtering on modifiedBys)
117            if ((modifiedByPattern != ANY_PATTERN)
118                    && !modifiedByPattern.matcher(param.getModifiedBy()).matches())
119                return true;
120    
121            // Block parameter if its parameter doesn't match the pattern
122            // (unless
123            // we're not
124            // filtering on parameters)
125            if ((parameterPattern != ANY_PATTERN)
126                    && !parameterPattern.matcher(param.getParameterName()).matches())
127                return true;
128    
129            // Block parameter if its value doesn't match the pattern
130            // (unless we're
131            // not
132            // filtering on values)
133            if ((valuePattern != ANY_PATTERN) && !valuePattern.matcher(param.getValue()).matches())
134                return true;
135    
136            // Block parameter if its band doesn't match the pattern (unless
137            // we're
138            // not
139            // filtering on bands)
140            if ((bandPattern != ANY_PATTERN)) {
141                if (bandPattern.equals(BLANK_PATTERN)) {
142                    if (param.getBand() != null) {
143                        return true;
144                    }
145                } else if (param.getBand() == null || !bandPattern.matcher(param.getBand()).matches()) {
146                    return true;
147                }
148            }
149    
150            // parameter was not blocked
151            return false;
152        }
153    
154        /**
155         * Sets this filter to a wide-open state. After this call all filtering
156         * criteria will be in their wide-open states and this filter will allow all
157         * parameters to pass.
158         */
159        public void clearAll() {
160            clearBandPattern();
161            clearEntityPattern();
162            clearModifiedByPattern();
163            clearParameterPattern();
164            clearValuePattern();
165        }
166    
167        /**
168         * Sets the band criterion to its wide-open state.
169         * 
170         * @see #setBandPattern(Pattern)
171         */
172        public void clearBandPattern() {
173            bandPattern = ANY_PATTERN;
174        }
175    
176        /**
177         * Sets the entity criterion to its wide-open state.
178         * 
179         * @see #setEntityPattern(Pattern)
180         */
181        public void clearEntityPattern() {
182            entityPattern = ANY_PATTERN;
183        }
184    
185        /**
186         * Sets the band criterion to its wide-open state.
187         * 
188         * @see #setModifiedByPattern(Pattern)
189         */
190        public void clearModifiedByPattern() {
191            modifiedByPattern = ANY_PATTERN;
192        }
193    
194        /**
195         * Sets the parameter criterion to its wide-open state.
196         * 
197         * @see #setParameterPattern(Pattern)
198         */
199        public void clearParameterPattern() {
200            parameterPattern = ANY_PATTERN;
201        }
202    
203        /**
204         * Sets the value criterion to its wide-open state.
205         * 
206         * @see #setValuePattern(Pattern)
207         */
208        public void clearValuePattern() {
209            valuePattern = ANY_PATTERN;
210        }
211    
212        /**
213         * Sets a regular expression for matching the bands of parameters. Only
214         * parameters whose bands are matched by {@code regex} are allowed to pass
215         * through this filter. If parameters should not be filtered based on their
216         * bands, call {@link #clearBandPattern()}.
217         * 
218         * @param regex
219         *                a regular expression for matching the bands of parameters.
220         */
221        public void setBandPattern(Pattern regex) {
222            bandPattern = (regex == null) ? ANY_PATTERN : regex;
223        }
224    
225        /**
226         * Sets a regular expression for matching the entities of parameters. Only
227         * parameters whose entities are matched by {@code regex} are allowed to
228         * pass through this filter. If parameters should not be filtered based on
229         * their entity, call {@link #clearEntityPattern()}.
230         * 
231         * @param regex
232         *                a regular expression for matching the entityss of
233         *                parameters.
234         */
235        public void setEntityPattern(Pattern regex) {
236            entityPattern = (regex == null) ? ANY_PATTERN : regex;
237        }
238    
239        /**
240         * Sets a regular expression for matching the modifiedBy's of parameters.
241         * Only parameters whose modifiedBy's are matched by {@code regex} are
242         * allowed to pass through this filter. If parameters should not be filtered
243         * based on their modifiedBy's, call {@link #clearModifiedByPattern()}.
244         * 
245         * @param regex
246         *                a regular expression for matching the modifiedBy's of
247         *                parameters.
248         */
249        public void setModifiedByPattern(Pattern regex) {
250            modifiedByPattern = (regex == null) ? ANY_PATTERN : regex;
251        }
252    
253        /**
254         * Sets a regular expression for matching the parameterss of parameters.
255         * Only parameters whose parameters are matched by {@code regex} are allowed
256         * to pass through this filter. If parameters should not be filtered based
257         * on their parameters, call {@link #clearParameterPattern()}.
258         * 
259         * @param regex
260         *                a regular expression for matching the parameterss of
261         *                parameters.
262         */
263        public void setParameterPattern(Pattern regex) {
264            parameterPattern = (regex == null) ? ANY_PATTERN : regex;
265        }
266    
267        /**
268         * Sets a regular expression for matching the values of parameters. Only
269         * parameters whose values are matched by {@code regex} are allowed to pass
270         * through this filter. If parameters should not be filtered based on their
271         * values, call {@link #clearValuePattern()}.
272         * 
273         * @param regex
274         *                a regular expression for matching the values of
275         *                parameters.
276         */
277        public void setValuePattern(Pattern regex) {
278            valuePattern = (regex == null) ? ANY_PATTERN : regex;
279        }
280    }