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.persistence.jpa.internal;
15 import java.util.Locale;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.library.types.DateTimeType;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.PointType;
21 import org.openhab.core.types.State;
24 * Helper class for dealing with State
26 * @author Manfred Bergmann - Initial contribution
30 public class StateHelper {
33 * Converts the given State to a string that can be persisted in the database.
35 * @param state the state of the item to be persisted
36 * @return state converted as string
38 public static String toString(State state) {
39 if (state instanceof DateTimeType type) {
40 return String.valueOf(type.getZonedDateTime().toInstant().toEpochMilli());
42 if (state instanceof DecimalType type) {
43 return String.valueOf(type.doubleValue());
45 if (state instanceof PointType pType) {
46 return String.format(Locale.ENGLISH, "%f;%f;%f", pType.getLatitude().doubleValue(),
47 pType.getLongitude().doubleValue(), pType.getAltitude().doubleValue());
50 return state.toString();