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.lutron.internal.keypadconfig;
15 import java.util.Collections;
16 import java.util.HashMap;
17 import java.util.LinkedList;
18 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.lutron.internal.KeypadComponent;
24 import org.openhab.binding.lutron.internal.discovery.project.ComponentType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * Abstract base class for keypad configuration definition classes
31 * @author Bob Adair - Initial contribution
34 public abstract class KeypadConfig {
35 private final Logger logger = LoggerFactory.getLogger(KeypadConfig.class);
37 protected final Map<String, List<KeypadComponent>> modelData = new HashMap<>();
39 public abstract boolean isCCI(int id);
41 public abstract boolean isButton(int id);
43 public abstract boolean isLed(int id);
46 * Get a list of all {@link KeypadComponent}s for the specified keypad model
48 * @param model The keypad model for which to return components.
49 * @return List of components. Will be empty if no components match.
51 public List<KeypadComponent> getComponents(String model) {
52 return getComponents(model, null);
56 * Get a list of {@link KeypadComponent}s of the specified type for the specified keypad model
58 * @param model The keypad model for which to return components.
59 * @param type The component type to include, or null for all components.
60 * @return List of components. Will be empty if no components match.
62 public List<KeypadComponent> getComponents(String model, @Nullable ComponentType type) {
63 List<KeypadComponent> filteredList = new LinkedList<>();
64 List<KeypadComponent> cList = modelData.get(model);
66 logger.debug("Keypad components lookup using invalid keypad model: {}", model);
68 } else if (type == null) {
71 for (KeypadComponent i : cList) {
72 if (i.type() == type) {
81 * Get a list of all component IDs for the specified keypad model
83 * @param model The keypad model for which to return component IDs.
84 * @return List of component IDs. Will be empty if no components match.
86 public @Nullable List<Integer> getComponentIds(String model) {
87 return getComponentIds(model, null);
91 * Get a list of component IDs of the specified type for the specified keypad model
93 * @param model The keypad model for which to return component IDs.
94 * @param type The component type to include, or null for all components.
95 * @return List of component IDs. Will be empty if no components match.
97 public List<Integer> getComponentIds(String model, @Nullable ComponentType type) {
98 List<Integer> idList = new LinkedList<>();
99 List<KeypadComponent> cList = modelData.get(model);
101 logger.debug("Keypad component IDs lookup using invalid keypad model: {}", model);
103 for (KeypadComponent i : cList) {
104 if (type == null || i.type() == type) {
113 * Determine keypad model from list of button component IDs
115 * @param buttonIds List of button component IDs for a keypad. Must be in ascending order.
116 * @return String containing the keypad model, or null if no models match.
118 public @Nullable String determineModelFromComponentIds(List<Integer> buttonIds) {
119 for (String k : modelData.keySet()) {
120 List<Integer> modelButtonIds = getComponentIds(k, ComponentType.BUTTON);
121 Collections.sort(modelButtonIds); // make sure button IDs are in ascending order for comparison
122 if (modelButtonIds.equals(buttonIds)) {
130 * Utility routine to concatenate multiple lists of {@link KeypadComponent}s
132 * @param lists Lists to concatenate
133 * @return Concatenated list
136 protected static final List<KeypadComponent> combinedList(final List<KeypadComponent>... lists) {
137 List<KeypadComponent> newlist = new LinkedList<>();
138 for (List<KeypadComponent> list : lists) {
139 newlist.addAll(list);