]> git.basschouten.com Git - openhab-addons.git/blob
c4ff56c93233fd77e4964df5d3b7d38fa48e95d6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.velux.internal.things;
14
15 import java.util.Map;
16 import java.util.concurrent.ConcurrentHashMap;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.velux.internal.VeluxBindingConstants;
20 import org.openhab.binding.velux.internal.things.VeluxScene.SceneName;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Combined set of scene informations provided by the <B>Velux</B> bridge,
26  * which can be used for later interactions.
27  * <P>
28  * The following class access methods exist:
29  * <UL>
30  * <LI>{@link VeluxExistingScenes#isRegistered} for querying existence of a {@link VeluxScene},</LI>
31  * <LI>{@link VeluxExistingScenes#register} for storing a {@link VeluxScene},</LI>
32  * <LI>{@link VeluxExistingScenes#get} for retrieval of a {@link VeluxScene},</LI>
33  * <LI>{@link VeluxExistingScenes#values} for retrieval of all {@link VeluxScene}s,</LI>
34  * <LI>{@link VeluxExistingScenes#getNoMembers} for retrieval of the number of all {@link VeluxScene}s,</LI>
35  * <LI>{@link VeluxExistingScenes#toString} for a descriptive string representation.</LI>
36  * </UL>
37  *
38  * @see VeluxScene
39  *
40  * @author Guenther Schreiner - initial contribution.
41  */
42 @NonNullByDefault
43 public class VeluxExistingScenes {
44     private final Logger logger = LoggerFactory.getLogger(VeluxExistingScenes.class);
45
46     // Type definitions, class-internal variables
47
48     private Map<String, VeluxScene> existingScenesBySceneName;
49     private int memberCount;
50
51     // Constructor methods
52
53     public VeluxExistingScenes() {
54         existingScenesBySceneName = new ConcurrentHashMap<>();
55         memberCount = 0;
56         logger.trace("VeluxExistingScenes(constructor) done.");
57     }
58
59     // Class access methods
60
61     public boolean isRegistered(SceneName sceneName) {
62         logger.trace("isRegistered({}) returns {}.", sceneName,
63                 existingScenesBySceneName.containsKey(sceneName.toString()) ? "true" : "false");
64         return existingScenesBySceneName.containsKey(sceneName.toString());
65     }
66
67     public boolean isRegistered(VeluxScene scene) {
68         return isRegistered(scene.getName());
69     }
70
71     public boolean register(VeluxScene newScene) {
72         if (isRegistered(newScene)) {
73             logger.trace("register() ignoring scene {} as already known.", newScene);
74             return false;
75         }
76         logger.trace("register() registering new scene {}.", newScene);
77         existingScenesBySceneName.put(newScene.getName().toString(), newScene);
78         memberCount++;
79         return true;
80     }
81
82     public VeluxScene get(SceneName sceneName) {
83         logger.trace("get({}) called.", sceneName);
84         if (!isRegistered(sceneName)) {
85             return VeluxScene.UNKNOWN;
86         }
87         return existingScenesBySceneName.getOrDefault(sceneName.toString(), VeluxScene.UNKNOWN);
88     }
89
90     public VeluxScene[] values() {
91         return existingScenesBySceneName.values().toArray(new VeluxScene[0]);
92     }
93
94     public int getNoMembers() {
95         logger.trace("getNoMembers() returns {}.", memberCount);
96         return memberCount;
97     }
98
99     public String toString(boolean showSummary, String delimiter) {
100         StringBuilder sb = new StringBuilder();
101
102         if (showSummary) {
103             sb.append(memberCount).append(" members: ");
104         }
105         for (VeluxScene scene : this.values()) {
106             sb.append(scene.toString()).append(delimiter);
107         }
108         if (sb.lastIndexOf(delimiter) > 0) {
109             sb.deleteCharAt(sb.lastIndexOf(delimiter));
110         }
111         return sb.toString();
112     }
113
114     @Override
115     public String toString() {
116         return toString(true, VeluxBindingConstants.OUTPUT_VALUE_SEPARATOR);
117     }
118 }