]> git.basschouten.com Git - openhab-addons.git/blob
0e86919a797439f8b8ff727e98a04eb463a93535
[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.io.neeo.internal.models;
14
15 import java.util.Arrays;
16 import java.util.Objects;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 import com.google.gson.annotations.SerializedName;
22
23 /**
24  * Represents the result of a directory list request. This class is simply used for serialization of the result back to
25  * the brain.
26  *
27  * @author Tim Roberts - Initial Contribution
28  *
29  */
30 @NonNullByDefault
31 public class NeeoDirectoryResult {
32     /** The title of the directory */
33     private final String title;
34
35     /** The amount of items to show (the limit) */
36     private final int limit;
37
38     /** The offset index position of the first item in items */
39     private final int offset;
40
41     /** The total number of items overally */
42     private final int totalMatchingItems;
43
44     /** The browse identifier (not used for lists) */
45     @Nullable
46     private final String browseIdentifier;
47
48     /** The subset of items in this result (index 0 = offset position in total) */
49     private final NeeoDirectoryResultItem[] items;
50
51     /** The meta data describing the current, previous and next positions */
52     @SerializedName("_meta")
53     private final NeeoDirectoryResultMeta meta;
54
55     /**
56      * Constructs the result given the request and overall directory
57      *
58      * @param req a non-null {@link NeeoDirectoryRequest}
59      * @param directory a non-null {@link NeeoDeviceChannelDirectory}
60      */
61     public NeeoDirectoryResult(NeeoDirectoryRequest req, NeeoDeviceChannelDirectory directory) {
62         Objects.requireNonNull(req, "req cannot be null");
63         Objects.requireNonNull(directory, "directory cannot be null");
64
65         final NeeoDeviceChannelDirectoryListItem[] listItems = directory.getListItems();
66
67         this.title = directory.getLabel();
68         this.limit = Math.min(10, listItems.length);
69         this.offset = req.getOffset();
70         this.totalMatchingItems = 1;
71         this.browseIdentifier = req.getBrowseIdentifier();
72
73         this.items = Arrays.stream(listItems).skip(this.offset).limit(this.limit).map(item -> {
74             return new NeeoDirectoryResultItem(item.getTitle(), item.getThumbNailUri(), null, item.getItemValue(),
75                     true);
76         }).toArray(NeeoDirectoryResultItem[]::new);
77
78         final NeeoDirectoryRequest current = new NeeoDirectoryRequest(this.offset, this.limit, this.browseIdentifier);
79
80         final NeeoDirectoryRequest previous = this.offset > 0
81                 ? new NeeoDirectoryRequest(Math.max(0, this.offset - this.limit), Math.min(this.offset, this.limit),
82                         this.browseIdentifier)
83                 : null;
84
85         final int nextOffset = this.offset + this.items.length;
86         final NeeoDirectoryRequest next = listItems.length > nextOffset
87                 ? new NeeoDirectoryRequest(nextOffset, this.limit, this.browseIdentifier)
88                 : null;
89
90         this.meta = new NeeoDirectoryResultMeta(listItems.length, listItems.length, current, previous, next);
91     }
92
93     /**
94      * Returns the title
95      *
96      * @return a non-null title
97      */
98     public String getTitle() {
99         return title;
100     }
101
102     /**
103      * Returns the result limit
104      *
105      * @return the result limit (>= 0)
106      */
107     public int getLimit() {
108         return limit;
109     }
110
111     /**
112      * Returns the offset of the items
113      *
114      * @return the offset (> = 0)
115      */
116     public int getOffset() {
117         return offset;
118     }
119
120     /**
121      * The total matching items
122      *
123      * @return the total items (>=0)
124      */
125     public int getTotalMatchingItems() {
126         return totalMatchingItems;
127     }
128
129     /**
130      * The browse identifier
131      *
132      * @return a non-null possibly empty browse identifier
133      */
134     @Nullable
135     public String getBrowseIdentifier() {
136         return browseIdentifier;
137     }
138
139     /**
140      * The items in this result
141      *
142      * @return a non-null, possibly empty list of items
143      */
144     public NeeoDirectoryResultItem[] getItems() {
145         return items;
146     }
147
148     /**
149      * The meta data for the request
150      *
151      * @return a non-null meta data
152      */
153     public NeeoDirectoryResultMeta getMeta() {
154         return meta;
155     }
156
157     @Override
158     public String toString() {
159         return "NeeoDiscoveryListResult [title=" + title + ", limit=" + limit + ", offset=" + offset
160                 + ", totalMatchingItems=" + totalMatchingItems + ", browseIdentifier=" + browseIdentifier + ", items="
161                 + Arrays.toString(items) + ", meta=" + meta + "]";
162     }
163 }