]> git.basschouten.com Git - openhab-addons.git/blob
e033d4666a5169503aa9bb01cbba90e721333206
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.modbus.internal;
14
15 import java.util.List;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.types.State;
20 import org.openhab.core.types.TypeParser;
21 import org.osgi.framework.BundleContext;
22
23 /**
24  * Interface for Transformation
25  *
26  * @author Sami Salonen - Initial contribution
27  *
28  */
29 @NonNullByDefault
30 public interface ValueTransformation {
31
32     String transform(BundleContext context, String value);
33
34     boolean isIdentityTransform();
35
36     /**
37      * Transform state to another state using this transformation
38      *
39      * @param context
40      * @param types types to used to parse the transformation result
41      * @param state
42      * @return Transformed command, or null if no transformation was possible
43      */
44     default @Nullable State transformState(BundleContext context, List<Class<? extends State>> types, State state) {
45         // Note that even identity transformations go through the State -> String -> State steps. This does add some
46         // overhead but takes care of DecimalType -> PercentType conversions, for example.
47         final String stateAsString = state.toString();
48         final String transformed = transform(context, stateAsString);
49         return TypeParser.parseState(types, transformed);
50     }
51 }