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