]> git.basschouten.com Git - openhab-addons.git/blob
eeb5d70cc77ff8b2f2b25eba8b8f787ac5baa019
[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
14 package org.openhab.binding.ecovacs.internal.util;
15
16 import java.util.HashMap;
17 import java.util.Optional;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20
21 /**
22  * @author Danny Baumann - Initial contribution
23  */
24 @NonNullByDefault
25 public class StateOptionMapping<T extends Enum<T>> extends HashMap<T, StateOptionEntry<T>> {
26     private static final long serialVersionUID = -6828690091106259902L;
27
28     public String getMappedValue(T key) {
29         StateOptionEntry<T> entry = get(key);
30         if (entry != null) {
31             return entry.value;
32         }
33         throw new IllegalArgumentException("No mapping for key " + key);
34     }
35
36     public Optional<T> findMappedEnumValue(String value) {
37         return entrySet().stream().filter(entry -> entry.getValue().value.equals(value)).map(entry -> entry.getKey())
38                 .findFirst();
39     }
40
41     @SafeVarargs
42     public static <T extends Enum<T>> StateOptionMapping<T> of(StateOptionEntry<T>... entries) {
43         StateOptionMapping<T> map = new StateOptionMapping<>();
44         for (StateOptionEntry<T> entry : entries) {
45             map.put(entry.enumValue, entry);
46         }
47         return map;
48     }
49 }