001// License: GPL. See LICENSE file for details.
002package org.openstreetmap.josm.actions.mapmode;
003
004/**
005 * TODO: rewrite to use awt modifers flag instead.
006 *
007 * @author Ole Jørgen Brønner (olejorgenb)
008 */
009public class ModifiersSpec {
010    public static final int ON = 1, OFF = 0, UNKNOWN = 2;
011    public int alt = UNKNOWN;
012    public int shift = UNKNOWN;
013    public int ctrl = UNKNOWN;
014
015    /**
016     *  'A' = Alt, 'S' = Shift, 'C' = Ctrl
017     *  Lowercase signifies off and '?' means unknown/optional.
018     *  Order is Alt, Shift, Ctrl
019     * @param str
020     */
021    public ModifiersSpec(String str) {
022        assert (str.length() == 3);
023        char a = str.charAt(0);
024        char s = str.charAt(1);
025        char c = str.charAt(2);
026        // @formatter:off
027        alt   = (a == '?' ? UNKNOWN : (a == 'A' ? ON : OFF));
028        shift = (s == '?' ? UNKNOWN : (s == 'S' ? ON : OFF));
029        ctrl  = (c == '?' ? UNKNOWN : (c == 'C' ? ON : OFF));
030        // @formatter:on
031    }
032
033    public ModifiersSpec(final int alt, final int shift, final int ctrl) {
034        this.alt = alt;
035        this.shift = shift;
036        this.ctrl = ctrl;
037    }
038
039    public boolean matchWithKnown(final int knownAlt, final int knownShift, final int knownCtrl) {
040        return match(alt, knownAlt) && match(shift, knownShift) && match(ctrl, knownCtrl);
041    }
042
043    public boolean matchWithKnown(final boolean knownAlt, final boolean knownShift, final boolean knownCtrl) {
044        return match(alt, knownAlt) && match(shift, knownShift) && match(ctrl, knownCtrl);
045    }
046
047    private boolean match(final int a, final int knownValue) {
048        assert (knownValue == ON | knownValue == OFF);
049        return a == knownValue || a == UNKNOWN;
050    }
051
052    private boolean match(final int a, final boolean knownValue) {
053        return a == (knownValue ? ON : OFF) || a == UNKNOWN;
054    }
055    // does java have built in 3-state support?
056}