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.user;
023    
024    import edu.nrao.sss.util.Filter;
025    
026    /**
027     * A filter that operates on parameters.
028     * <p>
029     * <b>Revision Info:</b> <table style="margin-left:2em">
030     * <tr>
031     * <td>$Revision: 230 $</td>
032     * </tr>
033     * <tr>
034     * <td>$Date: 2007-01-02 14:37:07 -0700 (Tue, 02 Jan 2007) $</td>
035     * </tr>
036     * <tr>
037     * <td>$Author: dharland $</td>
038     * </tr>
039     * </table>
040     * </p>
041     * 
042     * @author David M. Harland
043     * @since 2006-06-27
044     */
045    public class UserFilter implements Filter<User> {
046            private static final String ANY_PATTERN = null;
047    
048            private String lastNamePattern;
049    
050    
051            /**
052             * Creates a new wide-open filter that allows all parameters to pass.
053             */
054            public UserFilter() {
055                    clearAll();
056            }
057    
058            // ============================================================================
059            // CLEARING THE FILTERING CRITERIA
060            // ============================================================================
061    
062            /**
063             * Returns <i>true</i> if this filter allows the given parameter to pass
064             * through it. <i>Null</i> parameters are always blocked.
065             * <p>
066             * The logic for allowing or blocking a parameter is somewhat complex. See
067             * {@link #blocks(User)} for details.
068             * </p>
069             * 
070             * @param param
071             *            the parameter to be filtered.
072             * 
073             * @return <i>true</i> if this filter allows {@code param} to pass through
074             *         it.
075             */
076            public boolean allows(User param) {
077                    return !blocks(param);
078            }
079    
080            /**
081             * Returns <i>true</i> if this filter blocks the given parameter. <i>Null</i>
082             * parameters are always blocked.
083             * 
084             * @param user
085             *            the parameter to be filtered.
086             * 
087             * @return <i>true</i> if this filter blocks {@code user}.
088             */
089            public boolean blocks(User user) {
090                    // Filter blocks all null users
091                    if (user == null)
092                            return true;
093    
094                    if ((lastNamePattern != ANY_PATTERN) && 
095                        (user.getLastName() == null ||
096                         !user.getLastName().matches(lastNamePattern)))
097                            return true;
098    
099                    return false;
100            }
101    
102            /**
103             * Sets this filter to a wide-open state. After this call all filtering
104             * criteria will be in their wide-open states and this filter will allow all
105             * parameters to pass.
106             */
107            public void clearAll() {
108                    clearLastNamePattern();
109            }
110    
111            /**
112             * Sets the band criterion to its wide-open state.
113             * 
114             * @see #setLastNamePattern(String)
115             */
116            public void clearLastNamePattern() {
117                    lastNamePattern = ANY_PATTERN;
118            }
119    
120    
121            /**
122             * Sets a regular expression for matching the bandss of parameters. Only
123             * parameters whose bands are matched by {@code regex} are allowed to pass
124             * through this filter. If parameters should not be filtered based on their
125             * bands, call {@link #clearLastNamePattern()}.
126             * 
127             * @param regex
128             *            a regular expression for matching the bandss of parameters.
129             */
130            public void setLastNamePattern(String regex) {
131                    lastNamePattern = (regex == null) ? ANY_PATTERN : regex;
132            }
133    
134    }