001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.data.imagery.vectortile.mapbox.style; 003 004import java.util.Arrays; 005import java.util.Objects; 006import java.util.stream.Collectors; 007 008import jakarta.json.JsonArray; 009import jakarta.json.JsonObject; 010import jakarta.json.JsonString; 011import jakarta.json.JsonValue; 012 013/** 014 * A Mapbox vector style expression (immutable) 015 * @author Taylor Smock 016 * @see <a href="https://docs.mapbox.com/mapbox-gl-js/style-spec/expressions/">https://docs.mapbox.com/mapbox-gl-js/style-spec/expressions/</a> 017 * @since 17862 018 */ 019public final class Expression { 020 /** An empty expression to use */ 021 public static final Expression EMPTY_EXPRESSION = new Expression(JsonValue.NULL); 022 023 private final String mapcssFilterExpression; 024 025 /** 026 * Create a new filter expression. <i>Please note that this currently only supports basic comparators!</i> 027 * @param value The value to parse 028 */ 029 public Expression(JsonValue value) { 030 if (value.getValueType() == JsonValue.ValueType.ARRAY) { 031 final JsonArray array = value.asJsonArray(); 032 if (!array.isEmpty() && array.get(0).getValueType() == JsonValue.ValueType.STRING) { 033 if ("==".equals(array.getString(0))) { 034 // The mapcss equivalent of == is = (for the most part) 035 this.mapcssFilterExpression = convertToString(array.get(1)) + "=" + convertToString(array.get(2)); 036 } else if (Arrays.asList("<=", ">=", ">", "<", "!=").contains(array.getString(0))) { 037 this.mapcssFilterExpression = convertToString(array.get(1)) + array.getString(0) + convertToString(array.get(2)); 038 } else { 039 this.mapcssFilterExpression = ""; 040 } 041 } else { 042 this.mapcssFilterExpression = ""; 043 } 044 } else { 045 this.mapcssFilterExpression = ""; 046 } 047 } 048 049 /** 050 * Convert a value to a string 051 * @param value The value to convert 052 * @return A string 053 */ 054 private static String convertToString(JsonValue value) { 055 switch (value.getValueType()) { 056 case STRING: 057 return ((JsonString) value).getString(); 058 case FALSE: 059 return Boolean.FALSE.toString(); 060 case TRUE: 061 return Boolean.TRUE.toString(); 062 case NUMBER: 063 return value.toString(); 064 case ARRAY: 065 return '[' 066 + ((JsonArray) value).stream().map(Expression::convertToString).collect(Collectors.joining(",")) 067 + ']'; 068 case OBJECT: 069 return '{' 070 + ((JsonObject) value).entrySet().stream() 071 .map(entry -> entry.getKey() + ":" + convertToString(entry.getValue())).collect( 072 Collectors.joining(",")) 073 + '}'; 074 case NULL: 075 default: 076 return ""; 077 } 078 } 079 080 @Override 081 public String toString() { 082 return !"".equals(this.mapcssFilterExpression) ? '[' + this.mapcssFilterExpression + ']' : ""; 083 } 084 085 @Override 086 public boolean equals(Object other) { 087 if (other instanceof Expression) { 088 Expression o = (Expression) other; 089 return Objects.equals(this.mapcssFilterExpression, o.mapcssFilterExpression); 090 } 091 return false; 092 } 093 094 @Override 095 public int hashCode() { 096 return Objects.hash(this.mapcssFilterExpression); 097 } 098}