]> git.basschouten.com Git - openhab-addons.git/blob
d69e72c794b583a5dae60f17f2b05dda83627289
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.daikin.internal.api;
14
15 import java.util.Map;
16 import java.util.Optional;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21
22 /**
23  * Class for parsing the comma separated values and array values returned by the Daikin Controller.
24  *
25  * @author Jimmy Tanagra - Initial Contribution
26  *
27  */
28 @NonNullByDefault
29 public class InfoParser {
30     private InfoParser() {
31     }
32
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]));
40     }
41
42     public static Optional<Double> parseDouble(String value) {
43         if ("-".equals(value)) {
44             return Optional.empty();
45         }
46         try {
47             return Optional.of(Double.parseDouble(value));
48         } catch (NumberFormatException e) {
49             return Optional.empty();
50         }
51     }
52
53     public static Optional<Integer> parseInt(String value) {
54         if ("-".equals(value)) {
55             return Optional.empty();
56         }
57         try {
58             return Optional.of(Integer.parseInt(value));
59         } catch (NumberFormatException e) {
60             return Optional.empty();
61         }
62     }
63
64     public static Optional<Integer[]> parseArrayofInt(String value) {
65         if ("-".equals(value)) {
66             return Optional.empty();
67         }
68         try {
69             return Optional.of(Stream.of(value.split("/")).map(val -> Integer.parseInt(val)).toArray(Integer[]::new));
70         } catch (NumberFormatException e) {
71             return Optional.empty();
72         }
73     }
74
75     public static Optional<Integer[]> parseArrayofInt(String value, int expectedArraySize) {
76         Optional<Integer[]> result = parseArrayofInt(value);
77         if (result.isPresent() && result.get().length == expectedArraySize) {
78             return result;
79         }
80         return Optional.empty();
81     }
82 }