001// License: GPL. For details, see LICENSE file.
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 3 letters string defining modifiers
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        // CHECKSTYLE.OFF: SingleSpaceSeparator
028        alt   = a == '?' ? UNKNOWN : (a == 'A' ? ON : OFF);
029        shift = s == '?' ? UNKNOWN : (s == 'S' ? ON : OFF);
030        ctrl  = c == '?' ? UNKNOWN : (c == 'C' ? ON : OFF);
031        // CHECKSTYLE.ON: SingleSpaceSeparator
032        // @formatter:on
033    }
034
035    public ModifiersSpec(final int alt, final int shift, final int ctrl) {
036        this.alt = alt;
037        this.shift = shift;
038        this.ctrl = ctrl;
039    }
040
041    public boolean matchWithKnown(final int knownAlt, final int knownShift, final int knownCtrl) {
042        return match(alt, knownAlt) && match(shift, knownShift) && match(ctrl, knownCtrl);
043    }
044
045    public boolean matchWithKnown(final boolean knownAlt, final boolean knownShift, final boolean knownCtrl) {
046        return match(alt, knownAlt) && match(shift, knownShift) && match(ctrl, knownCtrl);
047    }
048
049    private static boolean match(final int a, final int knownValue) {
050        assert knownValue == ON | knownValue == OFF;
051        return a == knownValue || a == UNKNOWN;
052    }
053
054    private static boolean match(final int a, final boolean knownValue) {
055        return a == (knownValue ? ON : OFF) || a == UNKNOWN;
056    }
057    // does java have built in 3-state support?
058}