2 * Copyright (c) 2010-2023 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.Collections;
17 import java.util.List;
18 import java.util.stream.Collectors;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.osgi.framework.BundleContext;
25 * The {@link CascadedValueTransformationImpl} implements {@link SingleValueTransformation for a cascaded set of
28 * @author Jan N. Klug - Initial contribution
29 * @author Sami Salonen - Copied from HTTP binding to provide consistent user experience
32 public class CascadedValueTransformationImpl implements ValueTransformation {
33 private final List<SingleValueTransformation> transformations;
35 public CascadedValueTransformationImpl(@Nullable String transformationString) {
36 String transformationNonNull = transformationString == null ? "" : transformationString;
37 List<SingleValueTransformation> localTransformations = Arrays.stream(transformationNonNull.split("∩"))
38 .filter(s -> !s.isEmpty()).map(transformation -> new SingleValueTransformation(transformation))
39 .collect(Collectors.toList());
40 if (localTransformations.isEmpty()) {
41 localTransformations = Collections.singletonList(new SingleValueTransformation(transformationString));
43 transformations = localTransformations;
47 public String transform(BundleContext context, String value) {
49 // process all transformations
50 for (final ValueTransformation transformation : transformations) {
51 input = transformation.transform(context, input);
57 public boolean isIdentityTransform() {
58 return transformations.stream().allMatch(SingleValueTransformation::isIdentityTransform);
62 public String toString() {
63 return "CascadedValueTransformationImpl("
64 + transformations.stream().map(SingleValueTransformation::toString).collect(Collectors.joining(" ∩ "))
68 List<SingleValueTransformation> getTransformations() {
69 return transformations;