]> git.basschouten.com Git - openhab-addons.git/blob
f1a67f4152899152cc0b982f4ee5bc11954e8ad8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.benqprojector.internal;
14
15 import java.io.InvalidClassException;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.items.Item;
19 import org.openhab.core.library.items.NumberItem;
20 import org.openhab.core.library.items.StringItem;
21 import org.openhab.core.library.items.SwitchItem;
22
23 /**
24  * Represents all valid command types which could be processed by this
25  * binding.
26  *
27  * @author Michael Lobstein - Initial contribution
28  */
29 @NonNullByDefault
30 public enum BenqProjectorCommandType {
31     POWER("Power", SwitchItem.class),
32     SOURCE("Source", StringItem.class),
33     PICTURE_MODE("PictureMode", StringItem.class),
34     ASPECT_RATIO("AspectRatio", StringItem.class),
35     FREEZE("Freeze", SwitchItem.class),
36     BLANK("Blank", SwitchItem.class),
37     DIRECTCMD("DirectCmd", StringItem.class),
38     LAMP_TIME("LampTime", NumberItem.class);
39
40     private final String text;
41     private Class<? extends Item> itemClass;
42
43     private BenqProjectorCommandType(final String text, Class<? extends Item> itemClass) {
44         this.text = text;
45         this.itemClass = itemClass;
46     }
47
48     @Override
49     public String toString() {
50         return text;
51     }
52
53     public Class<? extends Item> getItemClass() {
54         return itemClass;
55     }
56
57     /**
58      * Procedure to validate command type string.
59      *
60      * @param commandTypeText
61      *            command string e.g. RawData, Command, Brightness
62      * @return true if item is valid.
63      * @throws IllegalArgumentException
64      *             Not valid command type.
65      * @throws InvalidClassException
66      *             Not valid class for command type.
67      */
68     public static boolean validateBinding(String commandTypeText, Class<? extends Item> itemClass)
69             throws IllegalArgumentException, InvalidClassException {
70         for (BenqProjectorCommandType c : BenqProjectorCommandType.values()) {
71             if (c.text.equalsIgnoreCase(commandTypeText)) {
72                 if (c.getItemClass().equals(itemClass)) {
73                     return true;
74                 } else {
75                     throw new InvalidClassException("Not valid class for command type");
76                 }
77             }
78         }
79
80         throw new IllegalArgumentException("Not valid command type");
81     }
82
83     /**
84      * Procedure to convert command type string to command type class.
85      *
86      * @param commandTypeText
87      *            command string e.g. RawData, Command, Brightness
88      * @return corresponding command type.
89      * @throws InvalidClassException
90      *             Not valid class for command type.
91      */
92     public static BenqProjectorCommandType getCommandType(String commandTypeText) throws IllegalArgumentException {
93         for (BenqProjectorCommandType c : BenqProjectorCommandType.values()) {
94             if (c.text.equalsIgnoreCase(commandTypeText)) {
95                 return c;
96             }
97         }
98
99         throw new IllegalArgumentException("Not valid command type");
100     }
101 }