001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint.mapcss;
003
004import org.openstreetmap.josm.gui.mappaint.Cascade;
005import org.openstreetmap.josm.gui.mappaint.Environment;
006
007/**
008 * A subpart identifies different rendering layers (<code>::subpart</code> syntax).
009 * @since  8086 (creation)
010 * @since 10600 (functional interface)
011 */
012@FunctionalInterface
013public interface Subpart {
014    String getId(Environment env);
015
016    Subpart DEFAULT_SUBPART = new StringSubpart("default");
017
018    /**
019     * Simple static subpart identifier.
020     *
021     * E.g. ::layer_1
022     */
023    class StringSubpart implements Subpart {
024        private final String id;
025
026        public StringSubpart(String id) {
027            this.id = id;
028        }
029
030        @Override
031        public String getId(Environment env) {
032            return id;
033        }
034
035        @Override
036        public String toString() {
037            return id;
038        }
039    }
040
041    /**
042     * Subpart identifier given by an expression.
043     *
044     * E.g. ::(concat("layer_", prop("i", "default")))
045     */
046    class ExpressionSubpart implements Subpart {
047        private final Expression id;
048
049        public ExpressionSubpart(Expression id) {
050            this.id = id;
051        }
052
053        @Override
054        public String getId(Environment env) {
055            return Cascade.convertTo(id.evaluate(env), String.class);
056        }
057
058        @Override
059        public String toString() {
060            return String.valueOf(id);
061        }
062    }
063}