Skip to content

Commit

Permalink
First attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
mkouba committed Aug 30, 2024
1 parent 5678c2b commit 4c0b0f7
Showing 1 changed file with 49 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand All @@ -31,17 +32,29 @@ public class LoopSectionHelper implements SectionHelper {
private static final String ITERABLE = "iterable";

private final String alias;
private final String metadataPrefix;
private final Expression iterable;
private final SectionBlock elseBlock;
private final Engine engine;

private final String[] metadataKeys;

LoopSectionHelper(SectionInitContext context, String metadataPrefix) {
this.alias = context.getParameterOrDefault(ALIAS, DEFAULT_ALIAS);
this.metadataPrefix = LoopSectionHelper.Factory.prefixValue(alias, metadataPrefix);
String prefix = LoopSectionHelper.Factory.prefixValue(alias, metadataPrefix);
this.iterable = Objects.requireNonNull(context.getExpression(ITERABLE));
this.elseBlock = context.getBlock(ELSE);
this.engine = context.getEngine();

if (prefix != null) {
// alias + "_" is the default
metadataKeys = new String[] { prefix + "count", prefix + "index", prefix + "indexParity",
prefix + "hasNext", prefix + "isLast", prefix + "isFirst", prefix + "isOdd",
prefix + "odd", prefix + "isEven", prefix + "even" };
} else {
metadataKeys = new String[] { "count", "index", "indexParity", "hasNext", "isLast", "isFirst", "isOdd", "odd",
"isEven", "even" };
}

}

@Override
Expand Down Expand Up @@ -142,7 +155,7 @@ private Iterator<?> extractIterator(Object it) {

CompletionStage<ResultNode> nextElement(Object element, int index, boolean hasNext, SectionResolutionContext context) {
ResolutionContext child = context.resolutionContext().createChild(
new IterationElement(alias, metadataPrefix, element, index, hasNext),
new IterationElement(element, index, hasNext),
null);
return context.execute(child);
}
Expand Down Expand Up @@ -266,20 +279,16 @@ static String prefixValue(String alias, String metadataPrefix) {
}
}

static class IterationElement implements Mapper {
class IterationElement implements Mapper {

static final CompletedStage<Object> EVEN = CompletedStage.of("even");
static final CompletedStage<Object> ODD = CompletedStage.of("odd");

final String alias;
final String metadataPrefix;
final CompletedStage<Object> element;
final int index;
final boolean hasNext;

public IterationElement(String alias, String metadataPrefix, Object element, int index, boolean hasNext) {
this.alias = alias;
this.metadataPrefix = metadataPrefix;
public IterationElement(Object element, int index, boolean hasNext) {
this.element = CompletedStage.of(element);
this.index = index;
this.hasNext = hasNext;
Expand All @@ -290,38 +299,43 @@ public CompletionStage<Object> getAsync(String key) {
if (alias.equals(key)) {
return element;
}
if (metadataPrefix != null) {
if (key.startsWith(metadataPrefix)) {
key = key.substring(metadataPrefix.length(), key.length());
} else {
return Results.notFound(key);
}
}
// Iteration metadata
final int count = index + 1;
return switch (key) {
case "count" -> CompletedStage.of(count);
case "index" -> CompletedStage.of(index);
case "indexParity" -> count % 2 == 0 ? EVEN : ODD;
case "hasNext" -> hasNext ? Results.TRUE : Results.FALSE;
case "isLast" -> hasNext ? Results.FALSE : Results.TRUE;
case "isFirst" -> index == 0 ? Results.TRUE : Results.FALSE;
case "isOdd", "odd" -> count % 2 != 0 ? Results.TRUE : Results.FALSE;
case "isEven", "even" -> count % 2 == 0 ? Results.TRUE : Results.FALSE;
default -> Results.notFound(key);
};
if (metadataKeys[0].equals(key)) {
// count
return CompletedStage.of(count);
} else if (metadataKeys[1].equals(key)) {
// index"
return CompletedStage.of(index);
} else if (metadataKeys[2].equals(key)) {
// indexParity
return count % 2 == 0 ? EVEN : ODD;
} else if (metadataKeys[3].equals(key)) {
// hasNext
return hasNext ? Results.TRUE : Results.FALSE;
} else if (metadataKeys[4].equals(key)) {
// isLast
return hasNext ? Results.FALSE : Results.TRUE;
} else if (metadataKeys[5].equals(key)) {
// isFirst
return index == 0 ? Results.TRUE : Results.FALSE;
} else if (metadataKeys[6].equals(key) || metadataKeys[7].equals(key)) {
// isOdd, odd
return count % 2 != 0 ? Results.TRUE : Results.FALSE;
} else if (metadataKeys[8].equals(key) || metadataKeys[9].equals(key)) {
// isEven, even
return count % 2 == 0 ? Results.TRUE : Results.FALSE;
}
return Results.notFound(key);

}

@Override
public Set<String> mappedKeys() {
if (metadataPrefix != null) {
return Set.of(alias, metadataPrefix + "count", metadataPrefix + "index", metadataPrefix + "indexParity",
metadataPrefix + "hasNext", metadataPrefix + "isLast", metadataPrefix + "isFirst",
metadataPrefix + "isOdd", metadataPrefix + "odd", metadataPrefix + "isEven", metadataPrefix + "even");
} else {
return Set.of(alias, "count", "index", "indexParity", "hasNext", "isLast", "isFirst", "isOdd",
"odd", "isEven", "even");
}
Set<String> ret = new HashSet<>();
ret.add(alias);
Collections.addAll(ret, metadataKeys);
return ret;
}

}
Expand Down

0 comments on commit 4c0b0f7

Please sign in to comment.