]> git.basschouten.com Git - openhab-addons.git/blob
0e5bfa86613ee41b0c60e3f96fa71821cff0277c
[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.common.impl;
14
15 import javax.ws.rs.client.Client;
16 import javax.ws.rs.client.ClientBuilder;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20
21 import com.google.gson.Gson;
22
23 /**
24  * Abstract class for clients.
25  *
26  * @author Gregory Moyer - Initial contribution
27  */
28 @NonNullByDefault
29 public abstract class AbstractClient {
30     protected final ClientBuilder clientBuilder;
31
32     @Nullable
33     private volatile Client client;
34     @Nullable
35     private volatile Gson gson;
36
37     public AbstractClient() {
38         this(ClientBuilder.newBuilder());
39     }
40
41     public AbstractClient(ClientBuilder clientBuilder) {
42         this.clientBuilder = clientBuilder;
43     }
44
45     protected @Nullable Client getClient() {
46         if (client == null) {
47             synchronized (this) {
48                 if (client == null) {
49                     client = createClient();
50                 }
51             }
52         }
53
54         return client;
55     }
56
57     protected @Nullable Gson getGson() {
58         if (gson == null) {
59             synchronized (this) {
60                 if (gson == null) {
61                     gson = createGson();
62                 }
63             }
64         }
65
66         return gson;
67     }
68
69     protected abstract Client createClient();
70
71     protected Gson createGson() {
72         return GsonGenerator.create();
73     }
74
75     protected ClientBuilder getClientBuilder() {
76         return clientBuilder;
77     }
78 }