]> git.basschouten.com Git - openhab-addons.git/blob
bde30da2ab621689ab25c94014a0a1dde5cbee57
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.upnpcontrol.internal.queue;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Objects;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20 import java.util.stream.Collectors;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.util.StringUtils;
25
26 /**
27  *
28  * @author Mark Herwege - Initial contribution
29  * @author Karel Goderis - Based on UPnP logic in Sonos binding
30  */
31 @NonNullByDefault
32 public class UpnpEntry {
33
34     private static final String DIRECTORY_ROOT = "0";
35
36     private static final Pattern CONTAINER_PATTERN = Pattern.compile("object.container");
37
38     private String id;
39     private String refId;
40     private String parentId;
41     private String upnpClass;
42     private String title = "";
43     private List<UpnpEntryRes> resList = new ArrayList<>();
44     private String album = "";
45     private String albumArtUri = "";
46     private String creator = "";
47     private String artist = "";
48     private String publisher = "";
49     private String genre = "";
50     private @Nullable Integer originalTrackNumber;
51
52     private boolean isContainer;
53
54     public UpnpEntry() {
55         this("", "", "", "");
56     }
57
58     public UpnpEntry(String id, String refId, String parentId, String upnpClass) {
59         this.id = id;
60         this.refId = refId;
61         this.parentId = parentId;
62         this.upnpClass = upnpClass;
63
64         Matcher matcher = CONTAINER_PATTERN.matcher(upnpClass);
65         isContainer = matcher.find();
66     }
67
68     public UpnpEntry withTitle(String title) {
69         this.title = title;
70         return this;
71     }
72
73     public UpnpEntry withAlbum(String album) {
74         this.album = album;
75         return this;
76     }
77
78     public UpnpEntry withAlbumArtUri(String albumArtUri) {
79         this.albumArtUri = albumArtUri;
80         return this;
81     }
82
83     public UpnpEntry withCreator(String creator) {
84         this.creator = creator;
85         return this;
86     }
87
88     public UpnpEntry withArtist(String artist) {
89         this.artist = artist;
90         return this;
91     }
92
93     public UpnpEntry withPublisher(String publisher) {
94         this.publisher = publisher;
95         return this;
96     }
97
98     public UpnpEntry withGenre(String genre) {
99         this.genre = genre;
100         return this;
101     }
102
103     public UpnpEntry withResList(List<UpnpEntryRes> resList) {
104         this.resList = resList;
105         return this;
106     }
107
108     public UpnpEntry withTrackNumber(@Nullable Integer originalTrackNumber) {
109         this.originalTrackNumber = originalTrackNumber;
110         return this;
111     }
112
113     /**
114      * @return the title of the entry.
115      */
116     @Override
117     public String toString() {
118         return title;
119     }
120
121     /**
122      * @return the unique identifier of this entry.
123      */
124     public String getId() {
125         return id;
126     }
127
128     /**
129      * @return the title of the entry.
130      */
131     public String getTitle() {
132         return title;
133     }
134
135     /**
136      * @return the identifier of the entry this reference intry refers to.
137      */
138     public String getRefId() {
139         return refId;
140     }
141
142     /**
143      * @return the unique identifier of the parent of this entry.
144      */
145     public String getParentId() {
146         return parentId.isEmpty() ? DIRECTORY_ROOT : parentId;
147     }
148
149     /**
150      * @return a URI for this entry. Thumbnail resources are not considered.
151      */
152     public String getRes() {
153         return resList.stream().filter(res -> !res.isThumbnailRes()).map(UpnpEntryRes::getRes).findAny().orElse("");
154     }
155
156     public List<String> getProtocolList() {
157         return resList.stream().map(UpnpEntryRes::getProtocolInfo).collect(Collectors.toList());
158     }
159
160     /**
161      * @return the UPnP classname for this entry.
162      */
163     public String getUpnpClass() {
164         return upnpClass;
165     }
166
167     public boolean isContainer() {
168         return isContainer;
169     }
170
171     /**
172      * @return the name of the album.
173      */
174     public String getAlbum() {
175         return album;
176     }
177
178     /**
179      * @return the URI for the album art.
180      */
181     public String getAlbumArtUri() {
182         return Objects.requireNonNull(StringUtils.unEscapeXml(albumArtUri));
183     }
184
185     /**
186      * @return the name of the artist who created the entry.
187      */
188     public String getCreator() {
189         return creator;
190     }
191
192     public String getArtist() {
193         return artist;
194     }
195
196     public String getPublisher() {
197         return publisher;
198     }
199
200     public String getGenre() {
201         return genre;
202     }
203
204     public @Nullable Integer getOriginalTrackNumber() {
205         return originalTrackNumber;
206     }
207 }