]> git.basschouten.com Git - openhab-addons.git/blob
34138c50935534c43038d97a7baca91993e43134
[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.mikrotik.internal.util;
14
15 import java.math.BigInteger;
16 import java.time.LocalDateTime;
17 import java.time.ZoneId;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.library.types.DateTimeType;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.OpenClosedType;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.library.unit.Units;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
30
31 /**
32  * The {@link StateUtil} class holds static methods to cast Java native/class types to OpenHAB values
33  *
34  * @author Oleg Vivtash - Initial contribution
35  */
36 @NonNullByDefault
37 public class StateUtil {
38
39     public static State stringOrNull(@Nullable String value) {
40         return value == null ? UnDefType.NULL : new StringType(value);
41     }
42
43     public static State qtyMegabitPerSecOrNull(@Nullable Float value) {
44         return value == null ? UnDefType.NULL : new QuantityType<>(value, Units.MEGABIT_PER_SECOND);
45     }
46
47     public static State qtyPercentOrNull(@Nullable Integer value) {
48         return value == null ? UnDefType.NULL : new QuantityType<>(value, Units.PERCENT);
49     }
50
51     public static State qtyBytesOrNull(@Nullable Integer value) {
52         return value == null ? UnDefType.NULL : new QuantityType<>(value, Units.BYTE);
53     }
54
55     public static State intOrNull(@Nullable Integer value) {
56         return value == null ? UnDefType.NULL : new DecimalType(value.floatValue());
57     }
58
59     public static State bigIntOrNull(@Nullable BigInteger value) {
60         return value == null ? UnDefType.NULL : DecimalType.valueOf(value.toString());
61     }
62
63     public static State floatOrNull(@Nullable Float value) {
64         return value == null ? UnDefType.NULL : new DecimalType(value);
65     }
66
67     public static State boolSwitchOrNull(@Nullable Boolean value) {
68         if (value == null) {
69             return UnDefType.NULL;
70         }
71         return value ? OnOffType.ON : OnOffType.OFF;
72     }
73
74     public static State boolContactOrNull(@Nullable Boolean value) {
75         if (value == null) {
76             return UnDefType.NULL;
77         }
78         return value ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
79     }
80
81     public static State timeOrNull(@Nullable LocalDateTime value) {
82         return value == null ? UnDefType.NULL : new DateTimeType(value.atZone(ZoneId.systemDefault()));
83     }
84 }