io.descoped.dc.api.node.builder.AbstractBaseNode Maven / Gradle / Ivy
The newest version!
package io.descoped.dc.api.node.builder;
import io.descoped.dc.api.node.Base;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
/**
* Base for node implementation classes
*/
public abstract class AbstractBaseNode implements Base {
@Override
public boolean instanceOf(Class extends Base> nodeClass) {
return this.getClass().isAssignableFrom(nodeClass);
}
@Override
public R cast(Class nodeClass) {
return nodeClass.cast(this);
}
@Override
public void given(Class isInstanceOfNodeClass, Consumer then) {
if (instanceOf(isInstanceOfNodeClass)) {
then.accept(cast(isInstanceOfNodeClass));
}
}
@Override
public void given(Class isInstanceOfNodeClass, AtomicBoolean andIfNotHandled, Consumer then) {
if (instanceOf(isInstanceOfNodeClass) && !andIfNotHandled.get()) {
then.accept(cast(isInstanceOfNodeClass));
andIfNotHandled.set(true);
}
}
}