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

jpaul.Misc.ActionPredicate Maven / Gradle / Ivy

Go to download

This library was originally developed by people listed at http://jpaul.sourceforge.net.

The newest version!
// ActionPredicate.java, created Wed Dec 21 07:14:42 2005 by salcianu
// Copyright (C) 2005 Alex Salcianu 
// Licensed under the Modified BSD Licence; see COPYING for details.
package jpaul.Misc;

/**
 * ActionPredicate is a combination of an
 * Action and a Predicate.  Intuitively, one
 * may say that ActionPredicate is an impure predicate,
 * but one should be aware of the fact that
 * ActionPredicate and Predicate are not iin
 * any subclassing relationship (in general, it is a bad software
 * engineering practice to have subclassing between pure and impure
 * varieties of the same concept).  Still, we provide a static method
 * ({@link #fromPredicate}) that converts a Predicate
 * into an ActionPredicate.
 *
 * @see Action
 * @see Predicate
 * 
 * @author  Alex Salcianu - [email protected]
 * @version $Id: ActionPredicate.java,v 1.4 2006/03/14 02:29:31 salcianu Exp $ */
public abstract class ActionPredicate implements Action {
    
    /** A boolean predicate WITH possible side-effects. */
    public abstract boolean actionPredicate(T obj);


    /** Action: executes {@link #actionPredicate} only for its
        side-effects, ignoring its result. */
    public final void action(T obj) {
	this.actionPredicate(obj);
    }


    /** Creates an ActionPredicate whose
        actionPredicate method first invokes the action
        from action and next returns the constant boolean
        value predicateResult.  */
    public static  ActionPredicate fromAction
	(final Action action,
	 final boolean predicateResult) {
	
	return new ActionPredicate() {
	    public boolean actionPredicate(T obj) {
		action.action(obj);
		return predicateResult;
	    }
	};
    }
    

    /** Creates an ActionPredicate wrapper around a
        Predicate: it does not perform any side-effect,
        it just returns the boolean value of teh predicate. */
    public static  ActionPredicate fromPredicate(final Predicate predicate) {
	return new ActionPredicate() {
	    public boolean actionPredicate(T obj) {
		return predicate.check(obj);
	    }
	};
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy