Skip to content

Commit

Permalink
Refactor java.util.Date usages to java.time.Instant (#4026)
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur authored Jan 7, 2024
1 parent 3c61c1d commit 6b2182d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -190,7 +190,7 @@ public synchronized void startScan(@Nullable ScanListener listener) {
}
}, getScanTimeout(), TimeUnit.SECONDS);
}
timestampOfLastScan = new Date().getTime();
timestampOfLastScan = Instant.now();

try {
startScan();
Expand Down Expand Up @@ -421,7 +421,7 @@ protected void stopBackgroundDiscovery() {
* @return timestamp as long
*/
protected long getTimestampOfLastScan() {
return timestampOfLastScan;
return Instant.MIN.equals(timestampOfLastScan) ? 0 : timestampOfLastScan.toEpochMilli();
}

private String inferKey(DiscoveryResult discoveryResult, String lastSegment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -86,7 +86,7 @@ public DiscoveryResultImpl(@Nullable ThingTypeUID thingTypeUID, ThingUID thingUI
this.representationProperty = representationProperty;
this.label = label == null ? "" : label;

this.timestamp = new Date().getTime();
this.timestamp = Instant.now();
this.timeToLive = timeToLive;

this.flag = DiscoveryResultFlag.NEW;
Expand Down Expand Up @@ -157,7 +157,7 @@ public void synchronize(@Nullable DiscoveryResult sourceResult) {
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();
}
}
Expand Down Expand Up @@ -221,7 +221,7 @@ public String toString() {

@Override
public long getTimestamp() {
return timestamp;
return Instant.MIN.equals(timestamp) ? 0 : timestamp.toEpochMilli();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
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;
Expand Down Expand Up @@ -112,7 +112,7 @@ public TimeToLiveCheckingThread(PersistentInbox inbox) {

@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());
Expand All @@ -121,11 +121,12 @@ public void run() {
}
}

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);
}
}

Expand Down Expand Up @@ -442,7 +443,7 @@ public void thingRemoved(DiscoveryService source, ThingUID thingUID) {
removedThings.add(thingUID);
remove(thingUID);
logger.debug("Removed thing '{}' from inbox because it was older than {}.", thingUID,
new Date(timestamp));
Instant.ofEpochMilli(timestamp));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

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;
Expand Down Expand Up @@ -163,7 +163,7 @@ private void printInboxEntries(Console console, List<DiscoveryResult> discoveryR
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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

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;
Expand Down Expand Up @@ -148,7 +148,7 @@ protected void startScan() {
put("pnr", 1234455);
put("snr", 12345);
put("manufacturer", "huawei");
put("manufactured", new Date(12344));
put("manufactured", Instant.ofEpochMilli(12344));
}
};

Expand Down Expand Up @@ -1019,7 +1019,7 @@ public Collection<ConfigDescription> getConfigDescriptions(@Nullable Locale loca
@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
Expand All @@ -1034,7 +1034,7 @@ public void assertThatRemoveOlderResultsOnlyRemovesResultsFromTheSameDiscoverySe
@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
Expand Down

0 comments on commit 6b2182d

Please sign in to comment.