Skip to content

Commit

Permalink
Removed deprecated constructor for 'DiscoveryResultImpl' (openhab#1428)
Browse files Browse the repository at this point in the history
* Removed deprecated contructor DiscoveryResultImpl
* Added SuppressWarnings('deprecation')
* Removed set default values
* Added nullness annotations

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp committed Apr 20, 2020
1 parent 6b4e54a commit a247a02
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.config.discovery.internal.DiscoveryResultImpl;
import org.openhab.core.i18n.I18nUtil;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
Expand Down Expand Up @@ -268,9 +267,11 @@ protected void thingDiscovered(final DiscoveryResult discoveryResult) {

String label = this.i18nProvider.getText(bundle, key, defaultLabel, this.localeProvider.getLocale());

discoveryResultNew = new DiscoveryResultImpl(discoveryResult.getThingTypeUID(),
discoveryResult.getThingUID(), discoveryResult.getBridgeUID(), discoveryResult.getProperties(),
discoveryResult.getRepresentationProperty(), label, discoveryResult.getTimeToLive());
discoveryResultNew = DiscoveryResultBuilder.create(discoveryResult.getThingUID())
.withThingType(discoveryResult.getThingTypeUID()).withBridge(discoveryResult.getBridgeUID())
.withProperties(discoveryResult.getProperties())
.withRepresentationProperty(discoveryResult.getRepresentationProperty()).withLabel(label)
.withTTL(discoveryResult.getTimeToLive()).build();
} else {
discoveryResultNew = discoveryResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;
Expand Down Expand Up @@ -48,7 +49,7 @@ public interface DiscoveryResult {
* discovered again, the same {@code Thing} ID must be created. A typical {@code Thing} ID could be the serial
* number. It's usually <i>not</i> a product number.
*
* @return the Thing ID of this result object (not null, not empty)
* @return the Thing ID
*/
public ThingUID getThingUID();

Expand All @@ -58,7 +59,7 @@ public interface DiscoveryResult {
* A {@code Thing} type ID could be a product number which identifies the same type of {@link Thing}s. It's usually
* <i>not</i> a serial number.
*
* @return the unique Thing type of this result object (not null, not empty)
* @return the unique Thing type
*/
public ThingTypeUID getThingTypeUID();

Expand All @@ -67,7 +68,7 @@ public interface DiscoveryResult {
* <p>
* The binding ID is extracted from the unique {@code Thing} ID.
*
* @return the binding ID of this result object (not null, not empty)
* @return the binding ID
*/
public String getBindingId();

Expand All @@ -77,7 +78,7 @@ public interface DiscoveryResult {
* <p>
* <b>Hint:</b> The returned properties are immutable.
*
* @return the properties of this result object (not null, could be empty)
* @return the properties (could be empty)
*/
public Map<String, Object> getProperties();

Expand All @@ -89,7 +90,7 @@ public interface DiscoveryResult {
* identifiers are typically the <code>ipAddress</code>, the <code>macAddress</code> or the
* <code>serialNumber</code> of the discovered thing.
*
* @return the representation property of this result object (could be null)
* @return the representation property
*/
public @Nullable String getRepresentationProperty();

Expand All @@ -100,33 +101,33 @@ public interface DiscoveryResult {
* case the result object should be regarded as known by the system so that
* further processing should be skipped.
*
* @return the flag of this result object (not null)
* @return the flag
*/
public DiscoveryResultFlag getFlag();

/**
* Returns the human readable label for this result object.
*
* @return the human readable label for this result object (not null, could be empty)
* @return the human readable label (could be empty)
*/
public String getLabel();

/**
* Returns the unique bridge ID of the {@link DiscoveryResult}.
* Returns the unique {@link Bridge} ID of this result object.
*
* @return the unique bridge ID (could be null)
* @return the unique Bridge ID
*/
public @Nullable ThingUID getBridgeUID();

/**
* Get the timestamp of this {@link DiscoveryResult}.
* Returns the timestamp of this result object.
*
* @return timestamp as long
*/
public long getTimestamp();

/**
* Get the time to live in seconds for this entry.
* Returns the time to live in seconds for this entry.
*
* @return time to live in seconds
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public DiscoveryResultBuilder withTTL(long ttl) {
*
* @return the desired result
*/
@SuppressWarnings("deprecation")
public DiscoveryResult build() {
return new DiscoveryResultImpl(thingTypeUID, thingUID, bridgeUID, properties, representationProperty, label,
ttl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,32 @@

import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.DiscoveryResultFlag;
import org.openhab.core.thing.Bridge;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.ThingUID;

/**
*
* @author Kai Kreuzer - Initial contribution
*/
@NonNullByDefault
public class DiscoveryResultImpl implements DiscoveryResult {

private ThingUID bridgeUID;
private ThingUID thingUID;
private ThingTypeUID thingTypeUID;
private @Nullable ThingUID bridgeUID;
private @NonNullByDefault({}) ThingUID thingUID;
private @Nullable ThingTypeUID thingTypeUID;

private Map<String, Object> properties;
private String representationProperty;
private DiscoveryResultFlag flag;
private String label;
private Map<String, Object> properties = Collections.emptyMap();
private @Nullable String representationProperty;
private @NonNullByDefault({}) DiscoveryResultFlag flag;
private @NonNullByDefault({}) String label;
private long timestamp;
private long timeToLive = TTL_UNLIMITED;

Expand All @@ -45,50 +49,26 @@ public class DiscoveryResultImpl implements DiscoveryResult {
DiscoveryResultImpl() {
}

/**
* Creates a new instance of this class with the specified parameters.
*
* @param thingUID
* the Thing UID to be set (must not be null). If a {@code Thing} disappears and is discovered again, the
* same {@code Thing} ID
* must be created. A typical {@code Thing} ID could be the
* serial number. It's usually <i>not</i> a product name.
* @param properties the properties to be set (could be null or empty)
* @param representationProperty the representationProperty to be set (could be null or empty)
* @param label the human readable label to set (could be null or empty)
* @param bridgeUID the unique bridge ID to be set
* @param timeToLive time to live in seconds
*
* @throws IllegalArgumentException
* if the Thing type UID or the Thing UID is null
* @deprecated use {@link #DiscoveryResultImpl(ThingUID, ThingTypeUID, ThingUID, Map, String, String, long)}
* instead.
*/
@Deprecated
public DiscoveryResultImpl(ThingUID thingUID, ThingUID bridgeUID, Map<String, Object> properties,
String representationProperty, String label, long timeToLive) throws IllegalArgumentException {
this(thingUID.getThingTypeUID(), thingUID, bridgeUID, properties, representationProperty, label, timeToLive);
}

/**
* Creates a new instance of this class with the specified parameters.
*
* @param thingTypeUID the {@link ThingTypeUID}
* @param thingUID the Thing UID to be set (must not be null). If a {@code Thing} disappears and is discovered
* again, the same {@code Thing} ID must be created. A typical {@code Thing} ID could be the serial
* number. It's usually <i>not</i> a product name.
* @param properties the properties to be set (could be null or empty)
* @param representationProperty the representationProperty to be set (could be null or empty)
* @param label the human readable label to set (could be null or empty)
* @param bridgeUID the unique bridge ID to be set
* @param thingUID the {@link ThingUID} to be set. If a {@code Thing} disappears and is discovered again, the same
* {@code Thing} ID must be created. A typical {@code Thing} ID could be the serial number. It's usually
* <i>not</i> a product name.
* @param bridgeUID the unique {@link Bridge} ID to be set
* @param properties the properties to be set
* @param representationProperty the representationProperty to be set
* @param label the human readable label to set
* @param timeToLive time to live in seconds
*
* @throws IllegalArgumentException
* if the Thing type UID or the Thing UID is null
* @throws IllegalArgumentException if the {@link ThingUID} is null or the time to live is less than 1
* @deprecated use {@link DiscoveryResultBuilder} instead.
*/
public DiscoveryResultImpl(ThingTypeUID thingTypeUID, ThingUID thingUID, ThingUID bridgeUID,
Map<String, Object> properties, String representationProperty, String label, long timeToLive)
throws IllegalArgumentException {
@Deprecated
public DiscoveryResultImpl(@Nullable ThingTypeUID thingTypeUID, ThingUID thingUID, @Nullable ThingUID bridgeUID,
@Nullable Map<String, Object> properties, @Nullable String representationProperty, @Nullable String label,
long timeToLive) throws IllegalArgumentException {
if (thingUID == null) {
throw new IllegalArgumentException("The thing UID must not be null!");
}
Expand All @@ -99,8 +79,7 @@ public DiscoveryResultImpl(ThingTypeUID thingTypeUID, ThingUID thingUID, ThingUI
this.thingUID = thingUID;
this.thingTypeUID = thingTypeUID;
this.bridgeUID = bridgeUID;
this.properties = Collections
.unmodifiableMap((properties != null) ? new HashMap<>(properties) : new HashMap<>());
this.properties = properties == null ? Collections.emptyMap() : Collections.unmodifiableMap(properties);
this.representationProperty = representationProperty;
this.label = label == null ? "" : label;

Expand All @@ -117,45 +96,42 @@ public ThingUID getThingUID() {

@Override
public ThingTypeUID getThingTypeUID() {
if (this.thingTypeUID != null) {
return this.thingTypeUID;
ThingTypeUID localThingTypeUID = thingTypeUID;
if (localThingTypeUID != null) {
return localThingTypeUID;
} else {
// fallback for discovery result which were created before the thingTypeUID field was added
return this.thingUID.getThingTypeUID();
return thingUID.getThingTypeUID();
}
}

@Override
public String getBindingId() {
ThingUID thingId = this.thingUID;
if (thingId != null) {
return thingId.getBindingId();
}
return "";
return thingUID.getBindingId();
}

@Override
public Map<String, Object> getProperties() {
return this.properties;
return properties;
}

@Override
public String getRepresentationProperty() {
return this.representationProperty;
public @Nullable String getRepresentationProperty() {
return representationProperty;
}

@Override
public DiscoveryResultFlag getFlag() {
return this.flag;
return flag;
}

@Override
public String getLabel() {
return this.label;
return label;
}

@Override
public ThingUID getBridgeUID() {
public @Nullable ThingUID getBridgeUID() {
return bridgeUID;
}

Expand All @@ -169,8 +145,8 @@ public ThingUID getBridgeUID() {
*
* @param sourceResult the discovery result which is used as source for the merge
*/
public void synchronize(DiscoveryResult sourceResult) {
if ((sourceResult != null) && (sourceResult.getThingUID().equals(this.thingUID))) {
public void synchronize(@Nullable DiscoveryResult sourceResult) {
if (sourceResult != null && thingUID.equals(sourceResult.getThingUID())) {
this.properties = sourceResult.getProperties();
this.representationProperty = sourceResult.getRepresentationProperty();
this.label = sourceResult.getLabel();
Expand All @@ -188,29 +164,23 @@ public void synchronize(DiscoveryResult sourceResult) {
* <p>
* If the specified flag is {@code null}, {@link DiscoveryResultFlag.NEW} is set by default.
*
* @param flag the flag of this result object to be set (could be null)
* @param flag the flag of this result object to be set
*/
public void setFlag(DiscoveryResultFlag flag) {
this.flag = (flag == null) ? DiscoveryResultFlag.NEW : flag;
public void setFlag(@Nullable DiscoveryResultFlag flag) {
this.flag = flag == null ? DiscoveryResultFlag.NEW : flag;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((thingUID == null) ? 0 : thingUID.hashCode());
return result;
return 31 + thingUID.hashCode();
}

@Override
public boolean equals(Object obj) {
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
DiscoveryResultImpl other = (DiscoveryResultImpl) obj;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.junit.Before;
import org.junit.Test;
import org.openhab.core.config.discovery.DiscoveryResult;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
import org.openhab.core.config.discovery.DiscoveryResultFlag;
import org.openhab.core.config.discovery.internal.DiscoveryResultImpl;
import org.openhab.core.thing.ThingTypeUID;
Expand Down Expand Up @@ -65,19 +66,19 @@ public class InboxPredicatesTest {
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
private static final Map<String, Object> PROPS2 = Collections.singletonMap(PROP2, PROP_VAL2);

private static final List<DiscoveryResultImpl> RESULTS = Arrays.asList(
new DiscoveryResultImpl(THING_TYPE_UID11, THING_UID11, null, PROPS1, PROP1, "label",
DiscoveryResult.TTL_UNLIMITED),
new DiscoveryResultImpl(THING_TYPE_UID11, THING_UID12, null, PROPS1, null, "label",
DiscoveryResult.TTL_UNLIMITED),
new DiscoveryResultImpl(THING_TYPE_UID12, THING_UID12, null, PROPS2, PROP2, "label",
DiscoveryResult.TTL_UNLIMITED),
new DiscoveryResultImpl(THING_TYPE_UID21, THING_UID22, null, PROPS2, null, "label",
DiscoveryResult.TTL_UNLIMITED));
private static final List<DiscoveryResult> RESULTS = Arrays.asList(
DiscoveryResultBuilder.create(THING_UID11).withThingType(THING_TYPE_UID11).withProperties(PROPS1)
.withRepresentationProperty(PROP1).withLabel("label").build(),
DiscoveryResultBuilder.create(THING_UID12).withThingType(THING_TYPE_UID11).withProperties(PROPS1)
.withLabel("label").build(),
DiscoveryResultBuilder.create(THING_UID12).withThingType(THING_TYPE_UID12).withProperties(PROPS2)
.withRepresentationProperty(PROP2).withLabel("label").build(),
DiscoveryResultBuilder.create(THING_UID22).withThingType(THING_TYPE_UID21).withProperties(PROPS2)
.withLabel("label").build());

@Before
public void setUp() throws Exception {
RESULTS.get(3).setFlag(DiscoveryResultFlag.IGNORED);
((DiscoveryResultImpl) RESULTS.get(3)).setFlag(DiscoveryResultFlag.IGNORED);
}

@Test
Expand Down
Loading

0 comments on commit a247a02

Please sign in to comment.