xapi.inject.impl.LazyMerger 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.inject.impl;
import xapi.util.api.MergesValues;
/**
* A default object designed to convert two values into one value.
*
* The {@link #create(K1, K2)} method is provided to initializes the value.
* If you use a constructor that accepts another {@link MergesValues},
* that delegate will only be called to create the value object.
* If you override the create method, it will be called until it returns non null.
*
* If you want to perform some initialization on every call to {@link #merge(K1, K2)},
* you should override the {@link #refresh(K1, K2, V)} method.
*
* Care should be taken to only perform idempotent actions inside the refresh method,
* as implementations are free to assume it is safe (and performant) to call refresh many times.
*
* @author "James X. Nelson ([email protected])"
*
* @param
* @param
* @param
*/
public class LazyMerger
implements MergesValues
{
public LazyMerger() {
}
public LazyMerger(MergesValues merger) {
this.delegate = merger;
}
public LazyMerger(MergesValues merger, V value) {
this.delegate = merger;
this.value = value;
}
public LazyMerger(V value) {
this.value = value;
}
MergesValues delegate;
V value;
@Override
public final V merge(K1 k1, K2 k2) {
if (value == null) {
value = create(k1, k2);
if (value != null) {
refresh(k1, k2, value);
}
} else {
refresh(k1, k2, value);
}
return value;
}
/**
* Default action is to do nothing.
*
* This method is provided so you can fill in actions that should be taken
* on the value result, (only called when the value is not null).
*/
protected void refresh(K1 k1, K2 k2, V value) {
}
protected V create(K1 k1, K2 k2) {
return delegate == null ? null :
delegate.merge(k1, k2);
}
}