]> git.basschouten.com Git - openhab-addons.git/blob
9ca8db11d373629e3c048c33f262c1965c8a2b48
[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.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 /**
22  * Maps Denon volume values in db to percentage
23  *
24  * @author Jeroen Idserda - Initial contribution
25  */
26 public class VolumeAdapter extends XmlAdapter<String, BigDecimal> {
27
28     @Override
29     public BigDecimal unmarshal(String v) throws Exception {
30         if (v != null && !"--".equals(v.trim())) {
31             return new BigDecimal(v.trim()).add(DB_OFFSET);
32         }
33
34         return BigDecimal.ZERO;
35     }
36
37     @Override
38     public String marshal(BigDecimal v) throws Exception {
39         if (v.equals(BigDecimal.ZERO)) {
40             return "--";
41         }
42
43         return v.subtract(DB_OFFSET).toString();
44     }
45 }