2 * Copyright (c) 2010-2022 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.velux.internal.handler.utils;
15 import java.math.BigDecimal;
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;
27 * This class support handling of openHAB type {@link State}. Therefore, it provides the methods:
29 * <LI>{@link #createState} for creating an openHAB {@link State}.</LI>
32 * @author Guenther Schreiner - Initial contribution
35 public class StateUtils {
38 * ************************
39 * ***** Constructors *****
42 // Suppress default constructor for non-instantiability
44 private StateUtils() {
45 throw new AssertionError();
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}.
53 * If {@code propertyValue} is {@code null}, {@link UnDefType#NULL} will be returned.
55 * Copied/adapted from the org.openhab.binding.koubachi binding.
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}.
61 public static State createState(@Nullable Object propertyValue) {
62 if (propertyValue == null) {
63 return UnDefType.NULL;
66 Class<?> dataType = propertyValue.getClass();
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) {
80 } else if (State.class.isAssignableFrom(dataType)) {
81 return (State) propertyValue;
83 return new StringType(propertyValue.toString());