Changeset 16259 in josm for trunk


Ignore:
Timestamp:
2020-04-11T17:36:58+02:00 (4 years ago)
Author:
simon04
Message:

Implement SearchCompiler.AbstractBinaryMatch.map

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/search/SearchCompiler.java

    r15893 r16259  
    1818import java.util.Objects;
    1919import java.util.Optional;
     20import java.util.function.BiFunction;
     21import java.util.function.Function;
    2022import java.util.function.Predicate;
    2123import java.util.function.Supplier;
     
    402404        }
    403405
     406        /**
     407         * First applies {@code mapper} to both sides and then applies {@code operator} on the two results.
     408         * @param mapper the mapping function
     409         * @param operator the operator
     410         * @param <T> the type of the intermediate result
     411         * @param <U> the type of the result
     412         * @return {@code operator.apply(mapper.apply(lhs), mapper.apply(rhs))}
     413         */
     414        public <T, U> U map(Function<Match, T> mapper, BiFunction<T, T, U> operator) {
     415            return operator.apply(mapper.apply(lhs), mapper.apply(rhs));
     416        }
     417
    404418        protected static String parenthesis(Match m) {
    405419            return '(' + m.toString() + ')';
     
    562576        @Override
    563577        public String toString() {
    564             return (lhs instanceof AbstractBinaryMatch && !(lhs instanceof And) ? parenthesis(lhs) : lhs) + " && "
    565                  + (rhs instanceof AbstractBinaryMatch && !(rhs instanceof And) ? parenthesis(rhs) : rhs);
     578            return map(m -> m instanceof AbstractBinaryMatch && !(m instanceof And) ? parenthesis(m) : m, (s1, s2) -> s1 + " && " + s2);
    566579        }
    567580    }
     
    592605        @Override
    593606        public String toString() {
    594             return (lhs instanceof AbstractBinaryMatch && !(lhs instanceof Or) ? parenthesis(lhs) : lhs) + " || "
    595                  + (rhs instanceof AbstractBinaryMatch && !(rhs instanceof Or) ? parenthesis(rhs) : rhs);
     607            return map(m -> m instanceof AbstractBinaryMatch && !(m instanceof Or) ? parenthesis(m) : m, (s1, s2) -> s1 + " || " + s2);
    596608        }
    597609    }
     
    622634        @Override
    623635        public String toString() {
    624             return (lhs instanceof AbstractBinaryMatch && !(lhs instanceof Xor) ? parenthesis(lhs) : lhs) + " ^ "
    625                  + (rhs instanceof AbstractBinaryMatch && !(rhs instanceof Xor) ? parenthesis(rhs) : rhs);
     636            return map(m -> m instanceof AbstractBinaryMatch && !(m instanceof Xor) ? parenthesis(m) : m, (s1, s2) -> s1 + " ^ " + s2);
    626637        }
    627638    }
Note: See TracChangeset for help on using the changeset viewer.