001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.mappaint.xml;
003
004import java.util.Collection;
005
006import org.openstreetmap.josm.data.osm.OsmPrimitive;
007import org.openstreetmap.josm.data.osm.OsmUtils;
008import org.openstreetmap.josm.gui.mappaint.Range;
009
010public abstract class Prototype {
011    // zoom range to display the feature
012    public Range range;
013
014    public int priority;
015    public String code;
016    public Collection<XmlCondition> conditions;
017
018    public Prototype(Range range) {
019        this.range = range;
020    }
021
022    /**
023     * Constructs a new {@code Prototype}.
024     */
025    public Prototype() {
026        // Allows subclassing
027    }
028
029    public String getCode() {
030        if (code == null) {
031            if (conditions == null || conditions.isEmpty()) {
032                code = "";
033            } else {
034                final StringBuilder sb = new StringBuilder();
035                for (XmlCondition r: conditions) {
036                    r.appendCode(sb);
037                }
038                code = sb.toString();
039            }
040        }
041        return code;
042    }
043
044    public boolean check(OsmPrimitive primitive) {
045        if (conditions == null)
046            return true;
047        for (XmlCondition r : conditions) {
048            String k = primitive.get(r.key);
049
050            if (k == null || (r.value != null && !k.equals(r.value)))
051                return false;
052
053            String bv = OsmUtils.getNamedOsmBoolean(r.boolValue);
054
055            if (bv != null && !bv.equals(OsmUtils.getNamedOsmBoolean(k)))
056                return false;
057        }
058        return true;
059    }
060}