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.weatherunderground.internal.json;
15 import java.math.BigDecimal;
16 import java.net.MalformedURLException;
18 import java.time.DateTimeException;
19 import java.time.Instant;
20 import java.time.ZoneId;
21 import java.time.ZonedDateTime;
23 import org.slf4j.LoggerFactory;
26 * The {@link WeatherUndergroundJsonUtils} class contains utilities methods.
28 * @author Laurent Garnier - Initial contribution
30 public class WeatherUndergroundJsonUtils {
32 private static final String TREND_UP = "up";
33 private static final String TREND_DOWN = "down";
34 private static final String TREND_STABLE = "stable";
37 * Convert a string representing an Epoch value into a Calendar object
39 * @param value the Epoch value as a string
41 * @return the ZonedDateTime object representing the date and time of the Epoch
42 * or null in case of conversion error
44 public static ZonedDateTime convertToZonedDateTime(String value, ZoneId zoneId) {
47 Instant epochSeconds = Instant.ofEpochSecond(Long.valueOf(value));
48 return ZonedDateTime.ofInstant(epochSeconds, zoneId);
49 } catch (DateTimeException e) {
50 LoggerFactory.getLogger(WeatherUndergroundJsonUtils.class).debug("Cannot convert {} to ZonedDateTime",
59 * Convert a string representing an integer value into an Integer object
61 * @param value the integer value as a string
63 * @return the Integer object representing the value or null in case of conversion error
65 public static Integer convertToInteger(String value) {
66 Integer result = null;
69 result = Integer.valueOf(value.trim());
70 } catch (NumberFormatException e) {
71 LoggerFactory.getLogger(WeatherUndergroundJsonUtils.class).debug("Cannot convert {} to Integer", value);
78 * Convert a string representing a decimal value into a BigDecimal object
80 * @param value the decimal value as a string
82 * @return the BigDecimal object representing the value or null in case of conversion error
84 public static BigDecimal convertToBigDecimal(String value) {
85 BigDecimal result = null;
87 result = new BigDecimal(value.trim());
92 private static boolean isValid(String value) {
93 return (value != null) && !value.isEmpty() && !"N/A".equalsIgnoreCase(value) && !"NA".equalsIgnoreCase(value)
94 && !"-".equals(value) && !"--".equals(value);
98 * Convert a string representing a URL into a URL object
100 * @param url the URL as a string
102 * @return the URL object representing the URL or null in case of invalid URL
104 public static URL getValidUrl(String url) {
106 if (url != null && !url.trim().isEmpty()) {
108 validUrl = new URL(url.trim());
109 } catch (MalformedURLException e) {
116 * Convert a string representing a decimal value into a pressure trend constant
118 * @param value the decimal value as a string
120 * @return the pressure trend constant representing the value or null in case of conversion error
122 public static String convertToTrend(String value) {
123 String result = null;
124 if (isValid(value)) {
126 int val = Integer.valueOf(value.trim());
129 } else if (val > 0) {
132 result = TREND_STABLE;
134 } catch (NumberFormatException e) {
135 LoggerFactory.getLogger(WeatherUndergroundJsonUtils.class).debug("Cannot convert {} to Integer", value);