Added nullness annotations to 'ThingActions' and 'ThingHandlerService' (#1317)

* Minor code improvements; Added nullness annotations to ThingActions and ThingHandlerService
* Revert renaming of 'thingRegistry' property

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2020-01-25 16:21:47 +01:00 committed by Kai Kreuzer
parent b30c1da741
commit 8a9d007e0b
7 changed files with 38 additions and 30 deletions

View File

@ -21,9 +21,6 @@ import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.thing.binding.ThingActionsScope;
import org.openhab.core.thing.binding.ThingHandler;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
/**
* The methods of this class are made available as functions in the scripts.
@ -35,13 +32,12 @@ import org.osgi.service.component.annotations.ReferencePolicy;
public class ScriptThingActions {
private static final Map<String, ThingActions> THING_ACTIONS_MAP = new HashMap<>();
private ThingRegistry thingRegistry;
ScriptThingActions(ThingRegistry thingRegistry) {
this.thingRegistry = thingRegistry;
}
private ThingRegistry thingRegistry;
public void dispose() {
this.thingRegistry = null;
}
@ -67,7 +63,6 @@ public class ScriptThingActions {
return null;
}
@Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE)
void addThingActions(ThingActions thingActions) {
String key = getKey(thingActions);
THING_ACTIONS_MAP.put(key, thingActions);

View File

@ -18,11 +18,11 @@ import java.util.concurrent.atomic.AtomicReference;
import org.openhab.core.events.EventPublisher;
import org.openhab.core.items.ItemRegistry;
import org.openhab.core.thing.ThingRegistry;
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.model.core.ModelRepository;
import org.openhab.core.model.script.engine.ScriptEngine;
import org.openhab.core.model.script.engine.action.ActionService;
import org.openhab.core.thing.ThingRegistry;
import org.openhab.core.thing.binding.ThingActions;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;

View File

@ -15,6 +15,8 @@ package org.openhab.core.model.script.internal.engine.action;
import java.util.HashMap;
import java.util.Map;
import org.openhab.core.model.script.actions.Things;
import org.openhab.core.model.script.engine.action.ActionService;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingRegistry;
import org.openhab.core.thing.ThingStatusInfo;
@ -22,8 +24,7 @@ import org.openhab.core.thing.ThingUID;
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.thing.binding.ThingActionsScope;
import org.openhab.core.thing.binding.ThingHandler;
import org.openhab.core.model.script.actions.Things;
import org.openhab.core.model.script.engine.action.ActionService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
@ -38,23 +39,19 @@ import org.osgi.service.component.annotations.ReferencePolicy;
@Component(immediate = true)
public class ThingActionService implements ActionService {
private static final Map<String, ThingActions> THING_ACTIONS_MAP = new HashMap<>();
private static ThingRegistry thingRegistry;
private static final Map<String, ThingActions> thingActionsMap = new HashMap<>();
@Activate
public ThingActionService(final @Reference ThingRegistry thingRegistry) {
ThingActionService.thingRegistry = thingRegistry;
}
@Override
public Class<?> getActionClass() {
return Things.class;
}
@Reference
public void setThingRegistry(ThingRegistry thingRegistry) {
ThingActionService.thingRegistry = thingRegistry;
}
public void unsetThingRegistry(ThingRegistry thingRegistry) {
ThingActionService.thingRegistry = null;
}
public static ThingStatusInfo getThingStatusInfo(String thingUid) {
ThingUID uid = new ThingUID(thingUid);
Thing thing = thingRegistry.get(uid);
@ -66,13 +63,21 @@ public class ThingActionService implements ActionService {
}
}
/**
* Gets an actions instance of a certain scope for a given thing UID
*
* @param scope the action scope
* @param thingUid the UID of the thing
*
* @return actions the actions instance or null, if not available
*/
public static ThingActions getActions(String scope, String thingUid) {
ThingUID uid = new ThingUID(thingUid);
Thing thing = thingRegistry.get(uid);
if (thing != null) {
ThingHandler handler = thing.getHandler();
if (handler != null) {
ThingActions thingActions = thingActionsMap.get(getKey(scope, thingUid));
ThingActions thingActions = THING_ACTIONS_MAP.get(getKey(scope, thingUid));
return thingActions;
}
}
@ -82,12 +87,12 @@ public class ThingActionService implements ActionService {
@Reference(policy = ReferencePolicy.DYNAMIC, cardinality = ReferenceCardinality.MULTIPLE)
public void addThingActions(ThingActions thingActions) {
String key = getKey(thingActions);
thingActionsMap.put(key, thingActions);
THING_ACTIONS_MAP.put(key, thingActions);
}
public void removeThingActions(ThingActions thingActions) {
String key = getKey(thingActions);
thingActionsMap.remove(key);
THING_ACTIONS_MAP.remove(key);
}
private static String getKey(ThingActions thingActions) {
@ -108,4 +113,5 @@ public class ThingActionService implements ActionService {
ThingActionsScope scopeAnnotation = actions.getClass().getAnnotation(ThingActionsScope.class);
return scopeAnnotation.name();
}
}

View File

@ -12,9 +12,9 @@
*/
package org.openhab.core.model.script.scoping;
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.model.script.ScriptServiceUtil;
import org.openhab.core.model.script.engine.action.ActionService;
import org.openhab.core.thing.binding.ThingActions;
/**
* This is a special class loader that tries to resolve classes from available {@link ActionService}s,

View File

@ -19,12 +19,13 @@ import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.eclipse.xtext.xbase.scoping.batch.ImplicitlyImportedFeatures;
import org.joda.time.DateTime;
import org.openhab.core.library.unit.BinaryPrefix;
import org.openhab.core.library.unit.ImperialUnits;
import org.openhab.core.library.unit.MetricPrefix;
import org.openhab.core.library.unit.SIUnits;
import org.openhab.core.library.unit.SmartHomeUnits;
import org.openhab.core.thing.binding.ThingActions;
import org.openhab.core.model.persistence.extensions.PersistenceExtensions;
import org.openhab.core.model.script.actions.Audio;
import org.openhab.core.model.script.actions.BusEvent;
@ -39,8 +40,7 @@ import org.openhab.core.model.script.engine.IActionServiceProvider;
import org.openhab.core.model.script.engine.IThingActionsProvider;
import org.openhab.core.model.script.engine.action.ActionService;
import org.openhab.core.model.script.lib.NumberExtensions;
import org.eclipse.xtext.xbase.scoping.batch.ImplicitlyImportedFeatures;
import org.joda.time.DateTime;
import org.openhab.core.thing.binding.ThingActions;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@ -53,7 +53,6 @@ import com.google.inject.Singleton;
* @author Kai Kreuzer - Initial contribution
* @author Oliver Libutzki - Xtext 2.5.0 migration
*/
@SuppressWarnings("restriction")
@Singleton
public class ScriptImplicitlyImportedTypes extends ImplicitlyImportedFeatures {

View File

@ -12,11 +12,14 @@
*/
package org.openhab.core.thing.binding;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* Marker interface for Automation Actions with access to a ThingHandler
* Marker interface for Automation Actions with access to a {@link ThingHandler}.
*
* @author Stefan Triller - Initial contribution
*/
@NonNullByDefault
public interface ThingActions extends ThingHandlerService {
}

View File

@ -12,12 +12,16 @@
*/
package org.openhab.core.thing.binding;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
/**
*
* Interface for a service that provides access to a {@link ThingHandler}
* Interface for a service that provides access to a {@link ThingHandler}.
*
* @author Stefan Triller - Initial contribution
*/
@NonNullByDefault
public interface ThingHandlerService {
/**
@ -32,6 +36,7 @@ public interface ThingHandlerService {
*
* @return the {@link ThingHandler}
*/
@Nullable
ThingHandler getThingHandler();
/**