Improvements for 'DefaultSystemChannelTypeProvider' (#807)
* Use constructor dependency injection * Added nullness annotations Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>pull/808/head
parent
b5f33d341d
commit
2a9485bc3b
|
@ -24,6 +24,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.eclipse.smarthome.core.thing.i18n.ChannelTypeI18nLocalizationService;
|
import org.eclipse.smarthome.core.thing.i18n.ChannelTypeI18nLocalizationService;
|
||||||
import org.eclipse.smarthome.core.thing.type.ChannelType;
|
import org.eclipse.smarthome.core.thing.type.ChannelType;
|
||||||
|
@ -37,6 +38,7 @@ import org.eclipse.smarthome.core.types.StateDescriptionFragmentBuilder;
|
||||||
import org.eclipse.smarthome.core.types.StateOption;
|
import org.eclipse.smarthome.core.types.StateOption;
|
||||||
import org.eclipse.smarthome.core.util.BundleResolver;
|
import org.eclipse.smarthome.core.util.BundleResolver;
|
||||||
import org.osgi.framework.Bundle;
|
import org.osgi.framework.Bundle;
|
||||||
|
import org.osgi.service.component.annotations.Activate;
|
||||||
import org.osgi.service.component.annotations.Component;
|
import org.osgi.service.component.annotations.Component;
|
||||||
import org.osgi.service.component.annotations.Reference;
|
import org.osgi.service.component.annotations.Reference;
|
||||||
|
|
||||||
|
@ -52,6 +54,7 @@ import org.osgi.service.component.annotations.Reference;
|
||||||
* @author Stefan Triller - Added more system channels
|
* @author Stefan Triller - Added more system channels
|
||||||
* @author Christoph Weitkamp - factored out common i18n aspects into ThingTypeI18nLocalizationService
|
* @author Christoph Weitkamp - factored out common i18n aspects into ThingTypeI18nLocalizationService
|
||||||
*/
|
*/
|
||||||
|
@NonNullByDefault
|
||||||
@Component
|
@Component
|
||||||
public class DefaultSystemChannelTypeProvider implements ChannelTypeProvider {
|
public class DefaultSystemChannelTypeProvider implements ChannelTypeProvider {
|
||||||
|
|
||||||
|
@ -279,16 +282,16 @@ public class DefaultSystemChannelTypeProvider implements ChannelTypeProvider {
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
private static class LocalizedChannelTypeKey {
|
private static class LocalizedChannelTypeKey {
|
||||||
public final String locale;
|
public @Nullable final String locale;
|
||||||
public final UID uid;
|
public final UID uid;
|
||||||
|
|
||||||
public LocalizedChannelTypeKey(UID uid, String locale) {
|
public LocalizedChannelTypeKey(UID uid, @Nullable String locale) {
|
||||||
this.uid = uid;
|
this.uid = uid;
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(@Nullable Object obj) {
|
||||||
if (this == obj) {
|
if (this == obj) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -335,13 +338,21 @@ public class DefaultSystemChannelTypeProvider implements ChannelTypeProvider {
|
||||||
SYSTEM_MEDIA_ARTIST, SYSTEM_WIND_DIRECTION, SYSTEM_WIND_SPEED, SYSTEM_OUTDOOR_TEMPERATURE,
|
SYSTEM_MEDIA_ARTIST, SYSTEM_WIND_DIRECTION, SYSTEM_WIND_SPEED, SYSTEM_OUTDOOR_TEMPERATURE,
|
||||||
SYSTEM_ATMOSPHERIC_HUMIDITY, SYSTEM_BAROMETRIC_PRESSURE).collect(Collectors.toList()));
|
SYSTEM_ATMOSPHERIC_HUMIDITY, SYSTEM_BAROMETRIC_PRESSURE).collect(Collectors.toList()));
|
||||||
|
|
||||||
private final Map<LocalizedChannelTypeKey, ChannelType> localizedChannelTypeCache = new ConcurrentHashMap<>();
|
private final Map<LocalizedChannelTypeKey, @Nullable ChannelType> localizedChannelTypeCache = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
private ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService;
|
private final ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService;
|
||||||
private BundleResolver bundleResolver;
|
private final BundleResolver bundleResolver;
|
||||||
|
|
||||||
|
@Activate
|
||||||
|
public DefaultSystemChannelTypeProvider(
|
||||||
|
final @Reference ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService,
|
||||||
|
final @Reference BundleResolver bundleResolver) {
|
||||||
|
this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
|
||||||
|
this.bundleResolver = bundleResolver;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<ChannelType> getChannelTypes(Locale locale) {
|
public @Nullable Collection<ChannelType> getChannelTypes(@Nullable Locale locale) {
|
||||||
final List<ChannelType> allChannelTypes = new ArrayList<>();
|
final List<ChannelType> allChannelTypes = new ArrayList<>();
|
||||||
final Bundle bundle = bundleResolver.resolveBundle(DefaultSystemChannelTypeProvider.class);
|
final Bundle bundle = bundleResolver.resolveBundle(DefaultSystemChannelTypeProvider.class);
|
||||||
|
|
||||||
|
@ -352,7 +363,7 @@ public class DefaultSystemChannelTypeProvider implements ChannelTypeProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ChannelType getChannelType(ChannelTypeUID channelTypeUID, Locale locale) {
|
public @Nullable ChannelType getChannelType(ChannelTypeUID channelTypeUID, @Nullable Locale locale) {
|
||||||
final Bundle bundle = bundleResolver.resolveBundle(DefaultSystemChannelTypeProvider.class);
|
final Bundle bundle = bundleResolver.resolveBundle(DefaultSystemChannelTypeProvider.class);
|
||||||
|
|
||||||
for (final ChannelType channelType : CHANNEL_TYPES) {
|
for (final ChannelType channelType : CHANNEL_TYPES) {
|
||||||
|
@ -363,27 +374,7 @@ public class DefaultSystemChannelTypeProvider implements ChannelTypeProvider {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Reference
|
private ChannelType createLocalizedChannelType(Bundle bundle, ChannelType channelType, @Nullable Locale locale) {
|
||||||
public void setChannelTypeI18nLocalizationService(
|
|
||||||
final ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
|
|
||||||
this.channelTypeI18nLocalizationService = channelTypeI18nLocalizationService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void unsetChannelTypeI18nLocalizationService(
|
|
||||||
final ChannelTypeI18nLocalizationService channelTypeI18nLocalizationService) {
|
|
||||||
this.channelTypeI18nLocalizationService = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Reference
|
|
||||||
public void setBundleResolver(BundleResolver bundleResolver) {
|
|
||||||
this.bundleResolver = bundleResolver;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void unsetBundleResolver(BundleResolver bundleResolver) {
|
|
||||||
this.bundleResolver = bundleResolver;
|
|
||||||
}
|
|
||||||
|
|
||||||
private ChannelType createLocalizedChannelType(Bundle bundle, ChannelType channelType, Locale locale) {
|
|
||||||
LocalizedChannelTypeKey localizedChannelTypeKey = getLocalizedChannelTypeKey(channelType.getUID(), locale);
|
LocalizedChannelTypeKey localizedChannelTypeKey = getLocalizedChannelTypeKey(channelType.getUID(), locale);
|
||||||
|
|
||||||
ChannelType cachedEntry = localizedChannelTypeCache.get(localizedChannelTypeKey);
|
ChannelType cachedEntry = localizedChannelTypeCache.get(localizedChannelTypeKey);
|
||||||
|
@ -400,14 +391,14 @@ public class DefaultSystemChannelTypeProvider implements ChannelTypeProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private @Nullable ChannelType localize(Bundle bundle, ChannelType channelType, Locale locale) {
|
private @Nullable ChannelType localize(Bundle bundle, ChannelType channelType, @Nullable Locale locale) {
|
||||||
if (channelTypeI18nLocalizationService == null) {
|
if (channelTypeI18nLocalizationService == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return channelTypeI18nLocalizationService.createLocalizedChannelType(bundle, channelType, locale);
|
return channelTypeI18nLocalizationService.createLocalizedChannelType(bundle, channelType, locale);
|
||||||
}
|
}
|
||||||
|
|
||||||
private LocalizedChannelTypeKey getLocalizedChannelTypeKey(UID uid, Locale locale) {
|
private LocalizedChannelTypeKey getLocalizedChannelTypeKey(UID uid, @Nullable Locale locale) {
|
||||||
return new LocalizedChannelTypeKey(uid, locale != null ? locale.toLanguageTag() : null);
|
return new LocalizedChannelTypeKey(uid, locale != null ? locale.toLanguageTag() : null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue