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 026import java.util.Arrays; 027import java.util.List; 028import java.util.ArrayList; 029 030/** 031 * The DefaultCompositeEventFilter can handle various attached {@link de.novanic.eventservice.client.event.filter.EventFilter} instances. 032 * The match method calls all attached {@link de.novanic.eventservice.client.event.filter.EventFilter} instances to check if the 033 * {@link de.novanic.eventservice.client.event.Event} has to be filtered. 034 * 035 * @author sstrohschein 036 * <br>Date: 04.03.2009 037 * <br>Time: 20:56:07 038 */ 039public class DefaultCompositeEventFilter implements CompositeEventFilter 040{ 041 private List<EventFilter> myEventFilters; 042 043 /** 044 * Creates a new DefaultCompositeEventFilter. {@link de.novanic.eventservice.client.event.filter.EventFilter} instances 045 * must be attached with {@link de.novanic.eventservice.client.event.filter.AppendableEventFilter#attach(EventFilter)} or 046 * the constructor {@link de.novanic.eventservice.client.event.filter.DefaultCompositeEventFilter#DefaultCompositeEventFilter(EventFilter...)} must 047 * be used to register {@link de.novanic.eventservice.client.event.filter.EventFilter} instances for the match method. 048 */ 049 public DefaultCompositeEventFilter() { 050 myEventFilters = new ArrayList<EventFilter>(); 051 } 052 053 /** 054 * Creates a new DefaultCompositeEventFilter. The {@link de.novanic.eventservice.client.event.filter.EventFilter} instances 055 * are used by the match method to filter the events. More {@link de.novanic.eventservice.client.event.filter.EventFilter} instances 056 * can be attached with the attach method ({@link de.novanic.eventservice.client.event.filter.AppendableEventFilter#attach(EventFilter)}). 057 * @param anEventFilters {@link de.novanic.eventservice.client.event.filter.EventFilter} which are used by the match method 058 */ 059 public DefaultCompositeEventFilter(EventFilter... anEventFilters) { 060 myEventFilters = new ArrayList<EventFilter>(Arrays.asList(anEventFilters)); 061 } 062 063 /** 064 * If the match method returns true for an event, the event should be ignored, because the EventFilter recognizes 065 * the event to filter it. The DefaultCompositeEventFilter uses the attached {@link de.novanic.eventservice.client.event.filter.EventFilter} 066 * instances to filter the events. See {@link de.novanic.eventservice.client.event.filter.DefaultCompositeEventFilter#DefaultCompositeEventFilter(EventFilter...)} 067 * or {@link de.novanic.eventservice.client.event.filter.AppendableEventFilter#attach(EventFilter)} to append 068 * {@link de.novanic.eventservice.client.event.filter.EventFilter} instances. 069 * @param anEvent event to check 070 * @return true when the event should be filtered, otherwise false 071 */ 072 public boolean match(Event anEvent) { 073 for(EventFilter theEventFilter: myEventFilters) { 074 if(theEventFilter.match(anEvent)) { 075 return true; 076 } 077 } 078 return false; 079 } 080 081 /** 082 * Appends another {@link de.novanic.eventservice.client.event.filter.EventFilter} which is used/called from the match method 083 * ({@link de.novanic.eventservice.client.event.filter.EventFilter#match(de.novanic.eventservice.client.event.Event)}). 084 * @param anEventFilter {@link de.novanic.eventservice.client.event.filter.EventFilter} to attach 085 * @return the created AppendableEventFilter (to attach more {@link de.novanic.eventservice.client.event.filter.EventFilter} instances) 086 */ 087 public AppendableEventFilter attach(EventFilter anEventFilter) { 088 myEventFilters.add(anEventFilter); 089 return this; 090 } 091 092 /** 093 * Detaches the attached {@link de.novanic.eventservice.client.event.filter.EventFilter} instances. 094 * EventFilter instances can be attached with {@link de.novanic.eventservice.client.event.filter.AppendableEventFilter#attach(EventFilter)}. 095 * @return true if the EventFilter was removed with that call, otherwise false 096 */ 097 public boolean detach(EventFilter anEventFilter) { 098 return myEventFilters.remove(anEventFilter); 099 } 100 101 /** 102 * Detaches all attached {@link de.novanic.eventservice.client.event.filter.EventFilter} instances. 103 * EventFilter instances can be attached with {@link de.novanic.eventservice.client.event.filter.AppendableEventFilter#attach(EventFilter)}. 104 * @return true if at least one EventFilter was removed with that call, otherwise false 105 */ 106 public boolean detach() { 107 boolean isEmpty = myEventFilters.isEmpty(); 108 myEventFilters.clear(); 109 return !isEmpty; 110 } 111 112 /** 113 * Returns the attached EventFilters. 114 * EventFilter instances can be attached with {@link de.novanic.eventservice.client.event.filter.AppendableEventFilter#attach(EventFilter)}. 115 * @return the attached {@link de.novanic.eventservice.client.event.filter.EventFilter} 116 */ 117 public List<EventFilter> getAttachedEventFilters() { 118 return myEventFilters; 119 } 120}