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.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.osgi.framework.BundleContext;
24 * The {@link CascadedValueTransformationImpl} implements {@link SingleValueTransformation for a cascaded set of
27 * @author Jan N. Klug - Initial contribution
28 * @author Sami Salonen - Copied from HTTP binding to provide consistent user experience
31 public class CascadedValueTransformationImpl implements ValueTransformation {
32 private final List<SingleValueTransformation> transformations;
34 public CascadedValueTransformationImpl(@Nullable String transformationString) {
35 String transformationNonNull = transformationString == null ? "" : transformationString;
36 List<SingleValueTransformation> localTransformations = Arrays.stream(transformationNonNull.split("∩"))
37 .filter(s -> !s.isEmpty()).map(transformation -> new SingleValueTransformation(transformation))
38 .collect(Collectors.toList());
39 if (localTransformations.isEmpty()) {
40 localTransformations = List.of(new SingleValueTransformation(transformationString));
42 transformations = localTransformations;
46 public String transform(BundleContext context, String value) {
48 // process all transformations
49 for (final ValueTransformation transformation : transformations) {
50 input = transformation.transform(context, input);
56 public boolean isIdentityTransform() {
57 return transformations.stream().allMatch(SingleValueTransformation::isIdentityTransform);
61 public String toString() {
62 return "CascadedValueTransformationImpl("
63 + transformations.stream().map(SingleValueTransformation::toString).collect(Collectors.joining(" ∩ "))
67 List<SingleValueTransformation> getTransformations() {
68 return transformations;