add common base class for generated Dataset classes

Fixes #10136
This commit is contained in:
Dan Hermann 2018-11-08 17:02:17 -06:00
parent dbf241285e
commit e0a125f3b9
3 changed files with 25 additions and 4 deletions

View file

@ -0,0 +1,21 @@
package org.logstash.config.ir.compiler;
/**
* Provides common behavior for Dataset classes generated by Java execution.
*/
public abstract class BaseDataset implements Dataset {
private boolean done;
protected void setDone() {
done = true;
}
protected void clearDone() {
done = false;
}
protected boolean isDone() {
return done;
}
}

View file

@ -107,7 +107,7 @@ public final class ComputeStepSyntaxElement<T extends Dataset> {
try {
return REDUNDANT_SEMICOLON.matcher(new Formatter().formatSource(
String.format(
"package org.logstash.generated;\npublic final class %s implements %s { %s }",
"package org.logstash.generated;\npublic final class %s extends org.logstash.config.ir.compiler.BaseDataset implements %s { %s }",
name,
type.getName(),
SyntaxFactory.join(

View file

@ -258,18 +258,18 @@ public final class DatasetCompiler {
*/
private static DatasetCompiler.ComputeAndClear withOutputBuffering(final Closure compute,
final Closure clear, final ValueSyntaxElement outputBuffer, final ClassFields fields) {
final ValueSyntaxElement done = fields.add(boolean.class);
final SyntaxFactory.MethodCallReturnValue done = new SyntaxFactory.MethodCallReturnValue(SyntaxFactory.value("this"), "isDone");
return computeAndClear(
Closure.wrap(
SyntaxFactory.ifCondition(done, Closure.wrap(SyntaxFactory.ret(outputBuffer)))
).add(compute)
.add(SyntaxFactory.assignment(done, SyntaxFactory.identifier("true")))
.add(new SyntaxFactory.MethodCallReturnValue(SyntaxFactory.value("this"), "setDone"))
.add(SyntaxFactory.ret(outputBuffer)),
Closure.wrap(
SyntaxFactory.ifCondition(
done, Closure.wrap(
clear.add(clear(outputBuffer)),
SyntaxFactory.assignment(done, SyntaxFactory.identifier("false"))
new SyntaxFactory.MethodCallReturnValue(SyntaxFactory.value("this"), "clearDone")
)
)
), fields