2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.alarmdecoder.internal.protocol;
15 import java.util.HashMap;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
24 * The {@link IntCommandMap} class contains an integer to command map used by the keypad intcommand channel.
26 * @author Bob Adair - Initial contribution
29 public class IntCommandMap {
30 private static final Pattern VALID_COMMAND_PATTERN = Pattern.compile(ADCommand.KEYPAD_COMMAND_REGEX);
32 private final Map<Integer, String> commandMap;
34 public IntCommandMap(String mappingString) throws IllegalArgumentException {
35 commandMap = new HashMap<>();
37 String mstring = mappingString.replace("POUND", "#");
38 String[] elements = mstring.split(",");
39 for (String element : elements) {
40 String[] kvPair = element.split("=");
41 if (kvPair.length != 2) {
42 throw new IllegalArgumentException("Invalid key-value pair format");
45 Matcher matcher = VALID_COMMAND_PATTERN.matcher(kvPair[1]);
46 if (!matcher.matches()) {
47 throw new IllegalArgumentException("Invalid command characters in mapping");
51 commandMap.put(Integer.parseInt(kvPair[0]), kvPair[1]);
52 } catch (NumberFormatException e) {
53 throw new IllegalArgumentException("Unable to parse integer in mapping", e);
59 public String getCommand(int key) {
60 return commandMap.get(key);
64 return commandMap.size();