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