]> git.basschouten.com Git - openhab-addons.git/blob
053671a5e038e5fc53660019d4c145611aff3dd1
[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.Arrays;
16 import java.util.List;
17 import java.util.stream.Collectors;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.osgi.framework.BundleContext;
22
23 /**
24  * The {@link CascadedValueTransformationImpl} implements {@link SingleValueTransformation for a cascaded set of
25  * transformations}
26  *
27  * @author Jan N. Klug - Initial contribution
28  * @author Sami Salonen - Copied from HTTP binding to provide consistent user experience
29  */
30 @NonNullByDefault
31 public class CascadedValueTransformationImpl implements ValueTransformation {
32     private final List<SingleValueTransformation> transformations;
33
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));
41         }
42         transformations = localTransformations;
43     }
44
45     @Override
46     public String transform(BundleContext context, String value) {
47         String input = value;
48         // process all transformations
49         for (final ValueTransformation transformation : transformations) {
50             input = transformation.transform(context, input);
51         }
52         return input;
53     }
54
55     @Override
56     public boolean isIdentityTransform() {
57         return transformations.stream().allMatch(SingleValueTransformation::isIdentityTransform);
58     }
59
60     @Override
61     public String toString() {
62         return "CascadedValueTransformationImpl("
63                 + transformations.stream().map(SingleValueTransformation::toString).collect(Collectors.joining(" ∩ "))
64                 + ")";
65     }
66
67     List<SingleValueTransformation> getTransformations() {
68         return transformations;
69     }
70 }