mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
[itemregistry] Use constructor injection to simplify lifecycle (#988)
* Use constructor injection to simplify lifecycle Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de> * PR extended by ItemStateConverter Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
committed by
Kai Kreuzer
parent
c8395ef5be
commit
be1cd25743
+7
-14
@@ -41,6 +41,7 @@ import org.eclipse.smarthome.core.service.StateDescriptionService;
|
||||
import org.osgi.service.component.ComponentContext;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Deactivate;
|
||||
import org.osgi.service.component.annotations.Reference;
|
||||
import org.osgi.service.component.annotations.ReferenceCardinality;
|
||||
import org.osgi.service.component.annotations.ReferencePolicy;
|
||||
@@ -53,9 +54,8 @@ import org.slf4j.LoggerFactory;
|
||||
* current state in memory. This is the central point where states are kept and
|
||||
* thus it is a core part for all stateful services.
|
||||
*
|
||||
* @author Kai Kreuzer - Initial contribution and API
|
||||
* @author Kai Kreuzer - Initial contribution
|
||||
* @author Stefan Bußweiler - Migration to new event mechanism
|
||||
*
|
||||
*/
|
||||
@Component(immediate = true)
|
||||
public class ItemRegistryImpl extends AbstractRegistry<Item, String, ItemProvider> implements ItemRegistry {
|
||||
@@ -65,13 +65,14 @@ public class ItemRegistryImpl extends AbstractRegistry<Item, String, ItemProvide
|
||||
private final List<RegistryHook<Item>> registryHooks = new CopyOnWriteArrayList<>();
|
||||
private StateDescriptionService stateDescriptionService;
|
||||
private CommandDescriptionService commandDescriptionService;
|
||||
private MetadataRegistry metadataRegistry;
|
||||
|
||||
private final MetadataRegistry metadataRegistry;
|
||||
private UnitProvider unitProvider;
|
||||
private ItemStateConverter itemStateConverter;
|
||||
|
||||
public ItemRegistryImpl() {
|
||||
@Activate
|
||||
public ItemRegistryImpl(final @Reference MetadataRegistry metadataRegistry) {
|
||||
super(ItemProvider.class);
|
||||
this.metadataRegistry = metadataRegistry;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -433,6 +434,7 @@ public class ItemRegistryImpl extends AbstractRegistry<Item, String, ItemProvide
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deactivate
|
||||
protected void deactivate() {
|
||||
super.deactivate();
|
||||
}
|
||||
@@ -480,13 +482,4 @@ public class ItemRegistryImpl extends AbstractRegistry<Item, String, ItemProvide
|
||||
super.unsetManagedProvider(provider);
|
||||
}
|
||||
|
||||
@Reference
|
||||
protected void setMetadataRegistry(MetadataRegistry metadataRegistry) {
|
||||
this.metadataRegistry = metadataRegistry;
|
||||
}
|
||||
|
||||
protected void unsetMetadataRegistry(MetadataRegistry metadataRegistry) {
|
||||
this.metadataRegistry = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+13
-16
@@ -15,7 +15,7 @@ package org.eclipse.smarthome.core.internal.items;
|
||||
import javax.measure.Quantity;
|
||||
import javax.measure.Unit;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.eclipse.smarthome.core.i18n.UnitProvider;
|
||||
import org.eclipse.smarthome.core.items.Item;
|
||||
@@ -27,6 +27,7 @@ import org.eclipse.smarthome.core.types.State;
|
||||
import org.eclipse.smarthome.core.types.StateDescription;
|
||||
import org.eclipse.smarthome.core.types.UnDefType;
|
||||
import org.eclipse.smarthome.core.types.util.UnitUtils;
|
||||
import org.osgi.service.component.annotations.Activate;
|
||||
import org.osgi.service.component.annotations.Component;
|
||||
import org.osgi.service.component.annotations.Reference;
|
||||
import org.slf4j.Logger;
|
||||
@@ -35,18 +36,23 @@ import org.slf4j.LoggerFactory;
|
||||
/**
|
||||
* Convert a {@link State} to an {@link Item} accepted {@link State}.
|
||||
*
|
||||
* @author Henning Treu - Initial contribution and API
|
||||
*
|
||||
* @author Henning Treu - Initial contribution
|
||||
*/
|
||||
@Component
|
||||
@NonNullByDefault
|
||||
public class ItemStateConverterImpl implements ItemStateConverter {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(ItemStateConverterImpl.class);
|
||||
|
||||
private UnitProvider unitProvider;
|
||||
private final UnitProvider unitProvider;
|
||||
|
||||
@Activate
|
||||
public ItemStateConverterImpl(final @Reference UnitProvider unitProvider) {
|
||||
this.unitProvider = unitProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NonNull State convertToAcceptedState(@Nullable State state, @Nullable Item item) {
|
||||
public State convertToAcceptedState(@Nullable State state, @Nullable Item item) {
|
||||
if (state == null) {
|
||||
logger.error("A conversion of null was requested:",
|
||||
new IllegalArgumentException("State must not be null."));
|
||||
@@ -103,7 +109,7 @@ public class ItemStateConverterImpl implements ItemStateConverter {
|
||||
return state;
|
||||
}
|
||||
|
||||
private @NonNull State convertOrUndef(QuantityType<?> quantityState, Unit<?> targetUnit) {
|
||||
private State convertOrUndef(QuantityType<?> quantityState, Unit<?> targetUnit) {
|
||||
QuantityType<?> converted = quantityState.toUnit(targetUnit);
|
||||
if (converted != null) {
|
||||
return converted;
|
||||
@@ -111,7 +117,7 @@ public class ItemStateConverterImpl implements ItemStateConverter {
|
||||
return UnDefType.UNDEF;
|
||||
}
|
||||
|
||||
private Unit<?> parseItemUnit(NumberItem numberItem) {
|
||||
private @Nullable Unit<?> parseItemUnit(NumberItem numberItem) {
|
||||
StateDescription stateDescription = numberItem.getStateDescription();
|
||||
if (stateDescription == null) {
|
||||
return null;
|
||||
@@ -128,13 +134,4 @@ public class ItemStateConverterImpl implements ItemStateConverter {
|
||||
return item.getAcceptedDataTypes().contains(state.getClass());
|
||||
}
|
||||
|
||||
@Reference
|
||||
public void setUnitProvider(UnitProvider unitProvider) {
|
||||
this.unitProvider = unitProvider;
|
||||
}
|
||||
|
||||
protected void unsetUnitProvider(UnitProvider unitProvider) {
|
||||
this.unitProvider = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-4
@@ -12,16 +12,16 @@
|
||||
*/
|
||||
package org.eclipse.smarthome.core.items;
|
||||
|
||||
import org.eclipse.jdt.annotation.NonNull;
|
||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.eclipse.jdt.annotation.Nullable;
|
||||
import org.eclipse.smarthome.core.types.State;
|
||||
|
||||
/**
|
||||
* Convert a {@link State} to an {@link Item} accepted {@link State}.
|
||||
*
|
||||
* @author Henning Treu - Initial contribution and API
|
||||
*
|
||||
* @author Henning Treu - Initial contribution
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public interface ItemStateConverter {
|
||||
|
||||
/**
|
||||
@@ -32,6 +32,5 @@ public interface ItemStateConverter {
|
||||
* @return the converted {@link State} according to an accepted State of the given Item. Will return the original
|
||||
* state in case item was {@code null}.
|
||||
*/
|
||||
@NonNull
|
||||
State convertToAcceptedState(@Nullable State state, @Nullable Item item);
|
||||
}
|
||||
|
||||
+3
-10
@@ -26,7 +26,6 @@ import org.eclipse.smarthome.core.library.types.DecimalType;
|
||||
import org.eclipse.smarthome.core.library.types.PercentType;
|
||||
import org.eclipse.smarthome.core.library.types.QuantityType;
|
||||
import org.eclipse.smarthome.core.library.unit.ImperialUnits;
|
||||
import org.eclipse.smarthome.core.library.unit.SIUnits;
|
||||
import org.eclipse.smarthome.core.types.State;
|
||||
import org.eclipse.smarthome.core.types.StateDescription;
|
||||
import org.eclipse.smarthome.core.types.UnDefType;
|
||||
@@ -45,7 +44,9 @@ public class ItemStateConverterImplTest {
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
itemStateConverter = new ItemStateConverterImpl();
|
||||
UnitProvider unitProvider = mock(UnitProvider.class);
|
||||
when(unitProvider.getUnit(Temperature.class)).thenReturn(ImperialUnits.FAHRENHEIT);
|
||||
itemStateConverter = new ItemStateConverterImpl(unitProvider);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -101,10 +102,6 @@ public class ItemStateConverterImplTest {
|
||||
NumberItem item = mock(NumberItem.class);
|
||||
doReturn(Temperature.class).when(item).getDimension();
|
||||
|
||||
UnitProvider unitProvider = mock(UnitProvider.class);
|
||||
when(unitProvider.getUnit(Temperature.class)).thenReturn(ImperialUnits.FAHRENHEIT);
|
||||
itemStateConverter.setUnitProvider(unitProvider);
|
||||
|
||||
State originalState = new QuantityType<>("12.34 °C");
|
||||
State convertedState = itemStateConverter.convertToAcceptedState(originalState, item);
|
||||
|
||||
@@ -116,10 +113,6 @@ public class ItemStateConverterImplTest {
|
||||
NumberItem item = mock(NumberItem.class);
|
||||
doReturn(Length.class).when(item).getDimension();
|
||||
|
||||
UnitProvider unitProvider = mock(UnitProvider.class);
|
||||
when(unitProvider.getUnit(Length.class)).thenReturn(SIUnits.METRE);
|
||||
itemStateConverter.setUnitProvider(unitProvider);
|
||||
|
||||
QuantityType<Length> originalState = new QuantityType<>("100 cm");
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
||||
+1
-2
@@ -116,8 +116,7 @@ public class GroupItemTest extends JavaOSGiTest {
|
||||
when(unitProvider.getUnit(Temperature.class)).thenReturn(Units.CELSIUS);
|
||||
|
||||
groupFunctionHelper = new GroupFunctionHelper();
|
||||
itemStateConverter = new ItemStateConverterImpl();
|
||||
((ItemStateConverterImpl) itemStateConverter).setUnitProvider(unitProvider);
|
||||
itemStateConverter = new ItemStateConverterImpl(unitProvider);
|
||||
}
|
||||
|
||||
@Ignore
|
||||
|
||||
+1
-2
@@ -103,10 +103,9 @@ public class ItemRegistryImplTest extends JavaTest {
|
||||
itemProvider.add(cameraItem4);
|
||||
|
||||
// setup ItemRegistryImpl with necessary dependencies:
|
||||
itemRegistry = new ItemRegistryImpl() {
|
||||
itemRegistry = new ItemRegistryImpl(mock(MetadataRegistry.class)) {
|
||||
{
|
||||
addProvider(itemProvider);
|
||||
|
||||
setManagedProvider(itemProvider);
|
||||
setEventPublisher(ItemRegistryImplTest.this.eventPublisher);
|
||||
setStateDescriptionService(mock(StateDescriptionService.class));
|
||||
|
||||
Reference in New Issue
Block a user