]> git.basschouten.com Git - openhab-addons.git/blob
6371c2c2201faf719a909cffee9032278dacf670
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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 Preset 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 RioPreset {
27     /**
28      * The preset id
29      */
30     private final int id;
31
32     /**
33      * Whether the preset is valid or not
34      */
35     private final AtomicBoolean valid = new AtomicBoolean(false);
36
37     /**
38      * The preset name
39      */
40     private final AtomicReference<String> name = new AtomicReference<>(null);
41
42     /**
43      * Simply creates the preset from the given ID. The preset will not be valid and the name will default to
44      * "Preset " + id
45      *
46      * @param id a preset ID between 1 and 36
47      * @throws IllegalArgumentException if id < 1 or > 36
48      */
49     public RioPreset(int id) {
50         this(id, false, "Preset " + id);
51     }
52
53     /**
54      * Creates the preset from the given ID, validity and name. If the name is empty or null, it will default to
55      * "Preset " + id
56      *
57      * @param id a preset ID between 1 and 36
58      * @param isValid true if the preset is valid, false otherwise
59      * @param name a possibly null, possibly empty preset name
60      * @throws IllegalArgumentException if id < 1 or > 32
61      */
62     public RioPreset(int id, boolean valid, String name) {
63         if (id < 1 || id > 36) {
64             throw new IllegalArgumentException("Preset ID can only be between 1 and 36");
65         }
66
67         if (StringUtils.isEmpty(name)) {
68             name = "Preset " + id;
69         }
70
71         this.id = id;
72         this.valid.set(valid);
73         this.name.set(name);
74     }
75
76     /**
77      * Returns the bank identifier this preset is for
78      *
79      * @return bank identifier between 1 and 6
80      */
81     public int getBank() {
82         return ((getId() - 1) / 6) + 1;
83     }
84
85     /**
86      * Returns the bank preset identifier this preset is for
87      *
88      * @return bank preset identifier between 1 and 6
89      */
90     public int getBankPreset() {
91         return ((getId() - 1) % 6) + 1;
92     }
93
94     /**
95      * Returns the preset identifier
96      *
97      * @return the preset identifier between 1 and 36
98      */
99     public int getId() {
100         return id;
101     }
102
103     /**
104      * Returns true if the preset is valid, false otherwise
105      *
106      * @return true if valid, false otherwise
107      */
108     public boolean isValid() {
109         return valid.get();
110     }
111
112     /**
113      * Sets whether the preset is valid (true) or not (false)
114      *
115      * @param presetValid true if valid, false otherwise
116      */
117     public void setValid(boolean presetValid) {
118         valid.set(presetValid);
119     }
120
121     /**
122      * Set's the preset name. If null or empty, will default to "Preset " + getId()
123      *
124      * @param presetName a possibly null, possibly empty preset name
125      */
126     public void setName(String presetName) {
127         name.set(StringUtils.isEmpty(presetName) ? "Preset " + getId() : presetName);
128     }
129
130     /**
131      * Returns the preset name
132      *
133      * @return a non-null, non-empty preset name
134      */
135     public String getName() {
136         return name.get();
137     }
138 }