]> git.basschouten.com Git - openhab-addons.git/blob
dfc033fe2ed3946d0f55a64c0e26a1fa23faba98
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.bosesoundtouch.internal;
14
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;
20 import java.util.Map;
21
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;
29
30 /**
31  * The {@link PresetContainer} class manages a PresetContainer which contains all additional Presets
32  *
33  * @author Thomas Traunbauer - Initial contribution
34  * @author Kai Kreuzer - Refactored it to use storage instead of file
35  */
36 @NonNullByDefault
37 public class PresetContainer {
38
39     private final Logger logger = LoggerFactory.getLogger(PresetContainer.class);
40
41     private final Map<Integer, ContentItem> mapOfPresets = new HashMap<>();
42     private Storage<ContentItem> storage;
43
44     /**
45      * Creates a new instance of this class
46      */
47     public PresetContainer(Storage<ContentItem> storage) {
48         this.storage = storage;
49         readFromStorage();
50     }
51
52     /**
53      * Returns a Collection of all Presets
54      *
55      * @param operationModeType
56      */
57     public Collection<ContentItem> getAllPresets() {
58         return mapOfPresets.values();
59     }
60
61     /**
62      * Adds a ContentItem as Preset, with presetID. Note that an eventually existing id in preset will be overwritten by
63      * presetID
64      *
65      * @param presetID
66      * @param preset
67      *
68      * @throws ContentItemNotPresetableException if ContentItem is not presetable
69      */
70     public void put(int presetID, ContentItem preset) throws ContentItemNotPresetableException {
71         preset.setPresetID(presetID);
72         if (preset.isPresetable()) {
73             mapOfPresets.put(presetID, preset);
74             writeToStorage();
75         } else {
76             throw new ContentItemNotPresetableException();
77         }
78     }
79
80     /**
81      * Remove the Preset stored under the specified Id
82      * 
83      * @param presetID
84      */
85     public void remove(int presetID) {
86         mapOfPresets.remove(presetID);
87         writeToStorage();
88     }
89
90     /**
91      * Returns the Preset with presetID
92      *
93      * @param presetID
94      *
95      * @throws NoPresetFoundException if Preset could not be found
96      */
97     public ContentItem get(int presetID) throws NoPresetFoundException {
98         ContentItem psFound = mapOfPresets.get(presetID);
99         if (psFound != null) {
100             return psFound;
101         } else {
102             throw new NoPresetFoundException();
103         }
104     }
105
106     /**
107      * Deletes all presets from the storage.
108      */
109     public void clear() {
110         if (storage instanceof DeletableStorage) {
111             ((DeletableStorage<ContentItem>) storage).delete();
112         } else {
113             Collection<@NonNull String> keys = storage.getKeys();
114             keys.forEach(key -> storage.remove(key));
115         }
116     }
117
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) {
125                 cii.remove();
126             }
127         }
128
129         if (!listOfPresets.isEmpty()) {
130             listOfPresets.forEach(item -> storage.put(String.valueOf(item.getPresetID()), item));
131         }
132     }
133
134     private void readFromStorage() {
135         Collection<@Nullable ContentItem> items = storage.getValues();
136         for (ContentItem item : items) {
137             try {
138                 if (item != null) {
139                     put(item.getPresetID(), item);
140                 }
141             } catch (ContentItemNotPresetableException e) {
142                 logger.debug("Item '{}' is not presetable - ignoring it.", item.getItemName());
143             }
144         }
145     }
146 }