]> git.basschouten.com Git - openhab-addons.git/blob
daab54000f9b69b6550554a2a1cbab2e46962fa7
[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.unifi.internal.api.util;
14
15 import java.lang.reflect.Type;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.unifi.internal.api.cache.UniFiControllerCache;
20 import org.openhab.binding.unifi.internal.api.dto.UniFiClient;
21 import org.openhab.binding.unifi.internal.api.dto.UniFiUnknownClient;
22 import org.openhab.binding.unifi.internal.api.dto.UniFiWiredClient;
23 import org.openhab.binding.unifi.internal.api.dto.UniFiWirelessClient;
24
25 import com.google.gson.InstanceCreator;
26 import com.google.gson.JsonSyntaxException;
27
28 /**
29  * The {@link UniFiClientInstanceCreator} creates instances of {@link UniFiClient}s during the JSON unmarshalling of
30  * controller responses.
31  *
32  * @author Matthew Bowman - Initial contribution
33  */
34 @NonNullByDefault
35 public class UniFiClientInstanceCreator implements InstanceCreator<UniFiClient> {
36
37     private final UniFiControllerCache cache;
38
39     public UniFiClientInstanceCreator(final UniFiControllerCache cache) {
40         this.cache = cache;
41     }
42
43     @Override
44     public UniFiClient createInstance(final @Nullable Type type) {
45         if (UniFiUnknownClient.class.equals(type)) {
46             return new UniFiUnknownClient(cache);
47         }
48         if (UniFiWirelessClient.class.equals(type)) {
49             return new UniFiWirelessClient(cache);
50         }
51         if (UniFiWiredClient.class.equals(type)) {
52             return new UniFiWiredClient(cache);
53         } else {
54             throw new JsonSyntaxException("Expected a UniFi Client type, but got " + type);
55         }
56     }
57 }