Collection of useful utilities for Java games and apps. A lot of interesting utilities that could maybe still find some use if you work with Java...
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.
mightyutils/src/mightypork/utils/math/constraints/rect/caching/AbstractRectCache.java

74 lines
1.4 KiB

package mightypork.utils.math.constraints.rect.caching;
import mightypork.utils.math.constraints.CachedConstraint;
import mightypork.utils.math.constraints.rect.Rect;
import mightypork.utils.math.constraints.rect.proxy.RectAdapter;
import mightypork.utils.math.constraints.rect.var.RectVar;
/**
* <p>
* A rect 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 AbstractRectCache extends RectAdapter implements CachedConstraint<Rect> {
private final RectVar cache = Rect.makeVar();
private boolean inited = false;
private boolean cachingEnabled = true;
public AbstractRectCache() {
enableDigestCaching(true); // it changes only on poll
}
@Override
protected final Rect getSource()
{
if (!inited) poll();
return (cachingEnabled ? cache : getCacheSource());
}
@Override
public final void poll()
{
inited = true;
// poll source
final Rect source = getCacheSource();
source.markDigestDirty(); // poll cached
// store source value
cache.setTo(source);
markDigestDirty();
onConstraintChanged();
}
@Override
public final void enableCaching(boolean yes)
{
cachingEnabled = yes;
enableDigestCaching(yes);
}
@Override
public final boolean isCachingEnabled()
{
return cachingEnabled;
}
}