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    /**
015     * Gets the ID of the suppart
016     * @param env The environment to get it from
017     * @return The id
018     */
019    String getId(Environment env);
020
021    /**
022     * The default subpart for normal rules
023     */
024    Subpart DEFAULT_SUBPART = new StringSubpart("default");
025
026    /**
027     * Simple static subpart identifier.
028     *
029     * E.g. ::layer_1
030     */
031    class StringSubpart implements Subpart {
032        private final String id;
033
034        public StringSubpart(String id) {
035            this.id = id;
036        }
037
038        @Override
039        public String getId(Environment env) {
040            return id;
041        }
042
043        @Override
044        public String toString() {
045            return id;
046        }
047    }
048
049    /**
050     * Subpart identifier given by an expression.
051     *
052     * E.g. ::(concat("layer_", prop("i", "default")))
053     */
054    class ExpressionSubpart implements Subpart {
055        private final Expression id;
056
057        public ExpressionSubpart(Expression id) {
058            this.id = id;
059        }
060
061        @Override
062        public String getId(Environment env) {
063            return Cascade.convertTo(id.evaluate(env), String.class);
064        }
065
066        @Override
067        public String toString() {
068            return String.valueOf(id);
069        }
070    }
071}