]> git.basschouten.com Git - openhab-addons.git/blob
6658b53e7213d7033fd8bb51ad15e27950ae217d
[openhab-addons.git] /
1 /**
2  * Copyright 2017-2018 Gregory Moyer and contributors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openhab.binding.lametrictime.api.impl;
17
18 import java.util.Base64;
19
20 import org.openhab.binding.lametrictime.api.model.Icon;
21
22 public abstract class AbstractDataIcon implements Icon
23 {
24     private volatile Object CONFIGURE_FLAG;
25
26     private String type;
27     private byte[] data;
28
29     protected void configure()
30     {
31         if (CONFIGURE_FLAG == null)
32         {
33             synchronized (this)
34             {
35                 if (CONFIGURE_FLAG == null)
36                 {
37                     populateFields();
38                 }
39             }
40         }
41     }
42
43     protected String getType()
44     {
45         configure();
46         return type;
47     }
48
49     protected void setType(String type)
50     {
51         this.type = type;
52     }
53
54     protected byte[] getData()
55     {
56         configure();
57         return data;
58     }
59
60     protected void setData(byte[] data)
61     {
62         this.data = data;
63     }
64
65     @Override
66     public String toRaw()
67     {
68         return new StringBuilder().append("data:")
69                                   .append(getType())
70                                   .append(";base64,")
71                                   .append(Base64.getEncoder().encodeToString(getData()))
72                                   .toString();
73     }
74
75     protected abstract void populateFields();
76 }