Simplify lifecycle by using constructor injection (#1278)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2019-12-18 23:02:56 +01:00 committed by Wouter Born
parent 853179e8f2
commit f32d779894
7 changed files with 29 additions and 37 deletions

View File

@ -134,7 +134,6 @@ public class ItemResource implements RESTResource {
private @NonNullByDefault({}) DTOMapper dtoMapper;
private @NonNullByDefault({}) MetadataSelectorMatcher metadataSelectorMatcher;
private @NonNullByDefault({}) ItemBuilderFactory itemBuilderFactory;
private @NonNullByDefault({}) LocaleService localeService;
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)

View File

@ -23,10 +23,13 @@ import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.core.ConfigDescription;
import org.eclipse.smarthome.config.core.ConfigDescriptionRegistry;
import org.eclipse.smarthome.core.common.AbstractUID;
import org.eclipse.smarthome.core.items.MetadataRegistry;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
@ -36,14 +39,21 @@ import org.osgi.service.component.annotations.Reference;
* @author Henning Treu - Initial contribution
*/
@Component(service = MetadataSelectorMatcher.class)
@NonNullByDefault
public class MetadataSelectorMatcher {
private static final String METADATA_SCHEME = "metadata";
private static final String METADATA_SCHEME_PREFIX = METADATA_SCHEME + ":";
private MetadataRegistry metadataRegistry;
private final MetadataRegistry metadataRegistry;
private final ConfigDescriptionRegistry configDescriptionRegistry;
private ConfigDescriptionRegistry configDescriptionRegistry;
@Activate
public MetadataSelectorMatcher(final @Reference MetadataRegistry metadataRegistry,
final @Reference ConfigDescriptionRegistry configDescriptionRegistry) {
this.metadataRegistry = metadataRegistry;
this.configDescriptionRegistry = configDescriptionRegistry;
}
/**
* Filter existing metadata namespaces against the given namespaeSelector. The given String might consist of a comma
@ -53,7 +63,7 @@ public class MetadataSelectorMatcher {
* @param locale the locale for config descriptions with the scheme "metadata".
* @return a {@link Set} of matching namespaces.
*/
public Set<String> filterNamespaces(String namespaceSelector, Locale locale) {
public Set<String> filterNamespaces(@Nullable String namespaceSelector, @Nullable Locale locale) {
if (namespaceSelector == null || namespaceSelector.isEmpty()) {
return Collections.emptySet();
} else {
@ -81,21 +91,4 @@ public class MetadataSelectorMatcher {
}
}
@Reference
protected void setMetadataRegistry(MetadataRegistry metadataRegistry) {
this.metadataRegistry = metadataRegistry;
}
protected void unsetMetadataRegistry(MetadataRegistry metadataRegistry) {
this.metadataRegistry = null;
}
@Reference
protected void setConfigDescriptionRegistry(ConfigDescriptionRegistry configDescriptionRegistry) {
this.configDescriptionRegistry = configDescriptionRegistry;
}
protected void unsetConfigDescriptionRegistry(ConfigDescriptionRegistry configDescriptionRegistry) {
this.configDescriptionRegistry = null;
}
}

View File

@ -27,6 +27,7 @@ import java.util.Collections;
import java.util.List;
import org.eclipse.smarthome.config.core.ConfigDescription;
import org.eclipse.smarthome.config.core.ConfigDescriptionBuilder;
import org.eclipse.smarthome.config.core.ConfigDescriptionRegistry;
import org.eclipse.smarthome.core.items.MetadataRegistry;
import org.junit.Before;
@ -55,19 +56,16 @@ public class MetadataSelectorMatcherTest {
when(configDescriptionRegistry.getConfigDescriptions(null)).thenReturn(mockConfigDescriptions());
when(metadataRegistry.isInternalNamespace(anyString())).thenReturn(false);
matcher = new MetadataSelectorMatcher();
matcher.setConfigDescriptionRegistry(configDescriptionRegistry);
matcher.setMetadataRegistry(metadataRegistry);
matcher = new MetadataSelectorMatcher(metadataRegistry, configDescriptionRegistry);
}
private Collection<ConfigDescription> mockConfigDescriptions() throws Exception {
List<ConfigDescription> configDescriptions = new ArrayList<>();
configDescriptions.add(new ConfigDescription(new URI("metadata:magic")));
configDescriptions.add(new ConfigDescription(new URI("metadata:magic2")));
configDescriptions.add(new ConfigDescription(new URI("metadata:homekit")));
configDescriptions.add(new ConfigDescription(new URI("metadata:alexa")));
configDescriptions.add(ConfigDescriptionBuilder.create(new URI("metadata:magic")).build());
configDescriptions.add(ConfigDescriptionBuilder.create(new URI("metadata:magic2")).build());
configDescriptions.add(ConfigDescriptionBuilder.create(new URI("metadata:homekit")).build());
configDescriptions.add(ConfigDescriptionBuilder.create(new URI("metadata:alexa")).build());
return configDescriptions;
}

View File

@ -40,8 +40,8 @@ import org.osgi.service.component.annotations.Component;
*
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
@Component(service = { MetadataProvider.class, GenericMetadataProvider.class })
@NonNullByDefault
public class GenericMetadataProvider extends AbstractProvider<Metadata> implements MetadataProvider {
private final Set<Metadata> metadata = new HashSet<>();

View File

@ -12,7 +12,7 @@
*/
package org.eclipse.smarthome.core.internal.items;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.common.registry.AbstractManagedProvider;
import org.eclipse.smarthome.core.items.ManagedMetadataProvider;
import org.eclipse.smarthome.core.items.Metadata;
@ -34,6 +34,7 @@ import org.slf4j.LoggerFactory;
* @author Kai Kreuzer - Initial contribution
*/
@Component(immediate = true, service = { MetadataProvider.class, ManagedMetadataProvider.class })
@NonNullByDefault
public class ManagedMetadataProviderImpl extends AbstractManagedProvider<Metadata, MetadataKey, Metadata>
implements ManagedMetadataProvider {
@ -50,12 +51,12 @@ public class ManagedMetadataProviderImpl extends AbstractManagedProvider<Metadat
}
@Override
protected @NonNull String keyToString(@NonNull MetadataKey key) {
protected String keyToString(MetadataKey key) {
return key.toString();
}
@Override
protected Metadata toElement(@NonNull String key, @NonNull Metadata persistableElement) {
protected Metadata toElement(String key, Metadata persistableElement) {
return persistableElement;
}
@ -70,7 +71,7 @@ public class ManagedMetadataProviderImpl extends AbstractManagedProvider<Metadat
* @param itemname the name of the item for which the metadata is to be removed.
*/
@Override
public void removeItemMetadata(@NonNull String name) {
public void removeItemMetadata(String name) {
logger.debug("Removing all metadata for item {}", name);
getAll().stream().filter(MetadataPredicates.ofItem(name)).map(Metadata::getUID).forEach(this::remove);
}

View File

@ -34,8 +34,8 @@ import org.osgi.service.component.annotations.ReferencePolicy;
*
* @author Kai Kreuzer - Initial contribution
*/
@Component(immediate = true, service = MetadataRegistry.class)
@NonNullByDefault
@Component(immediate = true, service = { MetadataRegistry.class })
public class MetadataRegistryImpl extends AbstractRegistry<Metadata, MetadataKey, MetadataProvider>
implements MetadataRegistry {

View File

@ -12,7 +12,7 @@
*/
package org.eclipse.smarthome.core.items;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.common.registry.ManagedProvider;
import org.eclipse.smarthome.core.storage.StorageService;
@ -23,6 +23,7 @@ import org.eclipse.smarthome.core.storage.StorageService;
*
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
public interface ManagedMetadataProvider extends ManagedProvider<Metadata, MetadataKey>, MetadataProvider {
/**
@ -30,6 +31,6 @@ public interface ManagedMetadataProvider extends ManagedProvider<Metadata, Metad
*
* @param itemname the name of the item for which the metadata is to be removed.
*/
void removeItemMetadata(@NonNull String name);
void removeItemMetadata(String name);
}