2 * Copyright (c) 2010-2023 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.mikrotik.internal.util;
15 import java.math.BigInteger;
16 import java.time.LocalDateTime;
17 import java.time.ZoneId;
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;
32 * The {@link StateUtil} class holds static methods to cast Java native/class types to OpenHAB values
34 * @author Oleg Vivtash - Initial contribution
37 public class StateUtil {
39 public static State stringOrNull(@Nullable String value) {
40 return value == null ? UnDefType.NULL : new StringType(value);
43 public static State qtyMegabitPerSecOrNull(@Nullable Float value) {
44 return value == null ? UnDefType.NULL : new QuantityType<>(value, Units.MEGABIT_PER_SECOND);
47 public static State qtyPercentOrNull(@Nullable Integer value) {
48 return value == null ? UnDefType.NULL : new QuantityType<>(value, Units.PERCENT);
51 public static State qtyBytesOrNull(@Nullable Integer value) {
52 return value == null ? UnDefType.NULL : new QuantityType<>(value, Units.BYTE);
55 public static State intOrNull(@Nullable Integer value) {
56 return value == null ? UnDefType.NULL : new DecimalType(value.floatValue());
59 public static State bigIntOrNull(@Nullable BigInteger value) {
60 return value == null ? UnDefType.NULL : DecimalType.valueOf(value.toString());
63 public static State floatOrNull(@Nullable Float value) {
64 return value == null ? UnDefType.NULL : new DecimalType(value);
67 public static State boolSwitchOrNull(@Nullable Boolean value) {
69 return UnDefType.NULL;
71 return value ? OnOffType.ON : OnOffType.OFF;
74 public static State boolContactOrNull(@Nullable Boolean value) {
76 return UnDefType.NULL;
78 return value ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
81 public static State timeOrNull(@Nullable LocalDateTime value) {
82 return value == null ? UnDefType.NULL : new DateTimeType(value.atZone(ZoneId.systemDefault()));