Skip to content

Commit

Permalink
Add null annotations to registries (openhab#1134)
Browse files Browse the repository at this point in the history
Adds null annotations to all registries, the interfaces they implement and a few other classes.

Also-by: Christoph Weitkamp <github@christophweitkamp.de>
Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored and cweitkamp committed Oct 23, 2019
1 parent 09170d1 commit 2430256
Show file tree
Hide file tree
Showing 62 changed files with 728 additions and 586 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public interface AudioSink {
* @return a localized string to be used in UIs
*/
@Nullable
String getLabel(Locale locale);
String getLabel(@Nullable Locale locale);

/**
* Processes the passed {@link AudioStream}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.audio.AudioFormat;
import org.eclipse.smarthome.core.audio.AudioSink;
import org.eclipse.smarthome.core.audio.AudioStream;
Expand All @@ -35,14 +37,15 @@
* @author Christoph Weitkamp - Added parameter to adjust the volume
* @author Wouter Born - Migrate tests from Groovy to Java
*/
@NonNullByDefault
public class AudioSinkFake implements AudioSink {

public AudioStream audioStream;
public AudioFormat audioFormat;
public @Nullable AudioStream audioStream;
public @Nullable AudioFormat audioFormat;

public boolean isStreamProcessed;
public boolean isStreamStopped;
public PercentType volume;
public @Nullable PercentType volume;
public boolean isIOExceptionExpected;
public boolean isUnsupportedAudioFormatExceptionExpected;
public boolean isUnsupportedAudioStreamExceptionExpected;
Expand All @@ -58,12 +61,12 @@ public String getId() {
}

@Override
public String getLabel(Locale locale) {
public @Nullable String getLabel(@Nullable Locale locale) {
return "testSinkLabel";
}

@Override
public void process(AudioStream audioStream)
public void process(@Nullable AudioStream audioStream)
throws UnsupportedAudioFormatException, UnsupportedAudioStreamException {
if (isUnsupportedAudioFormatExceptionExpected) {
throw new UnsupportedAudioFormatException("Expected audio format exception", null);
Expand All @@ -80,7 +83,7 @@ public void process(AudioStream audioStream)
isStreamProcessed = true;
}

public AudioFormat getAudioFormat() {
public @Nullable AudioFormat getAudioFormat() {
return audioFormat;
}

Expand All @@ -96,10 +99,11 @@ public Set<Class<? extends AudioStream>> getSupportedStreams() {

@Override
public PercentType getVolume() throws IOException {
if (isIOExceptionExpected) {
PercentType localVolume = volume;
if (localVolume == null || isIOExceptionExpected) {
throw new IOException();
}
return volume;
return localVolume;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.stream.Stream;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.ConfigConstants;
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter;
import org.eclipse.smarthome.config.core.ConfigDescriptionParameter.Type;
Expand All @@ -44,14 +46,15 @@
* @author Kai Kreuzer - Initial contribution
* @author Simon Kaufmann - added "say" action
*/
@NonNullByDefault
@Component(immediate = true)
public class MediaActionTypeProvider implements ModuleTypeProvider {

private AudioManager audioManager;
private @NonNullByDefault({}) AudioManager audioManager;

@SuppressWarnings("unchecked")
@Override
public ModuleType getModuleType(String UID, Locale locale) {
public @Nullable ModuleType getModuleType(String UID, @Nullable Locale locale) {
if (PlayActionHandler.TYPE_ID.equals(UID)) {
return getPlayActionType(locale);
} else if (SayActionHandler.TYPE_ID.equals(UID)) {
Expand All @@ -62,36 +65,36 @@ public ModuleType getModuleType(String UID, Locale locale) {
}

@Override
public Collection<ModuleType> getModuleTypes(Locale locale) {
public Collection<ModuleType> getModuleTypes(@Nullable Locale locale) {
return Stream.of(getPlayActionType(locale), getSayActionType(locale)).collect(Collectors.toList());
}

private ModuleType getPlayActionType(Locale locale) {
private ModuleType getPlayActionType(@Nullable Locale locale) {
return new ActionType(PlayActionHandler.TYPE_ID, getConfigPlayDesc(locale), "play a sound",
"Plays a sound file.", null, Visibility.VISIBLE, new ArrayList<>(), new ArrayList<>());
}

private ModuleType getSayActionType(Locale locale) {
private ModuleType getSayActionType(@Nullable Locale locale) {
return new ActionType(SayActionHandler.TYPE_ID, getConfigSayDesc(locale), "say something",
"Speaks a given text through a natural voice.", null, Visibility.VISIBLE, new ArrayList<>(),
new ArrayList<>());
}

private List<ConfigDescriptionParameter> getConfigPlayDesc(Locale locale) {
private List<ConfigDescriptionParameter> getConfigPlayDesc(@Nullable Locale locale) {
ConfigDescriptionParameter param1 = ConfigDescriptionParameterBuilder
.create(PlayActionHandler.PARAM_SOUND, Type.TEXT).withRequired(true).withLabel("Sound")
.withDescription("the sound to play").withOptions(getSoundOptions()).withLimitToOptions(true).build();
return Stream.of(param1, getAudioSinkConfigDescParam(locale)).collect(Collectors.toList());
}

private List<ConfigDescriptionParameter> getConfigSayDesc(Locale locale) {
private List<ConfigDescriptionParameter> getConfigSayDesc(@Nullable Locale locale) {
ConfigDescriptionParameter param1 = ConfigDescriptionParameterBuilder
.create(SayActionHandler.PARAM_TEXT, Type.TEXT).withRequired(true).withLabel("Text")
.withDescription("the text to speak").build();
return Stream.of(param1, getAudioSinkConfigDescParam(locale)).collect(Collectors.toList());
}

private ConfigDescriptionParameter getAudioSinkConfigDescParam(Locale locale) {
private ConfigDescriptionParameter getAudioSinkConfigDescParam(@Nullable Locale locale) {
ConfigDescriptionParameter param2 = ConfigDescriptionParameterBuilder
.create(SayActionHandler.PARAM_SINK, Type.TEXT).withRequired(false).withLabel("Sink")
.withDescription("the audio sink id").withOptions(getSinkOptions(locale)).withLimitToOptions(true)
Expand Down Expand Up @@ -124,7 +127,7 @@ private List<ParameterOption> getSoundOptions() {
*
* @return a list of parameter options representing the audio sinks
*/
private List<ParameterOption> getSinkOptions(Locale locale) {
private List<ParameterOption> getSinkOptions(@Nullable Locale locale) {
List<ParameterOption> options = new ArrayList<>();

for (AudioSink sink : audioManager.getAllSinks()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.common.registry.ProviderChangeListener;
import org.openhab.core.automation.type.ModuleType;
import org.openhab.core.automation.type.ModuleTypeProvider;
Expand All @@ -29,11 +33,12 @@
*
* @author Simon Merschjohann - Initial contribution
*/
@NonNullByDefault
@Component(immediate = true, service = { ScriptedCustomModuleTypeProvider.class, ModuleTypeProvider.class })
public class ScriptedCustomModuleTypeProvider implements ModuleTypeProvider {
private final HashMap<String, ModuleType> modulesTypes = new HashMap<>();
private final Map<String, ModuleType> modulesTypes = new HashMap<>();

private final HashSet<ProviderChangeListener<ModuleType>> listeners = new HashSet<>();
private final Set<ProviderChangeListener<ModuleType>> listeners = new HashSet<>();

@Override
public Collection<ModuleType> getAll() {
Expand All @@ -52,15 +57,13 @@ public void removeProviderChangeListener(ProviderChangeListener<ModuleType> list

@SuppressWarnings("unchecked")
@Override
public <T extends ModuleType> T getModuleType(String UID, Locale locale) {
ModuleType handler = modulesTypes.get(UID);

return (T) handler;
public <T extends ModuleType> T getModuleType(String UID, @Nullable Locale locale) {
return (T) modulesTypes.get(UID);
}

@SuppressWarnings("unchecked")
@Override
public <T extends ModuleType> Collection<T> getModuleTypes(Locale locale) {
public <T extends ModuleType> Collection<T> getModuleTypes(@Nullable Locale locale) {
return (Collection<T>) modulesTypes.values();
}

Expand Down Expand Up @@ -89,7 +92,7 @@ public void updateModuleHandler(String uid) {

if (modType != null) {
for (ProviderChangeListener<ModuleType> listener : listeners) {
listener.updated(this, null, modType);
listener.updated(this, modType, modType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.util.Set;
import java.util.stream.Stream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.common.registry.RegistryChangeListener;
import org.openhab.core.automation.Rule;
import org.openhab.core.automation.RuleRegistry;
Expand All @@ -28,6 +30,7 @@
*
* @author Simon Merschjohann - Initial contribution
*/
@NonNullByDefault
public class RuleSupportRuleRegistryDelegate implements RuleRegistry {
private final RuleRegistry ruleRegistry;

Expand Down Expand Up @@ -56,7 +59,7 @@ public Stream<Rule> stream() {
}

@Override
public Rule get(String key) {
public @Nullable Rule get(String key) {
return ruleRegistry.get(key);
}

Expand All @@ -83,12 +86,12 @@ public void addPermanent(Rule element) {
}

@Override
public Rule update(Rule element) {
public @Nullable Rule update(Rule element) {
return ruleRegistry.update(element);
}

@Override
public Rule remove(String key) {
public @Nullable Rule remove(String key) {
if (rules.remove(key)) {
ruleProvider.removeRule(key);
}
Expand All @@ -97,7 +100,7 @@ public Rule remove(String key) {
}

@Override
public Collection<Rule> getByTag(String tag) {
public Collection<Rule> getByTag(@Nullable String tag) {
return ruleRegistry.getByTag(tag);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.io.rest.LocaleService;
import org.eclipse.smarthome.io.rest.RESTResource;
import org.openhab.core.automation.dto.ActionTypeDTOMapper;
Expand Down Expand Up @@ -62,14 +64,15 @@
*/
@Path("module-types")
@Api("module-types")
@NonNullByDefault
@Component
public class ModuleTypeResource implements RESTResource {

private ModuleTypeRegistry moduleTypeRegistry;
private LocaleService localeService;
private @NonNullByDefault({}) ModuleTypeRegistry moduleTypeRegistry;
private @NonNullByDefault({}) LocaleService localeService;

@Context
private UriInfo uriInfo;
private @NonNullByDefault({}) UriInfo uriInfo;

@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
protected void setModuleTypeRegistry(ModuleTypeRegistry moduleTypeRegistry) {
Expand All @@ -94,11 +97,11 @@ protected void unsetLocaleService(LocaleService localeService) {
@ApiOperation(value = "Get all available module types.", response = ModuleTypeDTO.class, responseContainer = "List")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK", response = ModuleTypeDTO.class, responseContainer = "List") })
public Response getAll(@HeaderParam("Accept-Language") @ApiParam(value = "language") String language,
@QueryParam("tags") @ApiParam(value = "tags for filtering", required = false) String tagList,
@QueryParam("type") @ApiParam(value = "filtering by action, condition or trigger", required = false) String type) {
public Response getAll(@HeaderParam("Accept-Language") @ApiParam(value = "language") @Nullable String language,
@QueryParam("tags") @ApiParam(value = "tags for filtering", required = false) @Nullable String tagList,
@QueryParam("type") @ApiParam(value = "filtering by action, condition or trigger", required = false) @Nullable String type) {
final Locale locale = localeService.getLocale(language);
final String[] tags = tagList != null ? tagList.split(",") : null;
final String[] tags = tagList != null ? tagList.split(",") : new String[0];
final List<ModuleTypeDTO> modules = new ArrayList<>();

if (type == null || type.equals("trigger")) {
Expand All @@ -119,7 +122,7 @@ public Response getAll(@HeaderParam("Accept-Language") @ApiParam(value = "langua
@ApiOperation(value = "Gets a module type corresponding to the given UID.", response = ModuleTypeDTO.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = ModuleTypeDTO.class),
@ApiResponse(code = 404, message = "Module Type corresponding to the given UID does not found.") })
public Response getByUID(@HeaderParam("Accept-Language") @ApiParam(value = "language") String language,
public Response getByUID(@HeaderParam("Accept-Language") @ApiParam(value = "language") @Nullable String language,
@PathParam("moduleTypeUID") @ApiParam(value = "moduleTypeUID", required = true) String moduleTypeUID) {
Locale locale = localeService.getLocale(language);
final ModuleType moduleType = moduleTypeRegistry.get(moduleTypeUID, locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.io.rest.LocaleService;
import org.eclipse.smarthome.io.rest.RESTResource;
import org.openhab.core.automation.dto.RuleTemplateDTO;
Expand All @@ -52,14 +54,15 @@
*/
@Path("templates")
@Api("templates")
@NonNullByDefault
@Component
public class TemplateResource implements RESTResource {

private TemplateRegistry<RuleTemplate> templateRegistry;
private LocaleService localeService;
private @NonNullByDefault({}) TemplateRegistry<@NonNull RuleTemplate> templateRegistry;
private @NonNullByDefault({}) LocaleService localeService;

@Context
private UriInfo uriInfo;
private @NonNullByDefault({}) UriInfo uriInfo;

@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
protected void setTemplateRegistry(TemplateRegistry<RuleTemplate> templateRegistry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.core.automation;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.common.registry.Provider;

/**
Expand All @@ -20,6 +21,7 @@
*
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
public interface RuleProvider extends Provider<Rule> {

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import java.util.Collection;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.common.registry.Registry;

/**
Expand Down Expand Up @@ -50,6 +52,7 @@
*
* @author Yordan Mihaylov - Initial contribution
*/
@NonNullByDefault
public interface RuleRegistry extends Registry<Rule, String> {

/**
Expand All @@ -61,8 +64,8 @@ public interface RuleRegistry extends Registry<Rule, String> {
* @param rule a {@link Rule} instance which have to be added into the {@link RuleRegistry}.
* @return a copy of the added {@link Rule}
* @throws IllegalArgumentException when a rule with the same UID already exists or some of the conditions or
* actions has wrong format of input reference.
* @throws IllegalStateException when the RuleManagedProvider is unavailable.
* actions has wrong format of input reference.
* @throws IllegalStateException when the RuleManagedProvider is unavailable.
*/
@Override
public Rule add(Rule rule);
Expand All @@ -73,7 +76,7 @@ public interface RuleRegistry extends Registry<Rule, String> {
* @param tag specifies a tag that will filter the rules.
* @return collection of {@link Rule}s having specified tag.
*/
public Collection<Rule> getByTag(String tag);
public Collection<Rule> getByTag(@Nullable String tag);

/**
* Gets a collection of {@link Rule}s which has specified tags.
Expand Down
Loading

0 comments on commit 2430256

Please sign in to comment.