]> git.basschouten.com Git - openhab-addons.git/blob
cc982e4e851bb9b6efccde967359fb30ad9e65b8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.russound.internal.rio.models;
14
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.concurrent.atomic.AtomicReference;
17
18 import org.apache.commons.lang.StringUtils;
19
20 /**
21  * Simple model of a RIO Favorite (both system and zone) and it's attributes. Please note this class is used to
22  * serialize/deserialize to JSON.
23  *
24  * @author Tim Roberts - Initial contribution
25  */
26 public class RioFavorite {
27     /**
28      * The favorite ID
29      */
30     private final int id;
31
32     /**
33      * Whether the favorite is valid or not
34      */
35     private final AtomicBoolean valid = new AtomicBoolean(false);
36
37     /**
38      * The favorite name
39      */
40     private final AtomicReference<String> name = new AtomicReference<>(null);
41
42     /**
43      * Simply creates the favorite from the given ID. The favorite will not be valid and the name will default to
44      * "Favorite " + id
45      *
46      * @param id a favorite ID between 1 and 32
47      * @throws IllegalArgumentException if id < 1 or > 32
48      */
49     public RioFavorite(int id) {
50         this(id, false, null);
51     }
52
53     /**
54      * Creates the favorite from the given ID, validity and name. If the name is empty or null, it will default to
55      * "Favorite " + id
56      *
57      * @param id a favorite ID between 1 and 32
58      * @param isValid true if the favorite is valid, false otherwise
59      * @param name a possibly null, possibly empty favorite name
60      * @throws IllegalArgumentException if id < 1 or > 32
61      */
62     public RioFavorite(int id, boolean isValid, String name) {
63         if (id < 1 || id > 32) {
64             throw new IllegalArgumentException("Favorite ID must be between 1 and 32");
65         }
66
67         if (StringUtils.isEmpty(name)) {
68             name = "Favorite " + id;
69         }
70
71         this.id = id;
72         this.valid.set(isValid);
73         this.name.set(name);
74     }
75
76     /**
77      * Returns the favorite identifier
78      *
79      * @return a favorite id between 1 and 32
80      */
81     public int getId() {
82         return id;
83     }
84
85     /**
86      * Returns true if the favorite is valid, false otherwise
87      *
88      * @return true if valid, false otherwise
89      */
90     public boolean isValid() {
91         return valid.get();
92     }
93
94     /**
95      * Sets whether the favorite is valid or not
96      *
97      * @param favValid true if valid, false otherwise
98      */
99     public void setValid(boolean favValid) {
100         valid.set(favValid);
101     }
102
103     /**
104      * Set's the favorite name. If null or empty, will default to "Favorite " + getId()
105      *
106      * @param favName a possibly null, possibly empty favorite name
107      */
108     public void setName(String favName) {
109         name.set(StringUtils.isEmpty(favName) ? "Favorite " + getId() : favName);
110     }
111
112     /**
113      * Returns the favorite name
114      *
115      * @return a non-null, non-empty favorite name
116      */
117     public String getName() {
118         return name.get();
119     }
120 }