]> git.basschouten.com Git - openhab-addons.git/blob
96886609991175d7c7b57caa0b2b8587bd6c6ce7
[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.homematic.internal.converter;
14
15 import static org.openhab.binding.homematic.internal.HomematicBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.openhab.binding.homematic.internal.converter.type.DecimalTypeConverter;
21 import org.openhab.binding.homematic.internal.converter.type.OnOffTypeConverter;
22 import org.openhab.binding.homematic.internal.converter.type.OpenClosedTypeConverter;
23 import org.openhab.binding.homematic.internal.converter.type.PercentTypeConverter;
24 import org.openhab.binding.homematic.internal.converter.type.QuantityTypeConverter;
25 import org.openhab.binding.homematic.internal.converter.type.StringTypeConverter;
26
27 /**
28  * A factory for creating converters based on the itemType.
29  *
30  * @author Gerhard Riegler - Initial contribution
31  * @author Michael Reitler - QuantityType support
32  */
33 public class ConverterFactory {
34     private static Map<String, TypeConverter<?>> converterCache = new HashMap<>();
35
36     /**
37      * Returns the converter for an itemType.
38      */
39     public static TypeConverter<?> createConverter(String itemType) throws ConverterException {
40         Class<? extends TypeConverter<?>> converterClass = null;
41
42         if (itemType.startsWith(ITEM_TYPE_NUMBER + ":")) {
43             converterClass = QuantityTypeConverter.class;
44         } else {
45             switch (itemType) {
46                 case ITEM_TYPE_SWITCH:
47                     converterClass = OnOffTypeConverter.class;
48                     break;
49                 case ITEM_TYPE_ROLLERSHUTTER:
50                 case ITEM_TYPE_DIMMER:
51                     converterClass = PercentTypeConverter.class;
52                     break;
53                 case ITEM_TYPE_CONTACT:
54                     converterClass = OpenClosedTypeConverter.class;
55                     break;
56                 case ITEM_TYPE_STRING:
57                     converterClass = StringTypeConverter.class;
58                     break;
59                 case ITEM_TYPE_NUMBER:
60                     converterClass = DecimalTypeConverter.class;
61                     break;
62             }
63         }
64
65         TypeConverter<?> converter = null;
66         if (converterClass != null) {
67             converter = converterCache.get(converterClass.getName());
68             if (converter == null) {
69                 try {
70                     converter = converterClass.getConstructor().newInstance();
71                     converterCache.put(converterClass.getName(), converter);
72                 } catch (Exception e) {
73                     // ignore
74                 }
75             }
76         }
77         if (converter == null) {
78             throw new ConverterException("Can't find a converter for type '" + itemType + "'");
79         }
80         return converter;
81     }
82 }