Pipeline Switches

One of the things that make pipelines attract is the ability to create switches. Now my question to you is what if it isn’t the data your processing that should determine if a switch pushes the data down an alternate pipeline? We have several methods that perform this task

  • filter() filter out objects based on the Predicate().
  • switchIf() switch the data to different Pipeline if the Predicate indicates it should.

These methods on the pipeline allow a predicate to control their behavior. Typically the predicate would take in the data being processed and make a determination based on the data. However since we want a remote entity to control the decision, the data is passed in and ignored.

RemoteSwitch

RemoteSwitch is a simple class that implements the Predicate() function. However it maintains an internal boolean to indicate whether to allow the object to pass. A simple switch or entire another block of code can have a reference to this object and be able to effect the processing of the data in the pipeline. Calling the setEnabled() method on the switch with a boolean parameter will turn on/off switch.

package name.mymiller.extensions.utils.pipelines.switches;

import java.util.function.Predicate;

/**
 * @author jmiller Class used to remote enable/disable the predicate for use in
 *         pipeline switches.
 */
public class RemoteSwitch implements Predicate {

	private boolean enabled = false;

	/**
	 * Default Constructor specifying the switch is disabled.
	 */
	public RemoteSwitch() {
		this(false);
	}

	/**
	 * Constructor to specify initial setting for the switch
	 *
	 * @param enabled
	 *            boolean indicating if the swithc is enabled or disabled.
	 */
	public RemoteSwitch(boolean enabled) {
		super();
		this.enabled = enabled;
	}

	/**
	 * @return the enabled
	 */
	public synchronized boolean isEnabled() {
		return this.enabled;
	}

	/**
	 * @param enabled
	 *            the enabled to set
	 */
	public synchronized void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	@Override
	public boolean test(Object t) {
		return this.isEnabled();
	}

}

This is a simple instance, that could be used in Java code base. This could be extended easily based on your needs. The ability to pass in your own Predicate gives you the ability to construct a wide range of abilities into your code.

AbstractPropertyChangeListenerSwitch

First thing I just love long descriptive names for my classes, don’t you? Now then sometimes we want to automate our switch even a bit more. Why not tie in a PropertyChangeListner into our switch? Now I made this one abstract to give you a bit of flexibility here to make this work the way you want. Maybe this is a simple boolean flag on a listener, or maybe it’s more complete based on the value passed in and processing the incoming pipeline data against that value to determine to switch or not. Really this is more to show you the wide range of things you can do here.

package name.mymiller.extensions.utils.pipelines.switches;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.function.Predicate;

/**
 * @author jmiller
 *
 */
public abstract class AbstractPropertyChangeListenerSwitch<T> implements Predicate<T>, PropertyChangeListener {

	private String propertyName;

	private Object	newValue;
	private Object	source;
	private Object	oldValue;

	/**
	 * @param propertyName
	 * @param newValue
	 * @param source
	 * @param oldValue
	 */
	public AbstractPropertyChangeListenerSwitch(String propertyName, Object newValue, Object source, Object oldValue) {
		super();
		this.propertyName = propertyName;
		this.newValue = newValue;
		this.source = source;
		this.oldValue = oldValue;
	}

	/**
	 * @return the newValue
	 */
	protected synchronized Object getNewValue() {
		return this.newValue;
	}

	/**
	 * @return the oldValue
	 */
	protected synchronized Object getOldValue() {
		return this.oldValue;
	}

	/**
	 * @return the propertyName
	 */
	protected synchronized String getPropertyName() {
		return this.propertyName;
	}

	/**
	 * @return the source
	 */
	protected synchronized Object getSource() {
		return this.source;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.
	 * PropertyChangeEvent)
	 */
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		this.propertyName = evt.getPropertyName();
		this.newValue = evt.getNewValue();
		this.source = evt.getSource();
		this.oldValue = evt.getOldValue();
	}

}

The entire point here is to try to persuade you to think outside of the box. The flexibility here is only as great as your imagination. I believe you will find the pipeline tools here to be extremely flexible and a great development tool for you to use.

Ghostprogrammer
  • Ghostprogrammer
  • I am known for being able to quickly decipher difficult problems to assist development teams in producing a solution. I have been called upon to be the Team Lead for multiple large-scale projects. I have a keen interest in learning new technologies, always ready for a new challenge.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: