| 1 | package com.github.valid8j.pcond.core; | |
| 2 | ||
| 3 | import java.util.ArrayList; | |
| 4 | import java.util.LinkedList; | |
| 5 | import java.util.List; | |
| 6 | import java.util.function.BiFunction; | |
| 7 | import java.util.function.Function; | |
| 8 | ||
| 9 | import static java.util.Objects.requireNonNull; | |
| 10 | ||
| 11 | /** | |
| 12 | * The new design: | |
| 13 | * | |
| 14 | * Evaluator: Concentrates on "evaluate" an individual evaluable (form). No aware of how to compose evaluation entries. | |
| 15 | */ | |
| 16 | ||
| 17 | public class EvaluationContext<T> { | |
| 18 | final List<EvaluationEntry> evaluationEntries = new LinkedList<>(); | |
| 19 | final List<EvaluationEntry> visitorLineage = new LinkedList<>(); | |
| 20 | ||
| 21 | boolean expectationFlipped = false; | |
| 22 | ||
| 23 | public EvaluationContext() { | |
| 24 | } | |
| 25 | ||
| 26 | public EvaluationContext(EvaluationContext<?> parent) { | |
| 27 | this.expectationFlipped = parent.isExpectationFlipped(); | |
| 28 | } | |
| 29 | ||
| 30 | /** | |
| 31 | * @param evaluableIo An object to hold a form and its I/O. | |
| 32 | * @param evaluatorCallback A callback that executes a logic specific to the {@code evaluable}. | |
| 33 | */ | |
| 34 | public <E extends Evaluable<T>, O> void evaluate(EvaluableIo<T, E, O> evaluableIo, BiFunction<E, ValueHolder<T>, ValueHolder<O>> evaluatorCallback) { | |
| 35 |
1
1. evaluate : removed call to com/github/valid8j/pcond/core/EvaluationContext::evaluate → KILLED |
evaluate(resolveEvaluationEntryType(evaluableIo.evaluable()), evaluableIo, evaluatorCallback); |
| 36 | } | |
| 37 | ||
| 38 | public <E extends Evaluable<T>, O> void evaluate(EvaluationEntry.Type type, EvaluableIo<T, E, O> evaluableIo, BiFunction<E, ValueHolder<T>, ValueHolder<O>> evaluatorCallback) { | |
| 39 |
2
1. evaluate : removed call to com/github/valid8j/pcond/core/EvaluationContext::evaluate → KILLED 2. lambda$evaluate$0 : replaced return value with null for com/github/valid8j/pcond/core/EvaluationContext::lambda$evaluate$0 → KILLED |
evaluate(type, evaluableIo, io -> evaluatorCallback.apply(io.evaluable(), io.input())); |
| 40 | } | |
| 41 | ||
| 42 | public <E extends Evaluable<T>, O> void evaluate(EvaluationEntry.Type type, EvaluableIo<T, E, O> evaluableIo, Function<EvaluableIo<T, E, O>, ValueHolder<O>> function) { | |
| 43 |
1
1. evaluate : removed call to com/github/valid8j/pcond/core/EvaluationContext::evaluate → KILLED |
evaluate(type, formNameOf(evaluableIo), evaluableIo, function); |
| 44 | } | |
| 45 | ||
| 46 | public <E extends Evaluable<T>, O> void evaluate(EvaluationEntry.Type type, String formName, EvaluableIo<T, E, O> evaluableIo, Function<EvaluableIo<T, E, O>, ValueHolder<O>> function) { | |
| 47 | requireNonNull(evaluableIo); | |
| 48 | EvaluableIo<T, E, O> evaluableIoWork = this.enter(evaluableIo.input(), type, formName, evaluableIo.evaluable()); | |
| 49 |
1
1. evaluate : removed call to com/github/valid8j/pcond/core/EvaluationContext::leave → KILLED |
this.leave(evaluableIoWork, function.apply(evaluableIoWork)); |
| 50 |
1
1. evaluate : removed call to com/github/valid8j/pcond/core/DebuggingUtils::printTo → SURVIVED |
DebuggingUtils.printTo(this, System.err, 1); |
| 51 |
1
1. evaluate : removed call to com/github/valid8j/pcond/core/EvaluationContext::updateEvaluableIo → KILLED |
updateEvaluableIo(evaluableIo, evaluableIoWork); |
| 52 | } | |
| 53 | ||
| 54 | public static String formNameOf(EvaluableIo<?, ?, ?> evaluableIo) { | |
| 55 |
1
1. formNameOf : replaced return value with "" for com/github/valid8j/pcond/core/EvaluationContext::formNameOf → KILLED |
return formNameOf(evaluableIo.evaluableType(), evaluableIo.evaluable()); |
| 56 | } | |
| 57 | ||
| 58 | public static String formNameOf(EvaluationEntry.Type type, Evaluable<?> e) { | |
| 59 |
1
1. formNameOf : replaced return value with "" for com/github/valid8j/pcond/core/EvaluationContext::formNameOf → KILLED |
return type.formName(e); |
| 60 | } | |
| 61 | ||
| 62 | public boolean isExpectationFlipped() { | |
| 63 |
2
1. isExpectationFlipped : replaced boolean return with false for com/github/valid8j/pcond/core/EvaluationContext::isExpectationFlipped → KILLED 2. isExpectationFlipped : replaced boolean return with true for com/github/valid8j/pcond/core/EvaluationContext::isExpectationFlipped → KILLED |
return this.expectationFlipped; |
| 64 | } | |
| 65 | ||
| 66 | public void flipExpectation() { | |
| 67 |
1
1. flipExpectation : negated conditional → KILLED |
this.expectationFlipped = !expectationFlipped; |
| 68 | } | |
| 69 | ||
| 70 | private static <T, E extends Evaluable<T>, O> void updateEvaluableIo(EvaluableIo<T, E, O> evaluableIo, EvaluableIo<T, E, O> evaluableIoWork) { | |
| 71 |
1
1. updateEvaluableIo : removed call to com/github/valid8j/pcond/core/EvaluableIo::output → KILLED |
evaluableIo.output(evaluableIoWork.output()); |
| 72 | } | |
| 73 | ||
| 74 | public static <T> EvaluationEntry.Type resolveEvaluationEntryType(Evaluable<T> evaluable) { | |
| 75 |
3
1. resolveEvaluationEntryType : negated conditional → KILLED 2. resolveEvaluationEntryType : negated conditional → KILLED 3. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.LeafPred || evaluable instanceof Evaluable.CurriedContextPred || evaluable instanceof Evaluable.StreamPred) |
| 76 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/valid8j/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return EvaluationEntry.Type.LEAF; |
| 77 |
1
1. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.Func) |
| 78 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/valid8j/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return EvaluationEntry.Type.FUNCTION; |
| 79 |
1
1. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.Conjunction) |
| 80 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/valid8j/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return EvaluationEntry.Type.AND; |
| 81 |
1
1. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.Disjunction) |
| 82 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/valid8j/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return EvaluationEntry.Type.OR; |
| 83 |
1
1. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.Negation) |
| 84 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/valid8j/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return EvaluationEntry.Type.NOT; |
| 85 |
1
1. resolveEvaluationEntryType : negated conditional → KILLED |
if (evaluable instanceof Evaluable.Transformation) |
| 86 |
1
1. resolveEvaluationEntryType : replaced return value with null for com/github/valid8j/pcond/core/EvaluationContext::resolveEvaluationEntryType → KILLED |
return EvaluationEntry.Type.TRANSFORM_AND_CHECK; |
| 87 | throw new IllegalArgumentException(); | |
| 88 | } | |
| 89 | ||
| 90 | @SuppressWarnings("unchecked") | |
| 91 | private <E extends Evaluable<T>, O> EvaluableIo<T, E, O> enter(ValueHolder<T> input, EvaluationEntry.Type type, String formName, E evaluable) { | |
| 92 | EvaluableIo<T, Evaluable<T>, O> ret = createEvaluableIo(input, type, formName, evaluable); | |
| 93 | this.evaluationEntries.add(createEvaluationEntry(this, ret)); | |
| 94 |
1
1. enter : Replaced integer subtraction with addition → KILLED |
this.visitorLineage.add(evaluationEntries.get(evaluationEntries.size() - 1)); |
| 95 |
1
1. enter : replaced return value with null for com/github/valid8j/pcond/core/EvaluationContext::enter → KILLED |
return (EvaluableIo<T, E, O>) ret; |
| 96 | } | |
| 97 | ||
| 98 | private <E extends Evaluable<T>, O> void leave(EvaluableIo<T, E, O> evaluableIo, ValueHolder<O> output) { | |
| 99 |
1
1. leave : Replaced integer subtraction with addition → KILLED |
EvaluationEntry.Impl currentEvaluationEntry = (EvaluationEntry.Impl) this.visitorLineage.remove(this.visitorLineage.size() - 1); |
| 100 |
1
1. leave : removed call to com/github/valid8j/pcond/core/EvaluableIo::output → KILLED |
evaluableIo.output(output); |
| 101 |
1
1. leave : removed call to com/github/valid8j/pcond/core/EvaluationEntry$Impl::finalizeValues → KILLED |
currentEvaluationEntry.finalizeValues(); |
| 102 | } | |
| 103 | ||
| 104 | private static <T, O> EvaluableIo<T, Evaluable<T>, O> createEvaluableIo(ValueHolder<T> input, EvaluationEntry.Type type, String formName, Evaluable<T> evaluable) { | |
| 105 |
1
1. createEvaluableIo : replaced return value with null for com/github/valid8j/pcond/core/EvaluationContext::createEvaluableIo → KILLED |
return new EvaluableIo<>(input, type, formName, evaluable); |
| 106 | } | |
| 107 | ||
| 108 | private static <T, E extends Evaluable<T>> EvaluationEntry createEvaluationEntry( | |
| 109 | EvaluationContext<T> evaluationContext, | |
| 110 | EvaluableIo<T, E, ?> evaluableIo) { | |
| 111 |
1
1. createEvaluationEntry : replaced return value with null for com/github/valid8j/pcond/core/EvaluationContext::createEvaluationEntry → KILLED |
return new EvaluationEntry.Impl(evaluationContext, evaluableIo); |
| 112 | } | |
| 113 | ||
| 114 | public List<EvaluationEntry> resultEntries() { | |
| 115 |
1
1. resultEntries : replaced return value with Collections.emptyList for com/github/valid8j/pcond/core/EvaluationContext::resultEntries → KILLED |
return new ArrayList<>(this.evaluationEntries); |
| 116 | } | |
| 117 | ||
| 118 | public <R> void importEntries(EvaluationContext<R> childContext) { | |
| 119 |
1
1. importEntries : removed call to com/github/valid8j/pcond/core/EvaluationContext::importEntries → KILLED |
importEntries(childContext, currentIndentLevel()); |
| 120 | } | |
| 121 | ||
| 122 | public <R> void importEntries(EvaluationContext<R> childContext, int indentLevelGap) { | |
| 123 |
2
1. importEntries : removed call to java/util/List::forEach → SURVIVED 2. lambda$importEntries$1 : Replaced integer addition with subtraction → SURVIVED |
childContext.evaluationEntries.forEach(each -> each.level += indentLevelGap); |
| 124 | this.evaluationEntries.addAll(childContext.resultEntries()); | |
| 125 | } | |
| 126 | ||
| 127 | public int currentIndentLevel() { | |
| 128 |
1
1. currentIndentLevel : replaced int return with 0 for com/github/valid8j/pcond/core/EvaluationContext::currentIndentLevel → SURVIVED |
return this.visitorLineage.size(); |
| 129 | } | |
| 130 | } | |
Mutations | ||
| 35 |
1.1 |
|
| 39 |
1.1 2.2 |
|
| 43 |
1.1 |
|
| 49 |
1.1 |
|
| 50 |
1.1 |
|
| 51 |
1.1 |
|
| 55 |
1.1 |
|
| 59 |
1.1 |
|
| 63 |
1.1 2.2 |
|
| 67 |
1.1 |
|
| 71 |
1.1 |
|
| 75 |
1.1 2.2 3.3 |
|
| 76 |
1.1 |
|
| 77 |
1.1 |
|
| 78 |
1.1 |
|
| 79 |
1.1 |
|
| 80 |
1.1 |
|
| 81 |
1.1 |
|
| 82 |
1.1 |
|
| 83 |
1.1 |
|
| 84 |
1.1 |
|
| 85 |
1.1 |
|
| 86 |
1.1 |
|
| 94 |
1.1 |
|
| 95 |
1.1 |
|
| 99 |
1.1 |
|
| 100 |
1.1 |
|
| 101 |
1.1 |
|
| 105 |
1.1 |
|
| 111 |
1.1 |
|
| 115 |
1.1 |
|
| 119 |
1.1 |
|
| 123 |
1.1 2.2 |
|
| 128 |
1.1 |