public Pattern<T> with(PropertyPattern<? super T, ?, ?> pattern) { return new WithPattern<>(pattern, this); }
@Override public <T> Match<T> matchWith(WithPattern<T> withPattern, Object object, Captures captures) { Function<? super T, Optional<?>> property = withPattern.getProperty().getFunction(); Optional<?> propertyValue = property.apply((T) object); Match<?> propertyMatch = propertyValue .map(value -> match(withPattern.getPattern(), value, captures)) .orElse(Match.empty()); return propertyMatch.map(ignored -> (T) object); }
@Override public <T> Match<T> matchWith(WithPattern<T> withPattern, Object object, Captures captures) { Function<? super T, Optional<?>> property = withPattern.getProperty().getFunction(); Optional<?> propertyValue = property.apply((T) object); Optional<?> resolvedValue = propertyValue .map(value -> value instanceof GroupReference ? lookup.resolve(((GroupReference) value)) : value); Match<?> propertyMatch = resolvedValue .map(value -> match(withPattern.getPattern(), value, captures)) .orElse(Match.empty()); return propertyMatch.map(ignored -> (T) object); } }
@Override public void visitWith(WithPattern<?> pattern) { visitPrevious(pattern); appendLine("with(%s)", pattern.getProperty().getName()); level += 1; pattern.getPattern().accept(this); level -= 1; }
public Pattern<T> with(PropertyPattern<? super T, ?> pattern) { return new WithPattern<>(pattern, this); }
@Override public void visitWith(WithPattern<?> pattern) { visitPrevious(pattern); appendLine("with(%s)", pattern.getProperty().getName()); level += 1; pattern.getPattern().accept(this); level -= 1; }