001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint.mapcss;
003
004import java.util.List;
005import java.util.Objects;
006
007import org.openstreetmap.josm.gui.mappaint.Environment;
008import org.openstreetmap.josm.gui.mappaint.StyleSource;
009import org.openstreetmap.josm.tools.Utils;
010
011/**
012 * A declaration is a list of {@link Instruction}s
013 */
014public class Declaration {
015    /**
016     * The instructions in this declaration
017     */
018    public final List<Instruction> instructions;
019    /**
020     * The index of this declaration
021     * <p>
022     * declarations in the StyleSource are numbered consecutively
023     */
024    public final int idx;
025
026    /**
027     * Create a new {@link Declaration}
028     * @param instructions The instructions for this declaration
029     * @param idx The index in the {@link StyleSource}
030     */
031    public Declaration(List<Instruction> instructions, int idx) {
032        this.instructions = Utils.toUnmodifiableList(instructions);
033        this.idx = idx;
034    }
035
036    /**
037     * <p>Executes the instructions against the environment {@code env}</p>
038     *
039     * @param env the environment
040     */
041    public void execute(Environment env) {
042        for (Instruction i : instructions) {
043            i.execute(env);
044        }
045    }
046
047    @Override
048    public int hashCode() {
049        return Objects.hash(instructions, idx);
050    }
051
052    @Override
053    public boolean equals(Object obj) {
054        if (this == obj) return true;
055        if (obj == null || getClass() != obj.getClass()) return false;
056        Declaration that = (Declaration) obj;
057        return idx == that.idx &&
058                Objects.equals(instructions, that.instructions);
059    }
060
061    @Override
062    public String toString() {
063        return "Declaration [instructions=" + instructions + ", idx=" + idx + ']';
064    }
065}