2 * Copyright (c) 2010-2020 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.lutron.internal.keypadconfig;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.LinkedList;
18 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.lutron.internal.KeypadComponent;
23 import org.openhab.binding.lutron.internal.discovery.project.ComponentType;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Abstract base class for keypad configuration definition classes
30 * @author Bob Adair - Initial contribution
33 public abstract class KeypadConfig {
34 private final Logger logger = LoggerFactory.getLogger(KeypadConfig.class);
36 protected final HashMap<String, @Nullable List<KeypadComponent>> modelData = new HashMap<>();
38 public abstract boolean isCCI(int id);
40 public abstract boolean isButton(int id);
42 public abstract boolean isLed(int id);
45 * Get a list of all {@link KeypadComponent}s for the specified keypad model
47 * @param model The keypad model for which to return components.
48 * @return List of components. Will be empty if no components match.
50 public List<KeypadComponent> getComponents(String model) {
51 return getComponents(model, null);
55 * Get a list of {@link KeypadComponent}s of the specified type for the specified keypad model
57 * @param model The keypad model for which to return components.
58 * @param type The component type to include, or null for all components.
59 * @return List of components. Will be empty if no components match.
61 public List<KeypadComponent> getComponents(String model, @Nullable ComponentType type) {
62 List<KeypadComponent> filteredList = new LinkedList<>();
63 List<KeypadComponent> cList = modelData.get(model);
65 logger.debug("Keypad components lookup using invalid keypad model: {}", model);
67 } else if (type == null) {
70 for (KeypadComponent i : cList) {
71 if (i.type() == type) {
80 * Get a list of all component IDs for the specified keypad model
82 * @param model The keypad model for which to return component IDs.
83 * @return List of component IDs. Will be empty if no components match.
85 public @Nullable List<Integer> getComponentIds(String model) {
86 return getComponentIds(model, null);
90 * Get a list of component IDs of the specified type for the specified keypad model
92 * @param model The keypad model for which to return component IDs.
93 * @param type The component type to include, or null for all components.
94 * @return List of component IDs. Will be empty if no components match.
96 public List<Integer> getComponentIds(String model, @Nullable ComponentType type) {
97 List<Integer> idList = new LinkedList<>();
98 List<KeypadComponent> cList = modelData.get(model);
100 logger.debug("Keypad component IDs lookup using invalid keypad model: {}", model);
102 for (KeypadComponent i : cList) {
103 if (type == null || i.type() == type) {
112 * Determine keypad model from list of button component IDs
114 * @param buttonIds List of button component IDs for a keypad. Must be in ascending order.
115 * @return String containing the keypad model, or null if no models match.
117 public @Nullable String determineModelFromComponentIds(List<Integer> buttonIds) {
118 for (String k : modelData.keySet()) {
119 List<Integer> modelButtonIds = getComponentIds(k, ComponentType.BUTTON);
120 Collections.sort(modelButtonIds); // make sure button IDs are in ascending order for comparison
121 if (modelButtonIds.equals(buttonIds)) {
129 * Utility routine to concatenate multiple lists of {@link KeypadComponent}s
131 * @param lists Lists to concatenate
132 * @return Concatenated list
135 protected static final List<KeypadComponent> combinedList(final List<KeypadComponent>... lists) {
136 List<KeypadComponent> newlist = new LinkedList<>();
137 for (List<KeypadComponent> list : lists) {
138 newlist.addAll(list);