You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.5 KiB
84 lines
1.5 KiB
package mightypork.gamecore.util.math.constraints.num.caching;
|
|
|
|
|
|
import mightypork.gamecore.util.math.constraints.ConstraintCache;
|
|
import mightypork.gamecore.util.math.constraints.num.Num;
|
|
import mightypork.gamecore.util.math.constraints.num.mutable.NumVar;
|
|
import mightypork.gamecore.util.math.constraints.num.proxy.NumAdapter;
|
|
|
|
|
|
/**
|
|
* <p>
|
|
* A Num cache.
|
|
* </p>
|
|
* <p>
|
|
* Values are held in a caching VectVar, and digest caching is enabled by
|
|
* default.
|
|
* </p>
|
|
*
|
|
* @author Ondřej Hruška (MightyPork)
|
|
*/
|
|
public abstract class AbstractNumCache extends NumAdapter implements ConstraintCache<Num> {
|
|
|
|
private final NumVar cache = Num.makeVar();
|
|
private boolean inited = false;
|
|
private boolean cachingEnabled = true;
|
|
|
|
|
|
public AbstractNumCache()
|
|
{
|
|
enableDigestCaching(true); // it changes only on poll
|
|
}
|
|
|
|
|
|
@Override
|
|
protected final Num getSource()
|
|
{
|
|
if (!inited) markDigestDirty();
|
|
|
|
return (cachingEnabled ? cache : getCacheSource());
|
|
}
|
|
|
|
|
|
@Override
|
|
public final void poll()
|
|
{
|
|
inited = true;
|
|
|
|
// poll source
|
|
final Num source = getCacheSource();
|
|
source.markDigestDirty(); // poll cached
|
|
|
|
// store source value
|
|
cache.setTo(source);
|
|
|
|
// mark my digest dirty
|
|
markDigestDirty();
|
|
|
|
onConstraintChanged();
|
|
}
|
|
|
|
|
|
@Override
|
|
public abstract void onConstraintChanged();
|
|
|
|
|
|
@Override
|
|
public abstract Num getCacheSource();
|
|
|
|
|
|
@Override
|
|
public final void enableCaching(boolean yes)
|
|
{
|
|
cachingEnabled = yes;
|
|
enableDigestCaching(yes);
|
|
}
|
|
|
|
|
|
@Override
|
|
public final boolean isCachingEnabled()
|
|
{
|
|
return cachingEnabled;
|
|
}
|
|
|
|
}
|
|
|