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.daikin.internal.api;
16 import java.util.Optional;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
23 * Class for parsing the comma separated values and array values returned by the Daikin Controller.
25 * @author Jimmy Tanagra - Initial Contribution
29 public class InfoParser {
30 private InfoParser() {
33 public static Map<String, String> parse(String response) {
34 return Stream.of(response.split(",")).filter(kv -> kv.contains("=")).map(kv -> {
35 String[] keyValue = kv.split("=");
36 String key = keyValue[0];
37 String value = keyValue.length > 1 ? keyValue[1] : "";
38 return new String[] { key, value };
39 }).collect(Collectors.toMap(x -> x[0], x -> x[1]));
42 public static Optional<Double> parseDouble(String value) {
43 if ("-".equals(value)) {
44 return Optional.empty();
47 return Optional.of(Double.parseDouble(value));
48 } catch (NumberFormatException e) {
49 return Optional.empty();
53 public static Optional<Integer> parseInt(String value) {
54 if ("-".equals(value)) {
55 return Optional.empty();
58 return Optional.of(Integer.parseInt(value));
59 } catch (NumberFormatException e) {
60 return Optional.empty();
64 public static Optional<Integer[]> parseArrayofInt(String value) {
65 if ("-".equals(value)) {
66 return Optional.empty();
69 return Optional.of(Stream.of(value.split("/")).map(val -> Integer.parseInt(val)).toArray(Integer[]::new));
70 } catch (NumberFormatException e) {
71 return Optional.empty();
75 public static Optional<Integer[]> parseArrayofInt(String value, int expectedArraySize) {
76 Optional<Integer[]> result = parseArrayofInt(value);
77 if (result.isPresent() && result.get().length == expectedArraySize) {
80 return Optional.empty();