]> git.basschouten.com Git - openhab-addons.git/blob
c81566b99f3b0d463f2a1c67793b52224c658493
[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.handler.utils;
14
15 import java.math.BigDecimal;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.PercentType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.UnDefType;
25
26 /**
27  * This class support handling of openHAB type {@link State}. Therefore, it provides the methods:
28  * <UL>
29  * <LI>{@link #createState} for creating an openHAB {@link State}.</LI>
30  * </UL>
31  *
32  * @author Guenther Schreiner - Initial contribution
33  */
34 @NonNullByDefault
35 public class StateUtils {
36
37     /*
38      * ************************
39      * ***** Constructors *****
40      */
41
42     // Suppress default constructor for non-instantiability
43
44     private StateUtils() {
45         throw new AssertionError();
46     }
47
48     /**
49      * Creates an openHAB {@link State} in accordance to the class of the given {@code propertyValue}. Currently
50      * {@link PercentType}, {@link DecimalType}, and {@link Boolean} are handled explicitly. All other
51      * {@code dataTypes} are mapped to {@link StringType}.
52      * <p>
53      * If {@code propertyValue} is {@code null}, {@link UnDefType#NULL} will be returned.
54      * <P>
55      * Copied/adapted from the org.openhab.binding.koubachi binding.
56      * </P>
57      *
58      * @param propertyValue which should be converted,
59      * @return <b>state</B> of type {@link State} in accordance with {@code dataType}. Will never be {@code null}.
60      */
61     public static State createState(@Nullable Object propertyValue) {
62         if (propertyValue == null) {
63             return UnDefType.NULL;
64         }
65
66         Class<?> dataType = propertyValue.getClass();
67
68         if (PercentType.class.isAssignableFrom(dataType)) {
69             return new PercentType((Integer) propertyValue);
70         } else if (Integer.class.isAssignableFrom(dataType)) {
71             return new DecimalType((Integer) propertyValue);
72         } else if (BigDecimal.class.isAssignableFrom(dataType)) {
73             return new DecimalType((BigDecimal) propertyValue);
74         } else if (Boolean.class.isAssignableFrom(dataType)) {
75             if ((Boolean) propertyValue) {
76                 return OnOffType.ON;
77             } else {
78                 return OnOffType.OFF;
79             }
80         } else if (State.class.isAssignableFrom(dataType)) {
81             return (State) propertyValue;
82         } else {
83             return new StringType(propertyValue.toString());
84         }
85     }
86 }