All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.refcodes.observer.EventMatcherByDeclaration Maven / Gradle / Ivy

Go to download

Artifact for event handling purposes according to the observer (observable) pattern.

There is a newer version: 1.1.1
Show newest version
// /////////////////////////////////////////////////////////////////////////////
// REFCODES.ORG
// =============================================================================
// This code is copyright (c) by Siegfried Steiner, Munich, Germany and licensed
// under the following (see "http://en.wikipedia.org/wiki/Multi-licensing")
// licenses:
// =============================================================================
// GNU General Public License, v3.0 ("http://www.gnu.org/licenses/gpl-3.0.html")
// together with the GPL linking exception applied; as being applied by the GNU
// Classpath ("http://www.gnu.org/software/classpath/license.html")
// =============================================================================
// Apache License, v2.0 ("http://www.apache.org/licenses/LICENSE-2.0")
// =============================================================================
// Please contact the copyright holding author(s) of the software artifacts in
// question for licensing issues not being covered by the above listed licenses,
// also regarding commercial licensing models or regarding the compatibility
// with other open source licenses.
// /////////////////////////////////////////////////////////////////////////////

package org.refcodes.observer;

import org.refcodes.matcher.Matcher;
import org.refcodes.matcher.MatcherDeclaration;

public class EventMatcherByDeclaration {

	/**
	 * Factory method to create an event matcher by event type.
	 * 
	 * @param  The type of the event to be matched
	 * 
	 * @param aEventType The event type to be matched by this matcher.
	 * 
	 * @return An event matcher by event type.
	 */
	public static > EventMatcher isAssignableFrom( Class aEventType ) {
		Matcher theAssignableFrom = MatcherDeclaration.isAssignableFrom( aEventType );
		return new EventMatcherWrapper( theAssignableFrom );
	}

	/**
	 * Factory method to create an event matcher by event publisher type.
	 * 
	 * @param  The type of the event to be matched
	 * @param  The publisher descriptor type
	 * 
	 * @param aPublisherType The event publisher type to be matched by this
	 *        matcher.
	 * 
	 * @return An event matcher by event type.
	 */
	public static , PT extends Object> EventMatcher publisherIsAssignableFrom( Class aPublisherType ) {
		return new PublisherIsAssignableFromMatcherImpl( aPublisherType );
	}

	/**
	 * Factory method to create an "OR" matcher for the given matchers.
	 * 
	 * @param  The event type applied to the matcher
	 * 
	 * @param aEventMatchers The matchers to be combined by an "OR".
	 * 
	 * @return An "OR" matcher.
	 */
	@SafeVarargs
	public static > EventMatcher or( EventMatcher... aEventMatchers ) {
		return new EventMatcherWrapper( MatcherDeclaration.or( aEventMatchers ) );
	}

	/**
	 * Factory method to create an "AND" matcher for the given matchers.
	 * 
	 * @param  The event type applied to the matcher
	 * 
	 * @param aEventMatchers The matchers to be combined by an "AND".
	 * 
	 * @return An "AND" matcher.
	 */
	@SafeVarargs
	public static > EventMatcher and( EventMatcher... aEventMatchers ) {
		return new EventMatcherWrapper( MatcherDeclaration.and( aEventMatchers ) );
	}

	/**
	 * Factory method to create an "EQUAL WITH" matcher for the given name
	 * compared with the name stored in the {@link EventMetaData}.
	 * 
	 * @param  The event type applied to the matcher
	 * 
	 * @param aName The name to be compared with a {@link MetaDataEvent}'s
	 *        {@link EventMetaData}'s name property.
	 * 
	 * @return An "EQUAL WITH" matcher regarding the {@link MetaDataEvent}'s
	 *         name property.
	 */
	public static > EventMatcher nameEqualWith( String aName ) {
		return new NameEqualWithMatcherImpl( aName );
	}

	/**
	 * Factory method to create an "EQUAL WITH" matcher for the given group
	 * compared with the group stored in the {@link EventMetaData}.
	 * 
	 * @param  The event type applied to the matcher
	 * 
	 * @param aGroup The group to be compared with a {@link MetaDataEvent}'s
	 *        {@link EventMetaData}'s group property.
	 * 
	 * @return An "EQUAL WITH" matcher regarding the {@link MetaDataEvent}'s
	 *         group property.
	 */
	public static > EventMatcher groupEqualWith( String aGroup ) {
		return new GroupEqualWithMatcherImpl( aGroup );
	}

	/**
	 * Factory method to create an "EQUAL WITH" matcher for the given UID
	 * compared with the UID stored in the {@link EventMetaData}.
	 * 
	 * @param  The event type applied to the matcher
	 * 
	 * @param aUniversalId The UID to be compared with a {@link MetaDataEvent}'s
	 *        {@link EventMetaData}'s UID property.
	 * 
	 * @return An "EQUAL WITH" matcher regarding the {@link MetaDataEvent}'s UID
	 *         property.
	 */
	public static > EventMatcher universalIdEqualWith( String aUniversalId ) {
		return new UniversalIdEqualWithMatcherImpl( aUniversalId );
	}

