]> git.basschouten.com Git - openhab-addons.git/blob
fff54e8ba055c7c3434845dfeb245e9c2c1cfdc1
[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.dbquery.internal.domain;
14
15 import java.time.Duration;
16 import java.time.Instant;
17 import java.util.Date;
18 import java.util.LinkedHashMap;
19 import java.util.Map;
20 import java.util.Set;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Query result row
29  *
30  * @author Joan Pujol - Initial contribution
31  */
32 @NonNullByDefault
33 public class ResultRow {
34     private final Logger logger = LoggerFactory.getLogger(ResultRow.class);
35
36     private final LinkedHashMap<String, @Nullable Object> values;
37
38     public ResultRow(String columnName, @Nullable Object value) {
39         this.values = new LinkedHashMap<>();
40         put(columnName, value);
41     }
42
43     public ResultRow(Map<String, @Nullable Object> values) {
44         this.values = new LinkedHashMap<>();
45         values.forEach(this::put);
46     }
47
48     public Set<String> getColumnNames() {
49         return values.keySet();
50     }
51
52     public int getColumnsSize() {
53         return values.size();
54     }
55
56     public @Nullable Object getValue(String column) {
57         return values.get(column);
58     }
59
60     public static boolean isValidResultRowType(@Nullable Object object) {
61         return object == null || object instanceof String || object instanceof Boolean || object instanceof Number
62                 || object instanceof byte[] || object instanceof Instant || object instanceof Date
63                 || object instanceof Duration;
64     }
65
66     private void put(String columnName, @Nullable Object value) {
67         if (!isValidResultRowType(value)) {
68             logger.trace("Value {} of type {} converted to String as not supported internal type in dbquery", value,
69                     value.getClass());
70             value = value.toString();
71         }
72         values.put(columnName, value);
73     }
74
75     @Override
76     public String toString() {
77         return values.toString();
78     }
79 }