2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.modbus.internal;
15 import java.util.List;
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;
24 * Interface for Transformation
26 * @author Sami Salonen - Initial contribution
30 public interface ValueTransformation {
32 String transform(BundleContext context, String value);
34 boolean isIdentityTransform();
37 * Transform state to another state using this transformation
40 * @param types types to used to parse the transformation result
42 * @return Transformed command, or null if no transformation was possible
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);