Skip to content

Commit

Permalink
#1304 Refactored timestamps to use BigDecimal and Instant
Browse files Browse the repository at this point in the history
  • Loading branch information
adamsero committed Oct 5, 2022
1 parent d4939f0 commit 2fdf9f1
Show file tree
Hide file tree
Showing 28 changed files with 426 additions and 196 deletions.
4 changes: 2 additions & 2 deletions build-config/lib-build-config/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ Contributors:
<configuration>
<signature>
<groupId>net.sf.androidscents.signature</groupId>
<artifactId>android-api-level-19</artifactId>
<version>4.4.2_r4</version>
<artifactId>android-api-level-26</artifactId>
<version>8.0.0_r2</version>
</signature>
</configuration>
<executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*******************************************************************************/
package org.eclipse.leshan.client.send;

import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.eclipse.leshan.client.servers.ServerIdentity;
import org.eclipse.leshan.core.node.LwM2mNode;
Expand Down Expand Up @@ -45,7 +45,7 @@ public ManualDataSender(String name) {
}

public synchronized void collectData(List<LwM2mPath> paths) {
long currentTimestamp = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
Instant currentTimestamp = Instant.now();
Map<LwM2mPath, LwM2mNode> currentValues = dataSenderManager.getCurrentValues(ServerIdentity.SYSTEM, paths);
synchronized (this) {
builder.addNodes(currentTimestamp, currentValues);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.eclipse.leshan.client.send;

import java.time.Instant;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -86,7 +87,7 @@ public void test_collect_several_data() throws InterruptedException {

// ensure that sent values equals collected ones.
TimestampedLwM2mNodes lastValuesSent = fakeDataSenderManager.getLastValuesSent();
List<Long> timestamps = new ArrayList<>(lastValuesSent.getTimestamps());
List<Instant> timestamps = new ArrayList<>(lastValuesSent.getTimestamps());
Assert.assertEquals(3, timestamps.size());
Assert.assertEquals(firstValue, lastValuesSent.getNodesAt(timestamps.get(0)));
Assert.assertEquals(secondValue, lastValuesSent.getNodesAt(timestamps.get(1)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.eclipse.leshan.core.json;

import java.math.BigDecimal;

import org.eclipse.leshan.core.model.ResourceModel;
import org.eclipse.leshan.core.model.ResourceModel.Type;

Expand All @@ -32,7 +34,7 @@ public class JsonArrayEntry {

private String stringValue;

private Long time;
private BigDecimal time;

public ResourceModel.Type getType() {
if (booleanValue != null) {
Expand All @@ -58,11 +60,11 @@ public void setName(String name) {
this.name = name;
}

public Long getTime() {
public BigDecimal getTime() {
return time;
}

public void setTime(Long time) {
public void setTime(BigDecimal time) {
this.time = time;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.eclipse.leshan.core.json;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

Expand All @@ -29,7 +30,7 @@ public class JsonRootObject {

private List<JsonArrayEntry> jsonArray;

private Long baseTime;
private BigDecimal baseTime;

public JsonRootObject() {
}
Expand All @@ -42,11 +43,11 @@ public void setBaseName(String baseName) {
this.baseName = baseName;
}

public Long getBaseTime() {
public BigDecimal getBaseTime() {
return baseTime;
}

public void setBaseTime(Long baseTime) {
public void setBaseTime(BigDecimal baseTime) {
this.baseTime = baseTime;
}

Expand Down Expand Up @@ -99,7 +100,7 @@ public boolean equals(Object obj) {

@Override
public String toString() {
return String.format("LwM2mJsonElement [baseName=%s, baseTime=%d, resourceList=%s]", baseName, baseTime,
return String.format("LwM2mJsonElement [baseName=%s, baseTime=%s, resourceList=%s]", baseName, baseTime,
jsonArray);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class JsonArrayEntrySerDes extends JacksonJsonSerDes<JsonArrayEntry> {

@Override
public JsonNode jSerialize(JsonArrayEntry jae) throws JsonException {
ObjectNode o = JsonNodeFactory.instance.objectNode();
ObjectNode o = JsonNodeFactory.withExactBigDecimals(true).objectNode();
if (jae.getName() != null)
o.put("n", jae.getName());
Type type = jae.getType();
Expand Down Expand Up @@ -95,7 +95,7 @@ public JsonArrayEntry deserialize(JsonNode o) throws JsonException {

JsonNode t = o.get("t");
if (t != null && t.isNumber())
jae.setTime(t.asLong());
jae.setTime(new BigDecimal(t.asText()));

JsonNode v = o.get("v");
if (v != null && v.isNumber())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*******************************************************************************/
package org.eclipse.leshan.core.json.jackson;

import java.math.BigDecimal;

import org.eclipse.leshan.core.json.JsonRootObject;
import org.eclipse.leshan.core.util.json.JacksonJsonSerDes;
import org.eclipse.leshan.core.util.json.JsonException;
Expand All @@ -30,7 +32,7 @@ public class JsonRootObjectSerDes extends JacksonJsonSerDes<JsonRootObject> {

@Override
public JsonNode jSerialize(JsonRootObject jro) throws JsonException {
ObjectNode o = JsonNodeFactory.instance.objectNode();
ObjectNode o = JsonNodeFactory.withExactBigDecimals(true).objectNode();

if (jro.getBaseName() != null)
o.put("bn", jro.getBaseName());
Expand Down Expand Up @@ -64,7 +66,7 @@ public JsonRootObject deserialize(JsonNode jsonNode) throws JsonException {

JsonNode bt = jsonNode.get("bt");
if (bt != null && bt.isNumber())
jro.setBaseTime(bt.asLong());
jro.setBaseTime(new BigDecimal(bt.asText()));

return jro;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@
*******************************************************************************/
package org.eclipse.leshan.core.node;

import java.time.Instant;

import org.eclipse.leshan.core.util.Validate;

public class TimestampedLwM2mNode {

private final Long timestamp;
private final Instant timestamp;

private final LwM2mNode node;

public TimestampedLwM2mNode(Long timestamp, LwM2mNode node) {
public TimestampedLwM2mNode(Instant timestamp, LwM2mNode node) {
Validate.notNull(node);
this.timestamp = timestamp;
this.node = node;
}

public Long getTimestamp() {
public Instant getTimestamp() {
return timestamp;
}

Expand All @@ -38,7 +40,7 @@ public LwM2mNode getNode() {
}

public boolean isTimestamped() {
return timestamp != null && timestamp >= 0;
return timestamp != null && timestamp.isAfter(Instant.EPOCH);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/
package org.eclipse.leshan.core.node;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -30,9 +31,9 @@
*/
public class TimestampedLwM2mNodes {

private final Map<Long, Map<LwM2mPath, LwM2mNode>> timestampedPathNodesMap;
private final Map<Instant, Map<LwM2mPath, LwM2mNode>> timestampedPathNodesMap;

private TimestampedLwM2mNodes(Map<Long, Map<LwM2mPath, LwM2mNode>> timestampedPathNodesMap) {
private TimestampedLwM2mNodes(Map<Instant, Map<LwM2mPath, LwM2mNode>> timestampedPathNodesMap) {
this.timestampedPathNodesMap = timestampedPathNodesMap;
}

Expand All @@ -41,7 +42,7 @@ private TimestampedLwM2mNodes(Map<Long, Map<LwM2mPath, LwM2mNode>> timestampedPa
*
* @return map of {@link LwM2mPath}-{@link LwM2mNode} or null if there is no value for asked timestamp.
*/
public Map<LwM2mPath, LwM2mNode> getNodesAt(Long timestamp) {
public Map<LwM2mPath, LwM2mNode> getNodesAt(Instant timestamp) {
Map<LwM2mPath, LwM2mNode> map = timestampedPathNodesMap.get(timestamp);
if (map != null) {
return Collections.unmodifiableMap(timestampedPathNodesMap.get(timestamp));
Expand All @@ -55,7 +56,7 @@ public Map<LwM2mPath, LwM2mNode> getNodesAt(Long timestamp) {
*/
public Map<LwM2mPath, LwM2mNode> getNodes() {
Map<LwM2mPath, LwM2mNode> result = new HashMap<>();
for (Map.Entry<Long, Map<LwM2mPath, LwM2mNode>> entry : timestampedPathNodesMap.entrySet()) {
for (Map.Entry<Instant, Map<LwM2mPath, LwM2mNode>> entry : timestampedPathNodesMap.entrySet()) {
result.putAll(entry.getValue());
}
return Collections.unmodifiableMap(result);
Expand All @@ -65,7 +66,7 @@ public Map<LwM2mPath, LwM2mNode> getNodes() {
* Returns the all sorted timestamps of contained nodes with ascending order. Null timestamp is considered as most
* recent one.
*/
public Set<Long> getTimestamps() {
public Set<Instant> getTimestamps() {
return Collections.unmodifiableSet(timestampedPathNodesMap.keySet());
}

Expand Down Expand Up @@ -110,11 +111,11 @@ public static Builder builder() {
public static class Builder {

private static class InternalNode {
Long timestamp;
Instant timestamp;
LwM2mPath path;
LwM2mNode node;

public InternalNode(Long timestamp, LwM2mPath path, LwM2mNode node) {
public InternalNode(Instant timestamp, LwM2mPath path, LwM2mNode node) {
this.timestamp = timestamp;
this.path = path;
this.node = node;
Expand All @@ -136,14 +137,14 @@ public Builder addNodes(Map<LwM2mPath, LwM2mNode> pathNodesMap) {
return this;
}

public Builder addNodes(long timestamp, Map<LwM2mPath, LwM2mNode> pathNodesMap) {
public Builder addNodes(Instant timestamp, Map<LwM2mPath, LwM2mNode> pathNodesMap) {
for (Entry<LwM2mPath, LwM2mNode> node : pathNodesMap.entrySet()) {
nodes.add(new InternalNode(timestamp, node.getKey(), node.getValue()));
}
return this;
}

public Builder put(Long timestamp, LwM2mPath path, LwM2mNode node) {
public Builder put(Instant timestamp, LwM2mPath path, LwM2mNode node) {
nodes.add(new InternalNode(timestamp, path, node));
return this;
}
Expand All @@ -154,7 +155,7 @@ public Builder put(LwM2mPath path, LwM2mNode node) {
}

public Builder add(TimestampedLwM2mNodes timestampedNodes) {
for (Long timestamp : timestampedNodes.getTimestamps()) {
for (Instant timestamp : timestampedNodes.getTimestamps()) {
Map<LwM2mPath, LwM2mNode> pathNodeMap = timestampedNodes.getNodesAt(timestamp);
for (Map.Entry<LwM2mPath, LwM2mNode> pathNodeEntry : pathNodeMap.entrySet()) {
nodes.add(new InternalNode(timestamp, pathNodeEntry.getKey(), pathNodeEntry.getValue()));
Expand All @@ -168,7 +169,7 @@ public Builder add(TimestampedLwM2mNodes timestampedNodes) {
* invalid.
*/
public TimestampedLwM2mNodes build() throws IllegalArgumentException {
Map<Long, Map<LwM2mPath, LwM2mNode>> timestampToPathToNode = new TreeMap<>(getTimestampComparator());
Map<Instant, Map<LwM2mPath, LwM2mNode>> timestampToPathToNode = new TreeMap<>(getTimestampComparator());

for (InternalNode internalNode : nodes) {
// validate path is consistent with Node
Expand All @@ -195,7 +196,7 @@ public TimestampedLwM2mNodes build() throws IllegalArgumentException {
return new TimestampedLwM2mNodes(timestampToPathToNode);
}

private static Comparator<Long> getTimestampComparator() {
private static Comparator<Instant> getTimestampComparator() {
return (o1, o2) -> {
if (o1 == null) {
return (o2 == null) ? 0 : 1;
Expand Down
Loading

0 comments on commit 2fdf9f1

Please sign in to comment.