Add helpers for constructing event sources (#5078)

Signed-off-by: Cody Cutrer <cody@cutrer.us>
This commit is contained in:
Cody Cutrer
2025-10-24 00:01:37 +02:00
committed by GitHub
parent 32868df4d4
commit e1a1d6c166
2 changed files with 90 additions and 0 deletions
@@ -22,6 +22,8 @@ import org.eclipse.jdt.annotation.Nullable;
*/
@NonNullByDefault
public abstract class AbstractEvent implements Event {
public static final String ACTOR_SEPARATOR = "$";
public static final String DELEGATION_SEPARATOR = "=>";
private final String topic;
@@ -95,4 +97,45 @@ public abstract class AbstractEvent implements Event {
}
return true;
}
/**
* Utility method to build a source string from a package and an optional actor.
*
* @param packageName the package (such as org.openhab.core.thing or org.openhab.binding.matter)
* @param actor the actor
* @return the final source string
*/
public static String buildSource(String packageName, @Nullable String actor) {
if (actor == null || actor.isEmpty()) {
return packageName;
}
return packageName + ACTOR_SEPARATOR + actor;
}
/**
* Utility method to build a delegated source string from an original source and a package
*
* @param originalSource the original source (may be null)
* @param packageName the package (such as org.openhab.core.thing or org.openhab.binding.matter)
* @return the final source string
*/
public static String buildDelegatedSource(@Nullable String originalSource, String packageName) {
if (originalSource == null || originalSource.isEmpty()) {
return packageName;
}
return originalSource + DELEGATION_SEPARATOR + packageName;
}
/**
* Utility method to build a delegated source string from an original source, a package and an optional actor.
*
* @param originalSource the original source (may be null)
* @param packageName the package (such as org.openhab.core.thing or org.openhab.binding.matter)
* @param actor the actor
* @return the final source string
*/
public static String buildDelegatedSource(@Nullable String originalSource, String packageName,
@Nullable String actor) {
return buildDelegatedSource(originalSource, buildSource(packageName, actor));
}
}
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2010-2025 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.core.events;
import static org.junit.jupiter.api.Assertions.*;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
/**
* {@link AbstractEventTest} tests the utility methods in {@link org.openhab.core.events.AbstractEvent}.
*
* @author Cody Cutrer - Initial contribution
*/
@NonNullByDefault
public class AbstractEventTest {
@Test
public void testBuildSource() throws Exception {
assertEquals(AbstractEvent.buildSource("org.openhab.core.thing", null), "org.openhab.core.thing");
assertEquals(AbstractEvent.buildSource("org.openhab.core.thing", "actor"), "org.openhab.core.thing$actor");
}
@Test
public void testBuildDelegatedSource() throws Exception {
assertEquals(AbstractEvent.buildDelegatedSource(null, "org.openhab.core.thing"), "org.openhab.core.thing");
assertEquals(AbstractEvent.buildDelegatedSource("org.openhab.binding.matter", "org.openhab.core.thing"),
"org.openhab.binding.matter=>org.openhab.core.thing");
assertEquals(AbstractEvent.buildDelegatedSource(null, "org.openhab.core.thing", "actor"),
"org.openhab.core.thing$actor");
assertEquals(
AbstractEvent.buildDelegatedSource("org.openhab.binding.matter", "org.openhab.core.thing", "actor"),
"org.openhab.binding.matter=>org.openhab.core.thing$actor");
assertEquals(AbstractEvent.buildDelegatedSource("org.openhab.binding.matter$originalActor",
"org.openhab.core.thing", "actor"),
"org.openhab.binding.matter$originalActor=>org.openhab.core.thing$actor");
}
}