2 * Copyright (c) 2010-2023 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.binding.russound.internal.rio.models;
15 import java.util.concurrent.atomic.AtomicBoolean;
16 import java.util.concurrent.atomic.AtomicReference;
18 import org.eclipse.jdt.annotation.Nullable;
21 * Simple model of a RIO Preset and it's attributes. Please note this class is used to
22 * serialize/deserialize to JSON.
24 * @author Tim Roberts - Initial contribution
26 public class RioPreset {
33 * Whether the preset is valid or not
35 private final AtomicBoolean valid = new AtomicBoolean(false);
40 private final AtomicReference<String> name = new AtomicReference<>(null);
43 * Simply creates the preset from the given ID. The preset will not be valid and the name will default to
46 * @param id a preset ID between 1 and 36
47 * @throws IllegalArgumentException if id {@literal < 1} or > 36
49 public RioPreset(int id) {
50 this(id, false, "Preset " + id);
54 * Creates the preset from the given ID, validity and name. If the name is empty or null, it will default to
57 * @param id a preset ID between 1 and 36
58 * @param valid true if the preset is valid, false otherwise
59 * @param name a possibly null, possibly empty preset name
60 * @throws IllegalArgumentException if id {@literal < 1} or > 32
62 public RioPreset(int id, boolean valid, @Nullable String name) {
63 if (id < 1 || id > 36) {
64 throw new IllegalArgumentException("Preset ID can only be between 1 and 36");
68 this.valid.set(valid);
69 this.name.set(name == null || name.isEmpty() ? "Preset " + id : name);
73 * Returns the bank identifier this preset is for
75 * @return bank identifier between 1 and 6
77 public int getBank() {
78 return ((getId() - 1) / 6) + 1;
82 * Returns the bank preset identifier this preset is for
84 * @return bank preset identifier between 1 and 6
86 public int getBankPreset() {
87 return ((getId() - 1) % 6) + 1;
91 * Returns the preset identifier
93 * @return the preset identifier between 1 and 36
100 * Returns true if the preset is valid, false otherwise
102 * @return true if valid, false otherwise
104 public boolean isValid() {
109 * Sets whether the preset is valid (true) or not (false)
111 * @param presetValid true if valid, false otherwise
113 public void setValid(boolean presetValid) {
114 valid.set(presetValid);
118 * Set's the preset name. If null or empty, will default to "Preset " + getId()
120 * @param presetName a possibly null, possibly empty preset name
122 public void setName(@Nullable String presetName) {
123 name.set(presetName == null || presetName.isEmpty() ? "Preset " + getId() : presetName);
127 * Returns the preset name
129 * @return a non-null, non-empty preset name
131 public String getName() {