Skip to content

Commit 511f758

Browse files
authored
Refactor ThingStatusTriggerHandler for improved clarity and modernization (#4938)
- Removed unnecessary instance fields (`thingUID` and `bundleContext`) and replaced with local variables where appropriate. - Simplified `eventTopicFilter` initialization by using local `thingUID` variable. - Applied pattern matching for `instanceof` to reduce explicit casting in `receive()` method. - Cleaned up unused code and improved readability. Signed-off-by: Oleg Andreych <kjiec4@gmail.com>
1 parent 6f7d46c commit 511f758

1 file changed

Lines changed: 3 additions & 7 deletions

File tree

bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/ThingStatusTriggerHandler.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,28 @@ public class ThingStatusTriggerHandler extends BaseTriggerModuleHandler implemen
5757

5858
private final Logger logger = LoggerFactory.getLogger(ThingStatusTriggerHandler.class);
5959

60-
private final String thingUID;
6160
private @Nullable final String status;
6261
private @Nullable final String previousStatus;
6362
private final Set<String> types;
64-
private final BundleContext bundleContext;
6563

6664
private final ServiceRegistration<?> eventSubscriberRegistration;
6765
private final TopicEventFilter eventTopicFilter;
6866

6967
public ThingStatusTriggerHandler(Trigger module, BundleContext bundleContext) {
7068
super(module);
71-
this.thingUID = (String) module.getConfiguration().get(CFG_THING_UID);
69+
String thingUID = (String) module.getConfiguration().get(CFG_THING_UID);
7270
this.status = (String) module.getConfiguration().get(CFG_STATUS);
7371
this.previousStatus = (String) module.getConfiguration().get(CFG_PREVIOUS_STATUS);
7472
if (UPDATE_MODULE_TYPE_ID.equals(module.getTypeUID())) {
7573
this.types = Set.of(ThingStatusInfoEvent.TYPE);
7674
} else {
7775
this.types = Set.of(ThingStatusInfoChangedEvent.TYPE);
7876
}
79-
this.bundleContext = bundleContext;
8077

8178
this.eventTopicFilter = new TopicEventFilter(
8279
"^openhab/things/" + thingUID.replace("?", ".?").replace("*", ".*?") + "/.*$");
8380

84-
eventSubscriberRegistration = this.bundleContext.registerService(EventSubscriber.class.getName(), this, null);
81+
eventSubscriberRegistration = bundleContext.registerService(EventSubscriber.class.getName(), this, null);
8582
}
8683

8784
@Override
@@ -97,11 +94,10 @@ public Set<String> getSubscribedEventTypes() {
9794
@Override
9895
public void receive(Event event) {
9996
final ModuleHandlerCallback callback = this.callback;
100-
if (!(callback instanceof TriggerHandlerCallback)) {
97+
if (!(callback instanceof TriggerHandlerCallback thCallback)) {
10198
return;
10299
}
103100

104-
TriggerHandlerCallback thCallback = (TriggerHandlerCallback) callback;
105101
logger.trace("Received Event: Source: {} Topic: {} Type: {} Payload: {}", event.getSource(), event.getTopic(),
106102
event.getType(), event.getPayload());
107103
Map<String, Object> values = new HashMap<>();

0 commit comments

Comments
 (0)