xapi.util.impl.ReceivesMultiValue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xapi-gwt Show documentation
Show all versions of xapi-gwt Show documentation
This module exists solely to package all other gwt modules into a single
uber jar. This makes deploying to non-mavenized targets much easier.
Of course, you would be wise to inherit your dependencies individually;
the uber jar is intended for projects like collide,
which have complex configuration, and adding many jars would be a pain.
The newest version!
package xapi.util.impl;
import xapi.collect.api.Fifo;
import xapi.collect.impl.SimpleFifo;
import xapi.util.api.ReceivesValue;
/**
* An object designed to delegate a received value to multiple ReceivesValue callbacks.
*
*
*
* Fires callbacks based on insertion order.
* If you need more control of firing order, use {@link ReceivesPrioritizedValue} instead.
*
* @author James X. Nelson ([email protected], @james)
*
* @param - The type of object / event / signal being sent.
*/
public class ReceivesMultiValue implements ReceivesValue{
/** A simple array of callbacks to fire. Uses low-level js arrays in gwt.*/
final Fifo> handlers = new SimpleFifo>();
public void set(T value) {
while(!handlers.isEmpty()){
handlers.take().set(value);
}
};
/**
* Adds a receiver to the end of the callback array.
*
* @param receiver - A new receiver to add
* @return true if the receiver was added to our callbacks; false if already present.
*/
public boolean addReceiver(ReceivesValue receiver){
assert receiver != null : "Do not send null receivers to "+this+"; (ReceivesMultiValue) ";
assert receiver != this : "Do not send a ReceivesMultiValue to itself. Class: "+this+";";
if (handlers.contains(receiver))
return false;
handlers.give(receiver);
return true;
}
/**
* Clear our array of callbacks
*/
public void clearReceivers(){
handlers.clear();
}
/**
* @param receiver - The receiver to remove.
*/
public void removeReceiver(ReceivesValue receiver){
handlers.remove(receiver);
}
}