io.parsingdata.metal.expression.value.Fold Maven / Gradle / Ivy
Show all versions of metal-core Show documentation
/*
* Copyright 2013-2016 Netherlands Forensic Institute
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.parsingdata.metal.expression.value;
import static io.parsingdata.metal.Trampoline.complete;
import static io.parsingdata.metal.Trampoline.intermediate;
import static io.parsingdata.metal.Util.checkNotNull;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BinaryOperator;
import io.parsingdata.metal.Trampoline;
import io.parsingdata.metal.Util;
import io.parsingdata.metal.data.ImmutableList;
import io.parsingdata.metal.data.ParseState;
import io.parsingdata.metal.encoding.Encoding;
/**
* Base class for {@link ValueExpression} implementations of the Fold
* operation.
*
* Fold has three operands: values
(a {@link ValueExpression}),
* reducer
(a {@link BinaryOperator}) and initial
(a
* {@link ValueExpression}). First initial
is evaluated. If it
* does not return a single value, the final result is an empty list. Next,
* values
is evaluated and its result is passed to the abstract
* {@link #prepareValues(ImmutableList)} method. The returned list is prefixed
* by the value returned by evaluating initial
. On this list, the
* reducer
is applied to the first two values until a single
* value remains, which is then returned.
*/
public abstract class Fold implements ValueExpression {
public final ValueExpression values;
public final BinaryOperator reducer;
public final ValueExpression initial;
public Fold(final ValueExpression values, final BinaryOperator reducer, final ValueExpression initial) {
this.values = checkNotNull(values, "values");
this.reducer = checkNotNull(reducer, "reducer");
this.initial = initial;
}
@Override
public ImmutableList> eval(final ParseState parseState, final Encoding encoding) {
final ImmutableList> initial = this.initial != null ? this.initial.eval(parseState, encoding) : new ImmutableList<>();
if (initial.size > 1) {
return new ImmutableList<>();
}
final ImmutableList> values = prepareValues(this.values.eval(parseState, encoding));
if (values.isEmpty() || containsEmpty(values).computeResult()) {
return initial;
}
if (!initial.isEmpty()) {
return ImmutableList.create(fold(parseState, encoding, reducer, initial.head, values).computeResult());
}
return ImmutableList.create(fold(parseState, encoding, reducer, values.head, values.tail).computeResult());
}
private Trampoline> fold(final ParseState parseState, final Encoding encoding, final BinaryOperator reducer, final Optional head, final ImmutableList> tail) {
if (!head.isPresent() || tail.isEmpty()) {
return complete(() -> head);
}
final ImmutableList> reducedValue = reduce(reducer, head.get(), tail.head.get()).eval(parseState, encoding);
if (reducedValue.size != 1) {
throw new IllegalArgumentException("Reducer must evaluate to a single value.");
}
return intermediate(() -> fold(parseState, encoding, reducer, reducedValue.head, tail.tail));
}
private Trampoline containsEmpty(final ImmutableList> list) {
if (list.isEmpty()) {
return complete(() -> false);
}
return list.head
.map(t -> intermediate(() -> containsEmpty(list.tail)))
.orElseGet(() -> complete(() -> true));
}
protected abstract ImmutableList> prepareValues(ImmutableList> values);
protected abstract ValueExpression reduce(BinaryOperator reducer, Value head, Value tail);
@Override
public String toString() {
return getClass().getSimpleName() + "(" + values + "," + reducer + (initial == null ? "" : "," + initial) + ")";
}
@Override
public boolean equals(final Object obj) {
return Util.notNullAndSameClass(this, obj)
&& Objects.equals(values, ((Fold)obj).values)
&& Objects.equals(reducer, ((Fold)obj).reducer)
&& Objects.equals(initial, ((Fold)obj).initial);
}
@Override
public int hashCode() {
return Objects.hash(getClass(), values, reducer, initial);
}
}