001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.mappaint.mapcss; 003 004import java.awt.Color; 005import java.lang.annotation.ElementType; 006import java.lang.annotation.Retention; 007import java.lang.annotation.RetentionPolicy; 008import java.lang.annotation.Target; 009import java.lang.reflect.Array; 010import java.util.Arrays; 011import java.util.Collection; 012import java.util.Collections; 013import java.util.HashMap; 014import java.util.List; 015import java.util.Map; 016import java.util.Objects; 017import java.util.function.BiFunction; 018import java.util.function.DoubleBinaryOperator; 019import java.util.function.DoubleUnaryOperator; 020import java.util.function.Function; 021 022import org.openstreetmap.josm.data.osm.PrimitiveId; 023import org.openstreetmap.josm.gui.mappaint.Cascade; 024import org.openstreetmap.josm.gui.mappaint.Environment; 025import org.openstreetmap.josm.tools.SubclassFilteredCollection; 026import org.openstreetmap.josm.tools.Utils; 027 028/** 029 * Factory to generate {@link Expression}s. 030 * <p> 031 * See {@link #createFunctionExpression}. 032 */ 033public final class ExpressionFactory { 034 035 /** 036 * Marks functions which should be executed also when one or more arguments are null. 037 */ 038 @Target(ElementType.METHOD) 039 @Retention(RetentionPolicy.RUNTIME) 040 @interface NullableArguments {} 041 042 /** 043 * Represents a function that accepts three arguments and produces a result. This is a specialization of {@link Function}. 044 * This is a functional interface whose functional method is {@link #apply(Object, Object, Object)}. 045 * @param <T> The type of the first argument 046 * @param <U> The type of the second argument 047 * @param <V> The type of the third argument 048 * @param <R> The type of the result of the function 049 * @see Function 050 * @see BiFunction 051 */ 052 @FunctionalInterface 053 public interface TriFunction<T, U, V, R> { 054 /** 055 * Call the function 056 * @param t The first argument 057 * @param u The second argument 058 * @param v The third argument 059 * @return The result of the function call 060 */ 061 R apply(T t, U u, V v); 062 } 063 064 /** 065 * Represents a function that accepts four arguments and produces a result. This is a specialization of {@link Function}. 066 * This is a functional interface whose functional method is {@link #apply(Object, Object, Object, Object)}. 067 * @param <T> The type of the first argument 068 * @param <U> The type of the second argument 069 * @param <V> The type of the third argument 070 * @param <W> The type of the fourth argument 071 * @param <R> The type of the result of the function 072 * @see Function 073 * @see BiFunction 074 */ 075 @FunctionalInterface 076 public interface QuadFunction<T, U, V, W, R> { 077 /** 078 * Call the function 079 * @param t The first argument 080 * @param u The second argument 081 * @param v The third argument 082 * @param w The fourth argument 083 * @return The result of the function call 084 */ 085 R apply(T t, U u, V v, W w); 086 } 087 088 @FunctionalInterface 089 interface Factory { 090 Expression createExpression(List<Expression> args); 091 092 static Factory of(DoubleUnaryOperator operator) { 093 return of(Double.class, operator::applyAsDouble); 094 } 095 096 static Factory ofNumberVarArgs(double identity, DoubleUnaryOperator unaryOperator, DoubleBinaryOperator operator) { 097 return args -> env -> { 098 if (args.isEmpty()) { 099 return identity; 100 } else if (args.size() == 1) { 101 Double arg = Cascade.convertTo(args.get(0).evaluate(env), Double.class); 102 return arg == null ? null : unaryOperator.applyAsDouble(arg); 103 } else { 104 return args.stream() 105 .map(arg -> Cascade.convertTo(arg.evaluate(env), Double.class)) 106 .filter(Objects::nonNull) 107 .reduce(operator::applyAsDouble).orElse(null); 108 } 109 }; 110 } 111 112 static Factory ofStringVarargs(BiFunction<Environment, String[], ?> function) { 113 return args -> env -> function.apply(env, args.stream() 114 .map(arg -> Cascade.convertTo(arg.evaluate(env), String.class)) 115 .toArray(String[]::new)); 116 } 117 118 static Factory ofObjectVarargs(BiFunction<Environment, Object[], ?> function) { 119 return args -> env -> function.apply(env, args.stream() 120 .map(arg -> arg.evaluate(env)) 121 .toArray(Object[]::new)); 122 } 123 124 static <T> Factory of(Class<T> type, Function<T, ?> function) { 125 return args -> env -> { 126 T v = Cascade.convertTo(args.get(0).evaluate(env), type); 127 return v == null ? null : function.apply(v); 128 }; 129 } 130 131 static <T, U> Factory of(Class<T> type1, Class<U> type2, BiFunction<T, U, ?> function) { 132 return args -> env -> { 133 T v1 = Cascade.convertTo(args.get(0).evaluate(env), type1); 134 U v2 = Cascade.convertTo(args.get(1).evaluate(env), type2); 135 return v1 == null || v2 == null ? null : function.apply(v1, v2); 136 }; 137 } 138 139 static <T, U, V> Factory of(Class<T> type1, Class<U> type2, Class<V> type3, 140 BiFunction<T, U, ?> biFunction, TriFunction<T, U, V, ?> triFunction) { 141 return args -> env -> { 142 T v1 = !args.isEmpty() ? Cascade.convertTo(args.get(0).evaluate(env), type1) : null; 143 U v2 = args.size() >= 2 ? Cascade.convertTo(args.get(1).evaluate(env), type2) : null; 144 V v3 = args.size() >= 3 ? Cascade.convertTo(args.get(2).evaluate(env), type3) : null; 145 return v1 == null || v2 == null ? null : v3 == null ? biFunction.apply(v1, v2) : triFunction.apply(v1, v2, v3); 146 }; 147 } 148 149 static <T, U, V, W> Factory of(Class<T> type1, Class<U> type2, Class<V> type3, Class<W> type4, 150 QuadFunction<T, U, V, W, ?> function) { 151 return args -> env -> { 152 T v1 = !args.isEmpty() ? Cascade.convertTo(args.get(0).evaluate(env), type1) : null; 153 U v2 = args.size() >= 2 ? Cascade.convertTo(args.get(1).evaluate(env), type2) : null; 154 V v3 = args.size() >= 3 ? Cascade.convertTo(args.get(2).evaluate(env), type3) : null; 155 W v4 = args.size() >= 4 ? Cascade.convertTo(args.get(3).evaluate(env), type4) : null; 156 return v1 == null || v2 == null || v3 == null || v4 == null ? null : function.apply(v1, v2, v3, v4); 157 }; 158 } 159 160 /** 161 * Create a factory that accepts an iterable (array <i>or</i> generic iterable) 162 * @param type The expected type class 163 * @param function The function to apply the arguments to 164 * @param <T> The iterable type 165 * @return The result of the function call 166 */ 167 @SuppressWarnings("unchecked") 168 static <T> Factory ofIterable(Class<T> type, Function<Iterable<T>, ?> function) { 169 return args -> env -> { 170 Object arg0 = args.get(0).evaluate(env); 171 if (args.size() == 1 && arg0 instanceof Iterable) { 172 return function.apply((Iterable<T>) arg0); 173 } else { 174 return function.apply(Arrays.asList(args.stream().map(arg -> Cascade.convertTo(arg, type)) 175 .toArray(length -> (T[]) Array.newInstance(type, length)))); 176 } 177 }; 178 } 179 180 /** 181 * Create a {@link Factory} for a function 182 * @param function The function to use 183 * @return The result of the function 184 */ 185 static Factory ofEnv(Function<Environment, ?> function) { 186 return args -> function::apply; 187 } 188 189 /** 190 * Create a {@link Factory} for a function that takes a parameter 191 * @param type The parameter type class 192 * @param function The function to use when one argument is available 193 * @param <T> the type of the input to the function 194 * @return The result of the function 195 */ 196 static <T> Factory ofEnv(Class<T> type, BiFunction<Environment, T, ?> function) { 197 return args -> env -> { 198 T v = Cascade.convertTo(args.get(0).evaluate(env), type); 199 return v == null ? null : function.apply(env, v); 200 }; 201 } 202 203 /** 204 * Create a {@link Factory} for an overloaded function 205 * @param type1 The first parameter type class 206 * @param type2 The second parameter type class 207 * @param biFunction The function to use when one argument is available 208 * @param triFunction The function to use when two arguments are available 209 * @param <T> the type of the input to the function 210 * @param <U> the type of the input to the function 211 * @return The result of one of the functions 212 */ 213 static <T, U> Factory ofEnv(Class<T> type1, Class<U> type2, 214 BiFunction<Environment, T, ?> biFunction, TriFunction<Environment, T, U, ?> triFunction) { 215 return args -> env -> { 216 T v1 = !args.isEmpty() ? Cascade.convertTo(args.get(0).evaluate(env), type1) : null; 217 U v2 = args.size() >= 2 ? Cascade.convertTo(args.get(1).evaluate(env), type2) : null; 218 return v1 == null ? null : v2 == null ? biFunction.apply(env, v1) : triFunction.apply(env, v1, v2); 219 }; 220 } 221 222 /** 223 * Create a {@link Factory} for an overloaded function 224 * @param type1 The first parameter type class 225 * @param type2 The second parameter type class 226 * @param function The function to use when no args are available 227 * @param biFunction The function to use when one argument is available 228 * @param triFunction The function to use when two arguments are available 229 * @param <T> the type of the input to the function 230 * @param <U> the type of the input to the function 231 * @return The result of one of the functions 232 */ 233 static <T, U> Factory ofEnv(Class<T> type1, Class<U> type2, Function<Environment, ?> function, 234 BiFunction<Environment, T, ?> biFunction, TriFunction<Environment, T, U, ?> triFunction) { 235 return args -> env -> { 236 T v1 = !args.isEmpty() ? Cascade.convertTo(args.get(0).evaluate(env), type1) : null; 237 U v2 = args.size() >= 2 ? Cascade.convertTo(args.get(1).evaluate(env), type2) : null; 238 return v1 == null ? function.apply(env) : v2 == null ? biFunction.apply(env, v1) : triFunction.apply(env, v1, v2); 239 }; 240 } 241 } 242 243 static final Map<String, Factory> FACTORY_MAP = new HashMap<>(); 244 245 static { 246 initFactories(); 247 } 248 249 @SuppressWarnings("unchecked") 250 private static void initFactories() { 251 FACTORY_MAP.put("CRC32_checksum", Factory.of(String.class, Functions::CRC32_checksum)); 252 FACTORY_MAP.put("JOSM_pref", Factory.ofEnv(String.class, String.class, null, Functions::JOSM_pref)); 253 FACTORY_MAP.put("JOSM_search", Factory.ofEnv(String.class, Functions::JOSM_search)); 254 FACTORY_MAP.put("URL_decode", Factory.of(String.class, Functions::URL_decode)); 255 FACTORY_MAP.put("URL_encode", Factory.of(String.class, Functions::URL_encode)); 256 FACTORY_MAP.put("XML_encode", Factory.of(String.class, Functions::XML_encode)); 257 FACTORY_MAP.put("siunit_length", Factory.of(String.class, Functions::siunit_length)); 258 FACTORY_MAP.put("abs", Factory.of(Math::acos)); 259 FACTORY_MAP.put("acos", Factory.of(Math::acos)); 260 FACTORY_MAP.put("alpha", Factory.of(Color.class, Functions::alpha)); 261 FACTORY_MAP.put("any", Factory.ofObjectVarargs(Functions::any)); 262 FACTORY_MAP.put("areasize", Factory.ofEnv(Functions::areasize)); 263 FACTORY_MAP.put("asin", Factory.of(Math::asin)); 264 FACTORY_MAP.put("at", Factory.ofEnv(double.class, double.class, null, Functions::at)); 265 FACTORY_MAP.put("atan", Factory.of(Math::atan)); 266 FACTORY_MAP.put("atan2", Factory.of(Double.class, Double.class, Math::atan2)); 267 FACTORY_MAP.put("blue", Factory.of(Color.class, Functions::blue)); 268 FACTORY_MAP.put("cardinal_to_radians", Factory.of(String.class, Functions::cardinal_to_radians)); 269 FACTORY_MAP.put("ceil", Factory.of(Math::ceil)); 270 FACTORY_MAP.put("center", Factory.ofEnv(Functions::center)); 271 FACTORY_MAP.put("child_tag", Factory.ofEnv(String.class, Functions::child_tag)); 272 FACTORY_MAP.put("color2html", Factory.of(Color.class, Functions::color2html)); 273 FACTORY_MAP.put("concat", Factory.ofObjectVarargs(Functions::concat)); 274 FACTORY_MAP.put("convert_primitive_to_string", Factory.of(PrimitiveId.class, Functions::convert_primitive_to_string)); 275 FACTORY_MAP.put("convert_primitives_to_string", Factory.ofIterable(PrimitiveId.class, Functions::convert_primitives_to_string)); 276 FACTORY_MAP.put("cos", Factory.of(Math::cos)); 277 FACTORY_MAP.put("cosh", Factory.of(Math::cosh)); 278 FACTORY_MAP.put("count", Factory.of(List.class, Functions::count)); 279 FACTORY_MAP.put("count_roles", Factory.ofStringVarargs(Functions::count_roles)); 280 FACTORY_MAP.put("degree_to_radians", Factory.of(Functions::degree_to_radians)); 281 FACTORY_MAP.put("divided_by", Factory.ofNumberVarArgs(1.0, DoubleUnaryOperator.identity(), Functions::divided_by)); 282 FACTORY_MAP.put("equal", Factory.of(Object.class, Object.class, Functions::equal)); 283 FACTORY_MAP.put("eval", Factory.of(Object.class, Functions::eval)); 284 FACTORY_MAP.put("exp", Factory.of(Math::exp)); 285 FACTORY_MAP.put("floor", Factory.of(Math::floor)); 286 FACTORY_MAP.put("get", Factory.of(List.class, float.class, Functions::get)); 287 FACTORY_MAP.put("gpx_distance", Factory.ofEnv(Functions::gpx_distance)); 288 FACTORY_MAP.put("greater", Factory.of(float.class, float.class, Functions::greater)); 289 FACTORY_MAP.put("greater_equal", Factory.of(float.class, float.class, Functions::greater_equal)); 290 FACTORY_MAP.put("green", Factory.of(Color.class, Functions::green)); 291 FACTORY_MAP.put("has_tag_key", Factory.ofEnv(String.class, Functions::has_tag_key)); 292 FACTORY_MAP.put("hsb_color", Factory.of(float.class, float.class, float.class, null, Functions::hsb_color)); 293 FACTORY_MAP.put("html2color", Factory.of(String.class, Functions::html2color)); 294 FACTORY_MAP.put("index", Factory.ofEnv(Functions::index)); 295 FACTORY_MAP.put("inside", Factory.ofEnv(String.class, Functions::inside)); 296 FACTORY_MAP.put("is_anticlockwise", Factory.ofEnv(Functions::is_anticlockwise)); 297 FACTORY_MAP.put("is_clockwise", Factory.ofEnv(Functions::is_clockwise)); 298 FACTORY_MAP.put("is_prop_set", Factory.ofEnv(String.class, String.class, Functions::is_prop_set, Functions::is_prop_set)); 299 FACTORY_MAP.put("is_right_hand_traffic", Factory.ofEnv(Functions::is_right_hand_traffic)); 300 FACTORY_MAP.put("is_similar", Factory.of(String.class, String.class, Functions::is_similar)); 301 FACTORY_MAP.put("join", Factory.ofStringVarargs(Functions::join)); 302 FACTORY_MAP.put("join_list", Factory.of(String.class, List.class, Functions::join_list)); 303 FACTORY_MAP.put("less", Factory.of(float.class, float.class, Functions::less)); 304 FACTORY_MAP.put("less_equal", Factory.of(float.class, float.class, Functions::less_equal)); 305 FACTORY_MAP.put("list", Factory.ofObjectVarargs(Functions::list)); 306 FACTORY_MAP.put("log", Factory.of(Math::log)); 307 FACTORY_MAP.put("lower", Factory.of(String.class, Functions::lower)); 308 FACTORY_MAP.put("minus", Factory.ofNumberVarArgs(0.0, v -> -v, Functions::minus)); 309 FACTORY_MAP.put("mod", Factory.of(float.class, float.class, Functions::mod)); 310 FACTORY_MAP.put("not", Factory.of(boolean.class, Functions::not)); 311 FACTORY_MAP.put("not_equal", Factory.of(Object.class, Object.class, Functions::not_equal)); 312 FACTORY_MAP.put("number_of_tags", Factory.ofEnv(Functions::number_of_tags)); 313 FACTORY_MAP.put("osm_changeset_id", Factory.ofEnv(Functions::osm_changeset_id)); 314 FACTORY_MAP.put("osm_id", Factory.ofEnv(Functions::osm_id)); 315 FACTORY_MAP.put("osm_timestamp", Factory.ofEnv(Functions::osm_timestamp)); 316 FACTORY_MAP.put("osm_user_id", Factory.ofEnv(Functions::osm_user_id)); 317 FACTORY_MAP.put("osm_user_name", Factory.ofEnv(Functions::osm_user_name)); 318 FACTORY_MAP.put("osm_version", Factory.ofEnv(Functions::osm_version)); 319 FACTORY_MAP.put("outside", Factory.ofEnv(String.class, Functions::outside)); 320 FACTORY_MAP.put("parent_osm_id", Factory.ofEnv(Functions::parent_osm_id)); 321 FACTORY_MAP.put("parent_osm_primitives", Factory.ofEnv(String.class, String.class, 322 Functions::parent_osm_primitives, Functions::parent_osm_primitives, Functions::parent_osm_primitives)); 323 FACTORY_MAP.put("parent_tag", Factory.ofEnv(String.class, Functions::parent_tag)); 324 FACTORY_MAP.put("parent_tags", Factory.ofEnv(String.class, Functions::parent_tags)); 325 FACTORY_MAP.put("parent_way_angle", Factory.ofEnv(Functions::parent_way_angle)); 326 FACTORY_MAP.put("plus", Factory.ofNumberVarArgs(0.0, DoubleUnaryOperator.identity(), Functions::plus)); 327 FACTORY_MAP.put("print", Factory.of(Object.class, Functions::print)); 328 FACTORY_MAP.put("println", Factory.of(Object.class, Functions::println)); 329 FACTORY_MAP.put("prop", Factory.ofEnv(String.class, String.class, Functions::prop, Functions::prop)); 330 FACTORY_MAP.put("red", Factory.of(Color.class, Functions::red)); 331 FACTORY_MAP.put("regexp_match", Factory.of(String.class, String.class, String.class, Functions::regexp_match, Functions::regexp_match)); 332 FACTORY_MAP.put("regexp_test", Factory.of(String.class, String.class, String.class, Functions::regexp_test, Functions::regexp_test)); 333 FACTORY_MAP.put("replace", Factory.of(String.class, String.class, String.class, null, Functions::replace)); 334 FACTORY_MAP.put("rgb", Factory.of(float.class, float.class, float.class, null, Functions::rgb)); 335 FACTORY_MAP.put("rgba", Factory.of(float.class, float.class, float.class, float.class, Functions::rgba)); 336 FACTORY_MAP.put("role", Factory.ofEnv(Functions::role)); 337 FACTORY_MAP.put("round", Factory.of(Math::round)); 338 FACTORY_MAP.put("setting", Factory.ofEnv(String.class, Functions::setting)); 339 FACTORY_MAP.put("signum", Factory.of(Math::signum)); 340 FACTORY_MAP.put("sin", Factory.of(Math::sin)); 341 FACTORY_MAP.put("sinh", Factory.of(Math::sinh)); 342 FACTORY_MAP.put("sort", Factory.ofStringVarargs(Functions::sort)); 343 FACTORY_MAP.put("sort_list", Factory.of(List.class, Functions::sort_list)); 344 FACTORY_MAP.put("split", Factory.of(String.class, String.class, Functions::split)); 345 FACTORY_MAP.put("sqrt", Factory.of(Math::sqrt)); 346 FACTORY_MAP.put("substring", Factory.of(String.class, float.class, float.class, Functions::substring, Functions::substring)); 347 FACTORY_MAP.put("tag", Factory.ofEnv(String.class, Functions::tag)); 348 FACTORY_MAP.put("tag_regex", Factory.ofEnv(String.class, String.class, Functions::tag_regex, Functions::tag_regex)); 349 FACTORY_MAP.put("tan", Factory.of(Math::tan)); 350 FACTORY_MAP.put("tanh", Factory.of(Math::tanh)); 351 FACTORY_MAP.put("times", Factory.ofNumberVarArgs(1.0, DoubleUnaryOperator.identity(), Functions::times)); 352 FACTORY_MAP.put("title", Factory.of(String.class, Functions::title)); 353 FACTORY_MAP.put("to_boolean", Factory.of(String.class, Functions::to_boolean)); 354 FACTORY_MAP.put("to_byte", Factory.of(String.class, Functions::to_byte)); 355 FACTORY_MAP.put("to_double", Factory.of(String.class, Functions::to_double)); 356 FACTORY_MAP.put("to_float", Factory.of(String.class, Functions::to_float)); 357 FACTORY_MAP.put("to_int", Factory.of(String.class, Functions::to_int)); 358 FACTORY_MAP.put("to_long", Factory.of(String.class, Functions::to_long)); 359 FACTORY_MAP.put("to_short", Factory.of(String.class, Functions::to_short)); 360 FACTORY_MAP.put("tr", Factory.ofStringVarargs(Functions::tr)); 361 FACTORY_MAP.put("trim", Factory.of(String.class, Functions::trim)); 362 FACTORY_MAP.put("trim_list", Factory.of(List.class, Functions::trim_list)); 363 FACTORY_MAP.put("uniq", Factory.ofStringVarargs(Functions::uniq)); 364 FACTORY_MAP.put("uniq_list", Factory.of(List.class, Functions::uniq_list)); 365 FACTORY_MAP.put("upper", Factory.of(String.class, Functions::upper)); 366 FACTORY_MAP.put("waylength", Factory.ofEnv(Functions::waylength)); 367 } 368 369 private ExpressionFactory() { 370 // Hide default constructor for utils classes 371 } 372 373 /** 374 * Main method to create an function-like expression. 375 * 376 * @param name the name of the function or operator 377 * @param args the list of arguments (as expressions) 378 * @return the generated Expression. If no suitable function can be found, 379 * returns {@link NullExpression#INSTANCE}. 380 */ 381 public static Expression createFunctionExpression(String name, List<Expression> args) { 382 if ("cond".equals(name) && args.size() == 3) 383 return new CondOperator(args.get(0), args.get(1), args.get(2)); 384 else if ("and".equals(name)) 385 return new AndOperator(args); 386 else if ("or".equals(name)) 387 return new OrOperator(args); 388 else if ("length".equals(name) && args.size() == 1) 389 return new LengthFunction(args.get(0)); 390 else if ("max".equals(name) && !args.isEmpty()) 391 return new MinMaxFunction(args, true); 392 else if ("min".equals(name) && !args.isEmpty()) 393 return new MinMaxFunction(args, false); 394 else if ("inside".equals(name) && args.size() == 1) 395 return new IsInsideFunction(args.get(0)); 396 else if ("random".equals(name)) 397 return env -> Math.random(); 398 399 Factory factory = FACTORY_MAP.get(name); 400 if (factory != null) { 401 return factory.createExpression(args); 402 } 403 return NullExpression.INSTANCE; 404 } 405 406 /** 407 * Expression that always evaluates to null. 408 */ 409 public static class NullExpression implements Expression { 410 411 /** 412 * The unique instance. 413 */ 414 public static final NullExpression INSTANCE = new NullExpression(); 415 416 @Override 417 public Object evaluate(Environment env) { 418 return null; 419 } 420 } 421 422 /** 423 * Conditional operator. 424 */ 425 public static class CondOperator implements Expression { 426 427 private final Expression condition, firstOption, secondOption; 428 429 /** 430 * Constructs a new {@code CondOperator}. 431 * @param condition condition 432 * @param firstOption first option 433 * @param secondOption second option 434 */ 435 public CondOperator(Expression condition, Expression firstOption, Expression secondOption) { 436 this.condition = condition; 437 this.firstOption = firstOption; 438 this.secondOption = secondOption; 439 } 440 441 @Override 442 public Object evaluate(Environment env) { 443 Boolean b = Cascade.convertTo(condition.evaluate(env), boolean.class); 444 if (b != null && b) 445 return firstOption.evaluate(env); 446 else 447 return secondOption.evaluate(env); 448 } 449 } 450 451 /** 452 * "And" logical operator. 453 */ 454 public static class AndOperator implements Expression { 455 456 private final List<Expression> args; 457 458 /** 459 * Constructs a new {@code AndOperator}. 460 * @param args arguments 461 */ 462 public AndOperator(List<Expression> args) { 463 this.args = args; 464 } 465 466 @Override 467 public Object evaluate(Environment env) { 468 return args.stream() 469 .map(arg -> Cascade.convertTo(arg.evaluate(env), boolean.class)) 470 .allMatch(Boolean.TRUE::equals); 471 } 472 } 473 474 /** 475 * "Or" logical operator. 476 */ 477 public static class OrOperator implements Expression { 478 479 private final List<Expression> args; 480 481 /** 482 * Constructs a new {@code OrOperator}. 483 * @param args arguments 484 */ 485 public OrOperator(List<Expression> args) { 486 this.args = args; 487 } 488 489 @Override 490 public Object evaluate(Environment env) { 491 return args.stream() 492 .map(arg -> Cascade.convertTo(arg.evaluate(env), boolean.class)) 493 .anyMatch(Boolean.TRUE::equals); 494 } 495 } 496 497 /** 498 * Function to calculate the length of a string or list in a MapCSS eval expression. 499 * <p> 500 * Separate implementation to support overloading for different argument types. 501 * <p> 502 * The use for calculating the length of a list is deprecated, use 503 * {@link Functions#count(java.util.List)} instead (see #10061). 504 */ 505 public static class LengthFunction implements Expression { 506 507 private final Expression arg; 508 509 /** 510 * Constructs a new {@code LengthFunction}. 511 * @param args arguments 512 */ 513 public LengthFunction(Expression args) { 514 this.arg = args; 515 } 516 517 @Override 518 public Object evaluate(Environment env) { 519 List<?> l = Cascade.convertTo(arg.evaluate(env), List.class); 520 if (l != null) 521 return l.size(); 522 String s = Cascade.convertTo(arg.evaluate(env), String.class); 523 if (s != null) 524 return s.length(); 525 return null; 526 } 527 } 528 529 /** 530 * Computes the maximum/minimum value an arbitrary number of floats, or a list of floats. 531 */ 532 public static class MinMaxFunction implements Expression { 533 534 private final List<Expression> args; 535 private final boolean computeMax; 536 537 /** 538 * Constructs a new {@code MinMaxFunction}. 539 * @param args arguments 540 * @param computeMax if {@code true}, compute max. If {@code false}, compute min 541 */ 542 public MinMaxFunction(final List<Expression> args, final boolean computeMax) { 543 this.args = args; 544 this.computeMax = computeMax; 545 } 546 547 /** 548 * Compute the minimum / maximum over the list 549 * @param lst The list 550 * @return The minimum or maximum depending on {@link #computeMax} 551 */ 552 public Float aggregateList(List<?> lst) { 553 final List<Float> floats = Utils.transform(lst, (Function<Object, Float>) x -> Cascade.convertTo(x, float.class)); 554 final Collection<Float> nonNullList = SubclassFilteredCollection.filter(floats, Objects::nonNull); 555 return nonNullList.isEmpty() ? (Float) Float.NaN : computeMax ? Collections.max(nonNullList) : Collections.min(nonNullList); 556 } 557 558 @Override 559 public Object evaluate(final Environment env) { 560 List<?> l = Cascade.convertTo(args.get(0).evaluate(env), List.class); 561 if (args.size() != 1 || l == null) 562 l = Utils.transform(args, (Function<Expression, Object>) x -> x.evaluate(env)); 563 return aggregateList(l); 564 } 565 } 566 567 /** 568 * {@code Functions#inside} implementation for use in {@link org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker} 569 * 570 * @see Functions#inside 571 */ 572 public static class IsInsideFunction implements Expression { 573 private final Expression arg; 574 575 /** 576 * Constructs a new {@code IsInsideFunction}. 577 * @param arg argument 578 */ 579 public IsInsideFunction(Expression arg) { 580 this.arg = arg; 581 } 582 583 /** 584 * Returns the argument 585 * @return the argument 586 */ 587 public Expression getArg() { 588 return arg; 589 } 590 591 @Override 592 public Object evaluate(Environment env) { 593 String codes = Cascade.convertTo(arg.evaluate(env), String.class); 594 return Functions.inside(env, codes); 595 } 596 } 597}