]> git.basschouten.com Git - openhab-addons.git/blob
655b36a30ea6742636d5a8e4ce97085ebed96952
[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.ihc.internal.converters;
14
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
17 import org.openhab.binding.ihc.internal.ws.projectfile.IhcEnumValue;
18 import org.openhab.binding.ihc.internal.ws.resourcevalues.WSEnumValue;
19 import org.openhab.core.library.types.StringType;
20
21 /**
22  * StringType <-> WSEnumValue converter.
23  *
24  * @author Pauli Anttila - Initial contribution
25  */
26 public class StringTypeWSEnumValueConverter implements Converter<WSEnumValue, StringType> {
27
28     @Override
29     public StringType convertFromResourceValue(@NonNull WSEnumValue from, @NonNull ConverterAdditionalInfo convertData)
30             throws ConversionException {
31         return new StringType(from.enumName);
32     }
33
34     @Override
35     public WSEnumValue convertFromOHType(@NonNull StringType from, @NonNull WSEnumValue value,
36             @NonNull ConverterAdditionalInfo convertData) throws ConversionException {
37         if (convertData.getEnumValues() != null) {
38             for (IhcEnumValue item : convertData.getEnumValues()) {
39                 if (item.getName().equals(from.toString())) {
40                     return new WSEnumValue(value.resourceID, value.definitionTypeID, item.getId(), item.getName());
41                 }
42             }
43             throw new ConversionException("Can't find enum value for string " + value.toString());
44         } else {
45             throw new ConversionException("Enum list is null");
46         }
47     }
48 }