]> git.basschouten.com Git - openhab-addons.git/blob
44ed86294fc215801eb26331a4d8ef73b7c99a94
[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.action;
14
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 /**
23  * Query result as it's exposed to users in thing actions
24  *
25  * @author Joan Pujol - Initial contribution
26  */
27 @NonNullByDefault
28 public class ActionQueryResult {
29     private final boolean correct;
30     private List<Map<String, @Nullable Object>> data = Collections.emptyList();
31
32     public ActionQueryResult(boolean correct, @Nullable List<Map<String, @Nullable Object>> data) {
33         this.correct = correct;
34         if (data != null) {
35             this.data = data;
36         }
37     }
38
39     public boolean isCorrect() {
40         return correct;
41     }
42
43     public List<Map<String, @Nullable Object>> getData() {
44         return data;
45     }
46
47     public @Nullable Object getResultAsScalar() {
48         var firstResult = data.get(0);
49         return isScalarResult() ? firstResult.get(firstResult.keySet().iterator().next()) : null;
50     }
51
52     public boolean isScalarResult() {
53         return data.size() == 1 && data.get(0).keySet().size() == 1;
54     }
55 }