Skip to content

Commit 08b3fb2

Browse files
authored
Add AbstractThingHandlerDiscoveryService constructor for tests (#5100)
* Add AbstractThingHandlerDiscoveryService constructor for tests to use a different executor * Add JavaDocs * Make ThingHandler type generic Signed-off-by: Ravi Nadahar <[email protected]>
1 parent 58c9bb2 commit 08b3fb2

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

bundles/org.openhab.core.config.discovery/src/main/java/org/openhab/core/config/discovery/AbstractThingHandlerDiscoveryService.java

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.Map;
1616
import java.util.Set;
17+
import java.util.concurrent.ScheduledExecutorService;
1718

1819
import org.eclipse.jdt.annotation.NonNull;
1920
import org.eclipse.jdt.annotation.NonNullByDefault;
@@ -45,6 +46,21 @@ public abstract class AbstractThingHandlerDiscoveryService<T extends ThingHandle
4546
// initialized when the type is generic, so we have to initialize it with "something"
4647
protected @NonNullByDefault({}) T thingHandler = (@NonNull T) null;
4748

49+
/**
50+
* Creates a new instance of this class with the specified parameters.
51+
*
52+
* @param thingClazz the {@link ThingHandler} class.
53+
* @param supportedThingTypes the list of Thing types which are supported (can be {@code null}).
54+
* @param timeout the discovery timeout in seconds after which the discovery
55+
* service automatically stops its forced discovery process (>= 0).
56+
* @param backgroundDiscoveryEnabledByDefault defines, whether the default for this discovery service is to
57+
* enable background discovery or not.
58+
* @param scanInputLabel the label of the optional input parameter to start the discovery or null if no input
59+
* parameter supported.
60+
* @param scanInputDescription the description of the optional input parameter to start the discovery or null if no
61+
* input parameter supported.
62+
* @throws IllegalArgumentException if {@code timeout < 0}.
63+
*/
4864
protected AbstractThingHandlerDiscoveryService(Class<T> thingClazz, @Nullable Set<ThingTypeUID> supportedThingTypes,
4965
int timeout, boolean backgroundDiscoveryEnabledByDefault, @Nullable String scanInputLabel,
5066
@Nullable String scanInputDescription) throws IllegalArgumentException {
@@ -53,20 +69,79 @@ protected AbstractThingHandlerDiscoveryService(Class<T> thingClazz, @Nullable Se
5369
this.backgroundDiscoveryEnabled = backgroundDiscoveryEnabledByDefault;
5470
}
5571

72+
/**
73+
* Creates a new instance of this class with the specified parameters and with {@code scanInputLabel} and
74+
* {@code scanInputDescription} set to {@code null}.
75+
*
76+
* @param thingClazz the {@link ThingHandler} class.
77+
* @param supportedThingTypes the list of Thing types which are supported (can be {@code null}).
78+
* @param timeout the discovery timeout in seconds after which the discovery
79+
* service automatically stops its forced discovery process (>= 0).
80+
* @param backgroundDiscoveryEnabledByDefault defines, whether the default for this discovery service is to
81+
* enable background discovery or not.
82+
* @throws IllegalArgumentException if {@code timeout < 0}.
83+
*/
5684
protected AbstractThingHandlerDiscoveryService(Class<T> thingClazz, @Nullable Set<ThingTypeUID> supportedThingTypes,
5785
int timeout, boolean backgroundDiscoveryEnabledByDefault) throws IllegalArgumentException {
5886
this(thingClazz, supportedThingTypes, timeout, backgroundDiscoveryEnabledByDefault, null, null);
5987
}
6088

89+
/**
90+
* Creates a new instance of this class with the specified parameters and with {@code scanInputLabel} and
91+
* {@code scanInputDescription} set to {@code null}, and {@code backgroundDiscoveryEnabledByDefault} enabled.
92+
*
93+
* @param thingClazz the {@link ThingHandler} class.
94+
* @param supportedThingTypes the list of Thing types which are supported (can be {@code null}).
95+
* @param timeout the discovery timeout in seconds after which the discovery
96+
* service automatically stops its forced discovery process (>= 0).
97+
* @throws IllegalArgumentException if {@code timeout < 0}.
98+
*/
6199
protected AbstractThingHandlerDiscoveryService(Class<T> thingClazz, @Nullable Set<ThingTypeUID> supportedThingTypes,
62100
int timeout) throws IllegalArgumentException {
63101
this(thingClazz, supportedThingTypes, timeout, true);
64102
}
65103

104+
/**
105+
* Creates a new instance of this class with the specified parameters and with {@code scanInputLabel} and
106+
* {@code scanInputDescription} set to {@code null}, without any {@code supportedThingTypes}, and
107+
* {@code backgroundDiscoveryEnabledByDefault} enabled.
108+
*
109+
* @param thingClazz the {@link ThingHandler} class.
110+
* @param timeout the discovery timeout in seconds after which the discovery
111+
* service automatically stops its forced discovery process (>= 0).
112+
* @throws IllegalArgumentException if {@code timeout < 0}.
113+
*/
66114
protected AbstractThingHandlerDiscoveryService(Class<T> thingClazz, int timeout) throws IllegalArgumentException {
67115
this(thingClazz, null, timeout);
68116
}
69117

118+
/**
119+
* Creates a new instance of this class with the specified parameters.
120+
* <p>
121+
* <b>For use by tests only</b>, allows setting a different {@link ScheduledExecutorService} like
122+
* {@link org.openhab.core.util.SameThreadExecutorService} for synchronous behavior during testing.
123+
*
124+
* @param thingClazz the {@link ThingHandler} class.
125+
* @param supportedThingTypes the list of Thing types which are supported (can be {@code null}).
126+
* @param timeout the discovery timeout in seconds after which the discovery
127+
* service automatically stops its forced discovery process (>= 0).
128+
* @param backgroundDiscoveryEnabledByDefault defines, whether the default for this discovery service is to
129+
* enable background discovery or not.
130+
* @param scanInputLabel the label of the optional input parameter to start the discovery or null if no input
131+
* parameter supported.
132+
* @param scanInputDescription the description of the optional input parameter to start the discovery or null if no
133+
* input parameter supported.
134+
* @throws IllegalArgumentException if {@code timeout < 0}.
135+
*/
136+
protected AbstractThingHandlerDiscoveryService(ScheduledExecutorService scheduler, Class<T> thingClazz,
137+
@Nullable Set<ThingTypeUID> supportedThingTypes, int timeout, boolean backgroundDiscoveryEnabledByDefault,
138+
@Nullable String scanInputLabel, @Nullable String scanInputDescription) throws IllegalArgumentException {
139+
super(scheduler, supportedThingTypes, timeout, backgroundDiscoveryEnabledByDefault, scanInputLabel,
140+
scanInputDescription);
141+
this.thingClazz = thingClazz;
142+
this.backgroundDiscoveryEnabled = backgroundDiscoveryEnabledByDefault;
143+
}
144+
70145
@Override
71146
protected abstract void startScan();
72147

@@ -82,7 +157,7 @@ public void setThingHandler(ThingHandler handler) {
82157
}
83158

84159
@Override
85-
public @Nullable ThingHandler getThingHandler() {
160+
public @Nullable T getThingHandler() {
86161
return thingHandler;
87162
}
88163

0 commit comments

Comments
 (0)