]> git.basschouten.com Git - openhab-addons.git/blob
a51515f6bce6311984646af7f0c277d02b762afb
[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.serial.internal.transform;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Optional;
18 import java.util.function.Function;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.transform.TransformationService;
24
25 /**
26  * The {@link CascadedValueTransformationImpl} implements {@link ValueTransformation for a cascaded set of
27  * transformations}
28  *
29  * @author Jan N. Klug - Initial contribution
30  * @author Mike Major - Copied from HTTP binding to provide consistent user experience
31  */
32 @NonNullByDefault
33 public class CascadedValueTransformationImpl implements ValueTransformation {
34     private final List<ValueTransformation> transformations;
35
36     public CascadedValueTransformationImpl(final String transformationString,
37             final Function<String, @Nullable TransformationService> transformationServiceSupplier) {
38         transformations = Arrays.stream(transformationString.split("∩")).filter(s -> !s.isEmpty())
39                 .map(transformation -> new SingleValueTransformation(transformation, transformationServiceSupplier))
40                 .collect(Collectors.toList());
41     }
42
43     @Override
44     public Optional<String> apply(final String value) {
45         Optional<String> valueOptional = Optional.of(value);
46
47         // process all transformations
48         for (final ValueTransformation transformation : transformations) {
49             valueOptional = valueOptional.flatMap(transformation::apply);
50         }
51
52         return valueOptional;
53     }
54 }