-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathDeviceFactory.java
More file actions
44 lines (34 loc) · 1.45 KB
/
DeviceFactory.java
File metadata and controls
44 lines (34 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* Copyright (C) 2018 Matthias Bolte <[email protected]>
*
* Redistribution and use in source and binary forms of this file,
* with or without modification, are permitted. See the Creative
* Commons Zero (CC0 1.0) License for more details.
*/
package com.tinkerforge;
import java.util.Hashtable;
import java.util.ServiceLoader;
public class DeviceFactory {
private static Hashtable<Integer, DeviceProvider> deviceProviders = new Hashtable<Integer, DeviceProvider>();
static {
for (DeviceProvider deviceProvider: ServiceLoader.load(DeviceProvider.class)) {
deviceProviders.put(deviceProvider.getDeviceIdentifier(), deviceProvider);
}
}
private static DeviceProvider getDeviceProvider(int deviceIdentifier) {
DeviceProvider deviceProvider = deviceProviders.get(deviceIdentifier);
if (deviceProvider == null) {
throw new IllegalArgumentException("Unknown device identifier: " + deviceIdentifier);
}
return deviceProvider;
}
public static Class<? extends Device> getDeviceClass(int deviceIdentifier) {
return getDeviceProvider(deviceIdentifier).getDeviceClass();
}
public static String getDeviceDisplayName(int deviceIdentifier) {
return getDeviceProvider(deviceIdentifier).getDeviceDisplayName();
}
public static Device createDevice(int deviceIdentifier, String uid, IPConnection ipcon) throws Exception {
return getDeviceClass(deviceIdentifier).getConstructor(String.class, IPConnection.class).newInstance(uid, ipcon);
}
}