	/**
	 * Factory method to create an "EQUAL WITH" matcher for the given action
	 * compared with the action stored in the {@link EventMetaData}.
	 * 
	 * @param  The event type applied to the matcher.
	 * 
	 * @param  The type of the action stored in the event. CAUTION: The
	 *        drawback of not using generic generic type declaration on a class
	 *        level is no granted type safety, the advantage is the ease of use:
	 *        Sub-classes can be used out of the box.
	 * 
	 * @param aAction The action to be compared with a {@link MetaDataEvent}'s
	 *        {@link EventMetaData}'s action property.
	 * 
	 * @return An "EQUAL WITH" matcher regarding the {@link ActionEvent}'s
	 *         action property.
	 */
	public static , A> EventMatcher actionEqualWith( A aAction ) {
		return new ActionEqualWithMatcherImpl( aAction );
	}

	// -------------------------------------------------------------------------
	// INNER CLASSES:
	// -------------------------------------------------------------------------

	/**
	 * Wrapps an event matcher to by of the correct type.
	 */
	private static class EventMatcherWrapper> implements EventMatcher {

		private Matcher _eventMatcher;

		/**
		 * Wrapps the given event matcher to be of the correct type.
		 * 
		 * @param aMatcher the event matcher in question.
		 */
		public EventMatcherWrapper( Matcher aMatcher ) {
			assert (aMatcher != null);
			_eventMatcher = aMatcher;
		}

		@Override
		public boolean isMatching( E aEvent ) {
			return _eventMatcher.isMatching( aEvent );
		}
	}

	/**
	 * A basic implementation of an event matcher.
	 * 
	 * @param  The event type
	 */
	private static class PublisherIsAssignableFromMatcherImpl, PT extends Object> implements EventMatcher {

		private Class _eventPublisherType;

		public PublisherIsAssignableFromMatcherImpl( Class aEventPublisherType ) {
			_eventPublisherType = aEventPublisherType;
		}

		@Override
		public boolean isMatching( E aEvent ) {
			assert (aEvent != null);
			if ( _eventPublisherType != null ) {
				if ( !aEvent.getMetaData().getPublisherType().isAssignableFrom( _eventPublisherType ) ) { return false; }
			}
			return true;
		}
	}

	/**
	 * A basic implementation of an event matcher.
	 * 
	 * @param  The event type
	 */
	private static class NameEqualWithMatcherImpl> implements EventMatcher {

		private String _name;

		public NameEqualWithMatcherImpl( String aName ) {
			_name = aName;
		}

		@Override
		public boolean isMatching( E aEvent ) {
			assert (aEvent != null);

			if ( _name != null ) {
				return (_name.equals( aEvent.getMetaData().getName() ));
			}
			else if ( aEvent.getMetaData().getName() == null ) { return true; }
			return false;
		}
	}

	/**
	 * A basic implementation of an event matcher.
	 * 
	 * @param  The event type
	 */
	private static class GroupEqualWithMatcherImpl> implements EventMatcher {

		private String _group;

		public GroupEqualWithMatcherImpl( String aGroup ) {
			_group = aGroup;
		}

		@Override
		public boolean isMatching( E aEvent ) {
			assert (aEvent != null);

			if ( _group != null ) {
				return (_group.equals( aEvent.getMetaData().getGroup() ));
			}
			else if ( aEvent.getMetaData().getGroup() == null ) { return true; }
			return false;
		}
	}

	/**
	 * A basic implementation of an event matcher.
	 * 
	 * @param  The event type
	 */
	private static class UniversalIdEqualWithMatcherImpl> implements EventMatcher {

		private String _universalId;

		public UniversalIdEqualWithMatcherImpl( String aUniversalId ) {
			_universalId = aUniversalId;
		}

		@Override
		public boolean isMatching( E aEvent ) {
			assert (aEvent != null);

			if ( _universalId != null ) {
				return (_universalId.equals( aEvent.getMetaData().getUniversalId() ));
			}
			else if ( aEvent.getMetaData().getUniversalId() == null ) { return true; }
			return false;
		}
	}

	/**
	 * A basic implementation of an event matcher.
	 * 
	 * @param  The event type
	 */
	private static class ActionEqualWithMatcherImpl> implements EventMatcher {

		private Object _action;

		public  ActionEqualWithMatcherImpl( A aAction ) {
			_action = aAction;
		}

		@Override
		public boolean isMatching( E aEvent ) {
			assert (aEvent != null);

			if ( _action != null ) {
				return (_action.equals( aEvent.getAction() ));
			}
			else if ( aEvent.getAction() == null ) { return true; }
			return false;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy