2 * Copyright (c) 2010-2022 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;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.openhab.core.storage.DeletableStorage;
23 import org.openhab.core.storage.Storage;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * The {@link PresetContainer} class manages a PresetContainer which contains all additional Presets
30 * @author Thomas Traunbauer - Initial contribution
31 * @author Kai Kreuzer - Refactored it to use storage instead of file
33 public class PresetContainer {
35 private final Logger logger = LoggerFactory.getLogger(PresetContainer.class);
37 private HashMap<Integer, ContentItem> mapOfPresets;
38 private Storage<ContentItem> storage;
41 * Creates a new instance of this class
43 public PresetContainer(Storage<ContentItem> storage) {
44 this.storage = storage;
49 this.mapOfPresets = new HashMap<>();
54 * Returns a Collection of all Presets
56 * @param operationModeType
58 public Collection<ContentItem> getAllPresets() {
59 return mapOfPresets.values();
63 * Adds a ContentItem as Preset, with presetID. Note that an eventually existing id in preset will be overwritten by
69 * @throws ContentItemNotPresetableException if ContentItem is not presetable
71 public void put(int presetID, ContentItem preset) throws ContentItemNotPresetableException {
72 preset.setPresetID(presetID);
73 if (preset.isPresetable()) {
74 mapOfPresets.put(presetID, preset);
77 throw new ContentItemNotPresetableException();
82 * Remove the Preset stored under the specified Id
86 public void remove(int presetID) {
87 mapOfPresets.remove(presetID);
92 * Returns the Preset with presetID
96 * @throws NoPresetFoundException if Preset could not be found
98 public ContentItem get(int presetID) throws NoPresetFoundException {
99 ContentItem psFound = mapOfPresets.get(presetID);
100 if (psFound != null) {
103 throw new NoPresetFoundException();
108 * Deletes all presets from the storage.
110 public void clear() {
111 if (storage instanceof DeletableStorage) {
112 ((DeletableStorage<ContentItem>) storage).delete();
114 Collection<@NonNull String> keys = storage.getKeys();
115 keys.forEach(key -> storage.remove(key));
119 private void writeToStorage() {
120 Collection<ContentItem> colletionOfPresets = getAllPresets();
121 List<ContentItem> listOfPresets = new ArrayList<>();
122 listOfPresets.addAll(colletionOfPresets);
123 // Only binding presets get saved
124 for (Iterator<ContentItem> cii = listOfPresets.iterator(); cii.hasNext();) {
125 if (cii.next().getPresetID() <= 6) {
130 if (!listOfPresets.isEmpty()) {
131 listOfPresets.forEach(item -> storage.put(String.valueOf(item.getPresetID()), item));
135 private void readFromStorage() {
136 Collection<ContentItem> items = storage.getValues();
137 for (ContentItem item : items) {
139 put(item.getPresetID(), item);
140 } catch (ContentItemNotPresetableException e) {
141 logger.debug("Item '{}' is not presetable - ignoring it.", item.getItemName());