mirror of
https://github.com/danieldemus/openhab-core.git
synced 2025-01-25 11:45:49 +01:00
Refactor java.util.Date usages to java.time.Instant (#4026)
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
parent
3c61c1d649
commit
6b2182dec6
@ -12,9 +12,9 @@
|
||||
*/
|
||||
package org.openhab.core.config.discovery;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@ -69,7 +69,7 @@ public abstract class AbstractDiscoveryService implements DiscoveryService {
|
||||
private final Set<ThingTypeUID> supportedThingTypes;
|
||||
private final int timeout;
|
||||
|
||||
private long timestampOfLastScan = 0L;
|
||||
private Instant timestampOfLastScan = Instant.MIN;
|
||||
|
||||
private @Nullable ScheduledFuture<?> scheduledStop;
|
||||
|
||||
@ -190,7 +190,7 @@ public abstract class AbstractDiscoveryService implements DiscoveryService {
|
||||
}
|
||||
}, getScanTimeout(), TimeUnit.SECONDS);
|
||||
}
|
||||
timestampOfLastScan = new Date().getTime();
|
||||
timestampOfLastScan = Instant.now();
|
||||
|
||||
try {
|
||||
startScan();
|
||||
@ -421,7 +421,7 @@ public abstract class AbstractDiscoveryService implements DiscoveryService {
|
||||
* @return timestamp as long
|
||||
*/
|
||||
protected long getTimestampOfLastScan() {
|
||||
return timestampOfLastScan;
|
||||
return Instant.MIN.equals(timestampOfLastScan) ? 0 : timestampOfLastScan.toEpochMilli();
|
||||
}
|
||||
|
||||
private String inferKey(DiscoveryResult discoveryResult, String lastSegment) {
|
||||
|
@ -12,8 +12,8 @@
|
||||
*/
|
||||
package org.openhab.core.config.discovery.internal;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@ -43,7 +43,7 @@ public class DiscoveryResultImpl implements DiscoveryResult {
|
||||
private @Nullable String representationProperty;
|
||||
private @NonNullByDefault({}) DiscoveryResultFlag flag;
|
||||
private @NonNullByDefault({}) String label;
|
||||
private long timestamp;
|
||||
private Instant timestamp = Instant.MIN;
|
||||
private long timeToLive = TTL_UNLIMITED;
|
||||
|
||||
/**
|
||||
@ -86,7 +86,7 @@ public class DiscoveryResultImpl implements DiscoveryResult {
|
||||
this.representationProperty = representationProperty;
|
||||
this.label = label == null ? "" : label;
|
||||
|
||||
this.timestamp = new Date().getTime();
|
||||
this.timestamp = Instant.now();
|
||||
this.timeToLive = timeToLive;
|
||||
|
||||
this.flag = DiscoveryResultFlag.NEW;
|
||||
@ -157,7 +157,7 @@ public class DiscoveryResultImpl implements DiscoveryResult {
|
||||
this.properties = sourceResult.getProperties();
|
||||
this.representationProperty = sourceResult.getRepresentationProperty();
|
||||
this.label = sourceResult.getLabel();
|
||||
this.timestamp = new Date().getTime();
|
||||
this.timestamp = Instant.now();
|
||||
this.timeToLive = sourceResult.getTimeToLive();
|
||||
}
|
||||
}
|
||||
@ -221,7 +221,7 @@ public class DiscoveryResultImpl implements DiscoveryResult {
|
||||
|
||||
@Override
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
return Instant.MIN.equals(timestamp) ? 0 : timestamp.toEpochMilli();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -15,9 +15,9 @@ package org.openhab.core.config.discovery.internal;
|
||||
import static org.openhab.core.config.discovery.inbox.InboxPredicates.forThingUID;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -112,7 +112,7 @@ public final class PersistentInbox implements Inbox, DiscoveryListener, ThingReg
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
long now = new Date().getTime();
|
||||
Instant now = Instant.now();
|
||||
for (DiscoveryResult result : inbox.getAll()) {
|
||||
if (isResultExpired(result, now)) {
|
||||
logger.debug("Inbox entry for thing '{}' is expired and will be removed.", result.getThingUID());
|
||||
@ -121,11 +121,12 @@ public final class PersistentInbox implements Inbox, DiscoveryListener, ThingReg
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isResultExpired(DiscoveryResult result, long now) {
|
||||
if (result.getTimeToLive() == DiscoveryResult.TTL_UNLIMITED) {
|
||||
private boolean isResultExpired(DiscoveryResult result, Instant now) {
|
||||
long ttl = result.getTimeToLive();
|
||||
if (ttl == DiscoveryResult.TTL_UNLIMITED) {
|
||||
return false;
|
||||
}
|
||||
return (result.getTimestamp() + result.getTimeToLive() * 1000 < now);
|
||||
return Instant.ofEpochMilli(result.getTimestamp()).plusSeconds(ttl).isBefore(now);
|
||||
}
|
||||
}
|
||||
|
||||
@ -442,7 +443,7 @@ public final class PersistentInbox implements Inbox, DiscoveryListener, ThingReg
|
||||
removedThings.add(thingUID);
|
||||
remove(thingUID);
|
||||
logger.debug("Removed thing '{}' from inbox because it was older than {}.", thingUID,
|
||||
new Date(timestamp));
|
||||
Instant.ofEpochMilli(timestamp));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ package org.openhab.core.config.discovery.internal.console;
|
||||
|
||||
import static org.openhab.core.config.discovery.inbox.InboxPredicates.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@ -163,7 +163,7 @@ public class InboxConsoleCommandExtension extends AbstractConsoleCommandExtensio
|
||||
ThingUID bridgeId = discoveryResult.getBridgeUID();
|
||||
Map<String, Object> properties = discoveryResult.getProperties();
|
||||
String representationProperty = discoveryResult.getRepresentationProperty();
|
||||
String timestamp = new Date(discoveryResult.getTimestamp()).toString();
|
||||
String timestamp = Instant.ofEpochMilli(discoveryResult.getTimestamp()).toString();
|
||||
String timeToLive = discoveryResult.getTimeToLive() == DiscoveryResult.TTL_UNLIMITED ? "UNLIMITED"
|
||||
: "" + discoveryResult.getTimeToLive();
|
||||
console.println(String.format(
|
||||
|
@ -20,9 +20,9 @@ import static org.openhab.core.config.discovery.inbox.InboxPredicates.*;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.net.URI;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -148,7 +148,7 @@ public class InboxOSGiTest extends JavaOSGiTest {
|
||||
put("pnr", 1234455);
|
||||
put("snr", 12345);
|
||||
put("manufacturer", "huawei");
|
||||
put("manufactured", new Date(12344));
|
||||
put("manufactured", Instant.ofEpochMilli(12344));
|
||||
}
|
||||
};
|
||||
|
||||
@ -1019,7 +1019,7 @@ public class InboxOSGiTest extends JavaOSGiTest {
|
||||
@Test
|
||||
public void assertThatRemoveOlderResultsOnlyRemovesResultsFromTheSameDiscoveryService() {
|
||||
inbox.thingDiscovered(discoveryService1, testDiscoveryResult);
|
||||
long now = new Date().getTime() + 1;
|
||||
long now = Instant.now().toEpochMilli() + 1;
|
||||
assertThat(inbox.getAll().size(), is(1));
|
||||
|
||||
// should not remove a result
|
||||
@ -1034,7 +1034,7 @@ public class InboxOSGiTest extends JavaOSGiTest {
|
||||
@Test
|
||||
public void assertThatRemoveOlderResultsRemovesResultsWithoutAsource() {
|
||||
inbox.add(testDiscoveryResult);
|
||||
long now = new Date().getTime() + 1;
|
||||
long now = Instant.now().toEpochMilli() + 1;
|
||||
assertThat(inbox.getAll().size(), is(1));
|
||||
|
||||
// should remove a result
|
||||
|
Loading…
Reference in New Issue
Block a user