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

com.dragome.forms.bindings.client.channel.DefaultChannel Maven / Gradle / Ivy

There is a newer version: 0.96-beta4
Show newest version
package com.dragome.forms.bindings.client.channel;

import com.dragome.forms.bindings.client.binding.Disposable;
import com.dragome.forms.bindings.client.command.ParameterisedCommand;
import com.dragome.forms.bindings.client.function.Function;
import com.dragome.forms.bindings.client.util.SubscriptionList;
import com.dragome.forms.bindings.client.value.ValueTarget;
import com.dragome.model.interfaces.HasValue;

/**
 * Created by IntelliJ IDEA.
 * User: andrew
 * Date: Feb 23, 2010
 * Time: 2:32:52 PM
 * To change this template use File | Settings | File Templates.
 */
public class DefaultChannel extends SubscriptionList> implements Channel
{
	public void publish(final T value)
	{
		visitSubscribers(new Visitor>()
		{
			public void visit(Destination subscriber)
			{
				subscriber.receive(value);
			}
		});
	}

	public  Publisher getFormattedPublisher(final Function function)
	{
		if (function == null)
		{
			throw new NullPointerException("function is null");
		}
		return new Publisher()
		{
			public void publish(S value)
			{
				DefaultChannel.this.publish(function.compute(value));
			}
		};
	}

	public  Destination asFormattedDestination(final Function function)
	{
		if (function == null)
		{
			throw new NullPointerException("function is null");
		}
		return new Destination()
		{
			public void receive(S value)
			{
				publish(function.compute(value));
			}
		};
	}

	public Destination asDestination()
	{
		return new Destination()
		{
			public void receive(T value)
			{
				publish(value);
			}
		};
	}

	public Disposable sendTo(Destination destination)
	{
		if (destination == null)
		{
			throw new NullPointerException("destination is null");
		}
		return subscribe(destination);
	}

	public Disposable sendTo(final Publisher publisher)
	{
		return sendTo(new Destination()
		{
			public void receive(T value)
			{
				publisher.publish(value);
			}
		});
	}

	public Disposable sendTo(final ValueTarget destination)
	{
		return sendTo(new Destination()
		{
			public void receive(T value)
			{
				destination.setValue(value);
			}
		});
	}

	public Disposable sendTo(final HasValue destination)
	{
		return sendTo(new Destination()
		{
			public void receive(T value)
			{
				destination.setValue(value);
			}
		});
	}

	public Disposable sendTo(final ParameterisedCommand destination)
	{
		return sendTo(new Destination()
		{
			public void receive(T value)
			{
				destination.execute(value);
			}
		});
	}

}