]> git.basschouten.com Git - openhab-addons.git/blob
e223114960b2884cdcc0f29be5664c3b8df237b7
[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.mpd.internal.protocol;
14
15 import java.util.Map;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Class for representing a song.
23  *
24  * @author Stefan Röllin - Initial contribution
25  */
26 @NonNullByDefault
27 public class MPDSong {
28
29     private final Logger logger = LoggerFactory.getLogger(MPDSong.class);
30
31     private final String filename;
32     private final String album;
33     private final String artist;
34     private final String name;
35     private final int song;
36     private final int songId;
37     private final String title;
38     private final int track;
39
40     public MPDSong(MPDResponse response) {
41         Map<String, String> values = MPDResponseParser.responseToMap(response);
42         filename = values.getOrDefault("file", "");
43         album = values.getOrDefault("Album", "");
44         artist = values.getOrDefault("Artist", "");
45         name = values.getOrDefault("Name", "");
46         song = parseInteger(values.getOrDefault("Pos", "0"), 0);
47         songId = parseInteger(values.getOrDefault("Id", "0"), 0);
48         title = values.getOrDefault("Title", "");
49         track = parseInteger(values.getOrDefault("Track", "-1"), -1);
50     }
51
52     public String getFilename() {
53         return filename;
54     }
55
56     public String getAlbum() {
57         return album;
58     }
59
60     public String getArtist() {
61         return artist;
62     }
63
64     public String getName() {
65         return name;
66     }
67
68     public int getSong() {
69         return song;
70     }
71
72     public int getSongId() {
73         return songId;
74     }
75
76     public String getTitle() {
77         return title;
78     }
79
80     public int getTrack() {
81         return track;
82     }
83
84     private int parseInteger(String value, int aDefault) {
85         try {
86             return Integer.parseInt(value);
87         } catch (NumberFormatException e) {
88             logger.debug("parseInt of {} failed", value);
89         }
90         return aDefault;
91     }
92 }