]> git.basschouten.com Git - openhab-addons.git/blob
a0485d9fba7ede6e12d003686b09dd63d3ca8d92
[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.lametrictime.internal.api.impl;
14
15 import java.util.Base64;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.lametrictime.internal.api.dto.Icon;
20
21 /**
22  * Implementation class for icons.
23  *
24  * @author Gregory Moyer - Initial contribution
25  */
26 @NonNullByDefault
27 public abstract class AbstractDataIcon implements Icon {
28     @Nullable
29     private volatile Object CONFIGURE_FLAG;
30
31     @Nullable
32     private String type;
33
34     private byte @Nullable [] data;
35
36     protected void configure() {
37         if (CONFIGURE_FLAG == null) {
38             synchronized (this) {
39                 if (CONFIGURE_FLAG == null) {
40                     populateFields();
41                 }
42             }
43         }
44     }
45
46     protected @Nullable String getType() {
47         configure();
48         return type;
49     }
50
51     protected void setType(String type) {
52         this.type = type;
53     }
54
55     protected byte @Nullable [] getData() {
56         configure();
57         return data;
58     }
59
60     protected void setData(byte[] data) {
61         this.data = data;
62     }
63
64     @Override
65     public String toRaw() {
66         return new StringBuilder().append("data:").append(getType()).append(";base64,")
67                 .append(Base64.getEncoder().encodeToString(getData())).toString();
68     }
69
70     protected abstract void populateFields();
71 }