]> git.basschouten.com Git - openhab-addons.git/blob
d79bcc061f26dcbd658b8d8a7e68b43a47b8fd3e
[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.denonmarantz.internal.xml.adapters;
14
15 import static org.openhab.binding.denonmarantz.internal.DenonMarantzBindingConstants.DB_OFFSET;
16
17 import java.math.BigDecimal;
18
19 import javax.xml.bind.annotation.adapters.XmlAdapter;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23
24 /**
25  * Maps Denon volume values in db to percentage
26  *
27  * @author Jeroen Idserda - Initial contribution
28  */
29 @NonNullByDefault
30 public class VolumeAdapter extends XmlAdapter<@Nullable String, BigDecimal> {
31
32     @Override
33     public BigDecimal unmarshal(@Nullable String v) throws Exception {
34         if (v != null && !"--".equals(v.trim())) {
35             return new BigDecimal(v.trim()).add(DB_OFFSET);
36         }
37
38         return BigDecimal.ZERO;
39     }
40
41     @Override
42     public @Nullable String marshal(BigDecimal v) throws Exception {
43         if (v.equals(BigDecimal.ZERO)) {
44             return "--";
45         }
46
47         return v.subtract(DB_OFFSET).toString();
48     }
49 }