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 public Collection<ContentItem> getAllPresets() {
56 return mapOfPresets.values();
60 * Adds a ContentItem as Preset, with presetID. Note that an eventually existing id in preset will be overwritten by
66 * @throws ContentItemNotPresetableException if ContentItem is not presetable
68 public void put(int presetID, ContentItem preset) throws ContentItemNotPresetableException {
69 preset.setPresetID(presetID);
70 if (preset.isPresetable()) {
71 mapOfPresets.put(presetID, preset);
74 throw new ContentItemNotPresetableException();
79 * Remove the Preset stored under the specified Id
83 public void remove(int presetID) {
84 mapOfPresets.remove(presetID);
89 * Returns the Preset with presetID
93 * @throws NoPresetFoundException if Preset could not be found
95 public ContentItem get(int presetID) throws NoPresetFoundException {
96 ContentItem psFound = mapOfPresets.get(presetID);
97 if (psFound != null) {
100 throw new NoPresetFoundException();
105 * Deletes all presets from the storage.
107 public void clear() {
108 if (storage instanceof DeletableStorage) {
109 ((DeletableStorage<ContentItem>) storage).delete();
111 Collection<@NonNull String> keys = storage.getKeys();
112 keys.forEach(key -> storage.remove(key));
116 private void writeToStorage() {
117 Collection<ContentItem> colletionOfPresets = getAllPresets();
118 List<ContentItem> listOfPresets = new ArrayList<>();
119 listOfPresets.addAll(colletionOfPresets);
120 // Only binding presets get saved
121 for (Iterator<ContentItem> cii = listOfPresets.iterator(); cii.hasNext();) {
122 if (cii.next().getPresetID() <= 6) {
127 if (!listOfPresets.isEmpty()) {
128 listOfPresets.forEach(item -> storage.put(String.valueOf(item.getPresetID()), item));
132 private void readFromStorage() {
133 Collection<@Nullable ContentItem> items = storage.getValues();
134 for (ContentItem item : items) {
137 put(item.getPresetID(), item);
139 } catch (ContentItemNotPresetableException e) {
140 logger.debug("Item '{}' is not presetable - ignoring it.", item.getItemName());