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.transform.vat.internal;
15 import static org.openhab.transform.vat.internal.VATTransformationConstants.*;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.core.library.types.QuantityType;
22 import org.openhab.core.transform.TransformationException;
23 import org.openhab.core.transform.TransformationService;
24 import org.openhab.core.types.UnDefType;
25 import org.osgi.service.component.annotations.Component;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * This {@link TransformationService} adds VAT to the input according to configured country.
32 * @author Jacob Laursen - Initial contribution
35 @Component(service = { TransformationService.class }, property = { "openhab.transform=VAT" })
36 public class VATTransformationService implements TransformationService {
38 private final Logger logger = LoggerFactory.getLogger(VATTransformationService.class);
41 public @Nullable String transform(String valueString, String sourceString) throws TransformationException {
42 QuantityType<?> source;
44 source = new QuantityType<>(sourceString);
45 } catch (IllegalArgumentException e) {
46 if (UnDefType.NULL.toString().equals(sourceString) || UnDefType.UNDEF.toString().equals(sourceString)) {
49 logger.warn("Input value '{}' could not be converted to a valid number", sourceString);
50 throw new TransformationException("VAT Transformation can only be used with numeric inputs", e);
54 value = new BigDecimal(valueString);
55 } catch (NumberFormatException e) {
56 String rate = RATES.get(valueString);
58 logger.warn("Input value '{}' could not be converted to a valid number or country code", valueString);
59 throw new TransformationException("VAT Transformation can only be used with numeric inputs", e);
61 value = new BigDecimal(rate);
64 return addVAT(source, value).toString();
67 private QuantityType<?> addVAT(QuantityType<?> source, BigDecimal percentage) {
68 return source.multiply(percentage.divide(new BigDecimal("100")).add(BigDecimal.ONE));