001/*
002 * GWTEventService
003 * Copyright (c) 2011 and beyond, strawbill UG (haftungsbeschr?nkt)
004 *
005 * This is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU Lesser General Public License as
007 * published by the Free Software Foundation; either version 3 of
008 * the License, or (at your option) any later version.
009 * Other licensing for GWTEventService may also be possible on request.
010 * Please view the license.txt of the project for more information.
011 *
012 * This software is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this software; if not, write to the Free
019 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021 */
022package de.novanic.eventservice.client.event.filter;
023
024import de.novanic.eventservice.client.event.Event;
025
026/**
027 * Default implementation of the {@link de.novanic.eventservice.client.event.filter.EventFilter} interface.
028 * When no {@link de.novanic.eventservice.client.event.filter.EventFilter} is attached to the DefaultEventFilter,
029 * the DefaultEventFilter doesn't filter any events, because the match method returns false. An optional
030 * {@link de.novanic.eventservice.client.event.filter.EventFilter} instance can be attached using
031 * {@link de.novanic.eventservice.client.event.filter.AppendableEventFilter#attach(EventFilter)} or the constructor
032 * {@link de.novanic.eventservice.client.event.filter.DefaultEventFilter#DefaultEventFilter(EventFilter)} and is used/called
033 * by the default implementation of the match method of DefaultEventFilter. The DefaultEventFilter implements the
034 * {@link de.novanic.eventservice.client.event.filter.CascadingEventFilter} interface to allow to build cascading filter
035 * sequences. A CascadingEventFilter can only hold one (the next) {@link de.novanic.eventservice.client.event.filter.EventFilter}.
036 * See {@link de.novanic.eventservice.client.event.filter.CascadingEventFilter} for more information.
037 *
038 * @author sstrohschein
039 * <br>Date: 20.07.2008
040 * <br>Time: 15:33:39
041 */
042public class DefaultEventFilter implements CascadingEventFilter
043{
044    private EventFilter myNextEventFilter;
045
046    /**
047     * Creates a new DefaultEventFilter. An optional, appended {@link de.novanic.eventservice.client.event.filter.EventFilter} can
048     * be set/attached with {@link de.novanic.eventservice.client.event.filter.AppendableEventFilter#attach(EventFilter)} or
049     * the constructor {@link de.novanic.eventservice.client.event.filter.DefaultEventFilter#DefaultEventFilter(EventFilter)}.
050     */
051    public DefaultEventFilter() {}
052
053    /**
054     * Creates a new DefaultEventFilter. The {@link de.novanic.eventservice.client.event.filter.EventFilter} is used by
055     * the default implementation of the match method (in DefaultEventFilter) to filter the events.
056     * @param aNextEventFilter {@link de.novanic.eventservice.client.event.filter.EventFilter} to attach
057     */
058    public DefaultEventFilter(EventFilter aNextEventFilter) {
059        attach(aNextEventFilter);
060    }
061
062    /**
063     * When no {@link de.novanic.eventservice.client.event.filter.EventFilter} is attached to the DefaultEventFilter,
064     * no events will be filtered, because that method implementation returns false. An optional
065     * {@link de.novanic.eventservice.client.event.filter.EventFilter} instance can be attached using
066     * {@link de.novanic.eventservice.client.event.filter.AppendableEventFilter#attach(EventFilter)} or the constructor
067     * {@link de.novanic.eventservice.client.event.filter.DefaultEventFilter#DefaultEventFilter(EventFilter)}.
068     * @param anEvent event
069     * @return false or the result of the optional, attached {@link de.novanic.eventservice.client.event.filter.EventFilter}
070     */
071    public boolean match(Event anEvent) {
072        return myNextEventFilter != null && myNextEventFilter.match(anEvent);
073    }
074
075    /**
076     * Sets/Attaches another {@link de.novanic.eventservice.client.event.filter.EventFilter} which is used/called from the match method
077     * ({@link de.novanic.eventservice.client.event.filter.EventFilter#match(de.novanic.eventservice.client.event.Event)}).
078     * A CascadingEventFilter can only hold one {@link de.novanic.eventservice.client.event.filter.EventFilter}.
079     * @param anEventFilter {@link de.novanic.eventservice.client.event.filter.EventFilter} to attach/set
080     * @return itself/DefaultEventFilter (result of the attachment)
081     */
082    public AppendableEventFilter attach(EventFilter anEventFilter) {
083        myNextEventFilter = anEventFilter;
084        return this;
085    }
086
087    /**
088     * Detaches the attached {@link de.novanic.eventservice.client.event.filter.EventFilter}. A CascadingEventFilter can
089     * only hold one {@link de.novanic.eventservice.client.event.filter.EventFilter}.
090     * EventFilter instances can be attached with {@link de.novanic.eventservice.client.event.filter.AppendableEventFilter#attach(EventFilter)}.
091     * @return true if an EventFilter was registered, otherwise false
092     */
093    public boolean detach() {
094        if(myNextEventFilter != null) {
095            myNextEventFilter = null;
096            return true;
097        }
098        return false;
099    }
100
101    /**
102     * Returns the attached EventFilter. A CascadingEventFilter can only hold one {@link de.novanic.eventservice.client.event.filter.EventFilter}.
103     * EventFilter instances can be attached with {@link de.novanic.eventservice.client.event.filter.AppendableEventFilter#attach(EventFilter)}.
104     * @return the attached {@link de.novanic.eventservice.client.event.filter.EventFilter}
105     */
106    public EventFilter getAttachedEventFilter() {
107        return myNextEventFilter;
108    }
109}