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.bosesoundtouch.internal;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.Iterator;
19 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.core.storage.DeletableStorage;
26 import org.openhab.core.storage.Storage;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link PresetContainer} class manages a PresetContainer which contains all additional Presets
33 * @author Thomas Traunbauer - Initial contribution
34 * @author Kai Kreuzer - Refactored it to use storage instead of file
37 public class PresetContainer {
39 private final Logger logger = LoggerFactory.getLogger(PresetContainer.class);
41 private final Map<Integer, ContentItem> mapOfPresets = new HashMap<>();
42 private Storage<ContentItem> storage;
45 * Creates a new instance of this class
47 public PresetContainer(Storage<ContentItem> storage) {
48 this.storage = storage;
53 * Returns a Collection of all Presets
55 * @param operationModeType
57 public Collection<ContentItem> getAllPresets() {
58 return mapOfPresets.values();
62 * Adds a ContentItem as Preset, with presetID. Note that an eventually existing id in preset will be overwritten by
68 * @throws ContentItemNotPresetableException if ContentItem is not presetable
70 public void put(int presetID, ContentItem preset) throws ContentItemNotPresetableException {
71 preset.setPresetID(presetID);
72 if (preset.isPresetable()) {
73 mapOfPresets.put(presetID, preset);
76 throw new ContentItemNotPresetableException();
81 * Remove the Preset stored under the specified Id
85 public void remove(int presetID) {
86 mapOfPresets.remove(presetID);
91 * Returns the Preset with presetID
95 * @throws NoPresetFoundException if Preset could not be found
97 public ContentItem get(int presetID) throws NoPresetFoundException {
98 ContentItem psFound = mapOfPresets.get(presetID);
99 if (psFound != null) {
102 throw new NoPresetFoundException();
107 * Deletes all presets from the storage.
109 public void clear() {
110 if (storage instanceof DeletableStorage) {
111 ((DeletableStorage<ContentItem>) storage).delete();
113 Collection<@NonNull String> keys = storage.getKeys();
114 keys.forEach(key -> storage.remove(key));
118 private void writeToStorage() {
119 Collection<ContentItem> colletionOfPresets = getAllPresets();
120 List<ContentItem> listOfPresets = new ArrayList<>();
121 listOfPresets.addAll(colletionOfPresets);
122 // Only binding presets get saved
123 for (Iterator<ContentItem> cii = listOfPresets.iterator(); cii.hasNext();) {
124 if (cii.next().getPresetID() <= 6) {
129 if (!listOfPresets.isEmpty()) {
130 listOfPresets.forEach(item -> storage.put(String.valueOf(item.getPresetID()), item));
134 private void readFromStorage() {
135 Collection<@Nullable ContentItem> items = storage.getValues();
136 for (ContentItem item : items) {
139 put(item.getPresetID(), item);
141 } catch (ContentItemNotPresetableException e) {
142 logger.debug("Item '{}' is not presetable - ignoring it.", item.getItemName());