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.io.neeo.internal.models;
15 import java.util.Arrays;
16 import java.util.Objects;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
21 import com.google.gson.annotations.SerializedName;
24 * Represents the result of a directory list request. This class is simply used for serialization of the result back to
27 * @author Tim Roberts - Initial Contribution
31 public class NeeoDirectoryResult {
32 /** The title of the directory */
33 private final String title;
35 /** The amount of items to show (the limit) */
36 private final int limit;
38 /** The offset index position of the first item in items */
39 private final int offset;
41 /** The total number of items overally */
42 private final int totalMatchingItems;
44 /** The browse identifier (not used for lists) */
46 private final String browseIdentifier;
48 /** The subset of items in this result (index 0 = offset position in total) */
49 private final NeeoDirectoryResultItem[] items;
51 /** The meta data describing the current, previous and next positions */
52 @SerializedName("_meta")
53 private final NeeoDirectoryResultMeta meta;
56 * Constructs the result given the request and overall directory
58 * @param req a non-null {@link NeeoDirectoryRequest}
59 * @param directory a non-null {@link NeeoDeviceChannelDirectory}
61 public NeeoDirectoryResult(NeeoDirectoryRequest req, NeeoDeviceChannelDirectory directory) {
62 Objects.requireNonNull(req, "req cannot be null");
63 Objects.requireNonNull(directory, "directory cannot be null");
65 final NeeoDeviceChannelDirectoryListItem[] listItems = directory.getListItems();
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();
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(),
76 }).toArray(NeeoDirectoryResultItem[]::new);
78 final NeeoDirectoryRequest current = new NeeoDirectoryRequest(this.offset, this.limit, this.browseIdentifier);
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)
85 final int nextOffset = this.offset + this.items.length;
86 final NeeoDirectoryRequest next = listItems.length > nextOffset
87 ? new NeeoDirectoryRequest(nextOffset, this.limit, this.browseIdentifier)
90 this.meta = new NeeoDirectoryResultMeta(listItems.length, listItems.length, current, previous, next);
96 * @return a non-null title
98 public String getTitle() {
103 * Returns the result limit
105 * @return the result limit (>= 0)
107 public int getLimit() {
112 * Returns the offset of the items
114 * @return the offset (> = 0)
116 public int getOffset() {
121 * The total matching items
123 * @return the total items (>=0)
125 public int getTotalMatchingItems() {
126 return totalMatchingItems;
130 * The browse identifier
132 * @return a non-null possibly empty browse identifier
135 public String getBrowseIdentifier() {
136 return browseIdentifier;
140 * The items in this result
142 * @return a non-null, possibly empty list of items
144 public NeeoDirectoryResultItem[] getItems() {
149 * The meta data for the request
151 * @return a non-null meta data
153 public NeeoDirectoryResultMeta getMeta() {
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 + "]";