]> git.basschouten.com Git - openhab-addons.git/blob
fcd160ef33fdf3adbc5cfdd91a4b56ef36c20d4a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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     public Collection<ContentItem> getAllPresets() {
56         return mapOfPresets.values();
57     }
58
59     /**
60      * Adds a ContentItem as Preset, with presetID. Note that an eventually existing id in preset will be overwritten by
61      * presetID
62      *
63      * @param presetID
64      * @param preset
65      *
66      * @throws ContentItemNotPresetableException if ContentItem is not presetable
67      */
68     public void put(int presetID, ContentItem preset) throws ContentItemNotPresetableException {
69         preset.setPresetID(presetID);
70         if (preset.isPresetable()) {
71             mapOfPresets.put(presetID, preset);
72             writeToStorage();
73         } else {
74             throw new ContentItemNotPresetableException();
75         }
76     }
77
78     /**
79      * Remove the Preset stored under the specified Id
80      * 
81      * @param presetID
82      */
83     public void remove(int presetID) {
84         mapOfPresets.remove(presetID);
85         writeToStorage();
86     }
87
88     /**
89      * Returns the Preset with presetID
90      *
91      * @param presetID
92      *
93      * @throws NoPresetFoundException if Preset could not be found
94      */
95     public ContentItem get(int presetID) throws NoPresetFoundException {
96         ContentItem psFound = mapOfPresets.get(presetID);
97         if (psFound != null) {
98             return psFound;
99         } else {
100             throw new NoPresetFoundException();
101         }
102     }
103
104     /**
105      * Deletes all presets from the storage.
106      */
107     public void clear() {
108         if (storage instanceof DeletableStorage) {
109             ((DeletableStorage<ContentItem>) storage).delete();
110         } else {
111             Collection<@NonNull String> keys = storage.getKeys();
112             keys.forEach(key -> storage.remove(key));
113         }
114     }
115
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) {
123                 cii.remove();
124             }
125         }
126
127         if (!listOfPresets.isEmpty()) {
128             listOfPresets.forEach(item -> storage.put(String.valueOf(item.getPresetID()), item));
129         }
130     }
131
132     private void readFromStorage() {
133         Collection<@Nullable ContentItem> items = storage.getValues();
134         for (ContentItem item : items) {
135             try {
136                 if (item != null) {
137                     put(item.getPresetID(), item);
138                 }
139             } catch (ContentItemNotPresetableException e) {
140                 logger.debug("Item '{}' is not presetable - ignoring it.", item.getItemName());
141             }
142         }
143     }
144 }