Skip to content

Commit

Permalink
refactor: 优化标签设置
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Aug 16, 2024
1 parent af558e7 commit 303c69e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/main/java/org/jetlinks/core/message/UpdateTagMessage.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jetlinks.core.message;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import lombok.Setter;
import org.jetlinks.core.utils.SerializeUtils;

Expand All @@ -9,6 +10,7 @@
import java.io.ObjectOutput;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class UpdateTagMessage extends CommonDeviceMessage<UpdateTagMessage> implements UpdateTingTagsMessage {
Expand All @@ -21,6 +23,9 @@ public Map<String, Object> getTags() {
}

public synchronized UpdateTagMessage tag(String tag, Object value) {
if (value == null) {
return this;
}
if (tags == null) {
tags = new ConcurrentHashMap<>();
}
Expand All @@ -33,7 +38,7 @@ public synchronized UpdateTagMessage tags(Map<String, Object> tags) {
return this;
}
if (this.tags == null) {
this.tags = new ConcurrentHashMap<>(tags);
this.tags = new ConcurrentHashMap<>(Maps.filterValues(tags, Objects::nonNull));
return this;
}
this.tags.putAll(tags);
Expand All @@ -48,18 +53,18 @@ public MessageType getMessageType() {
@Override
public void fromJson(JSONObject jsonObject) {
super.fromJson(jsonObject);
this.tags = jsonObject.getJSONObject("tags");
tags(jsonObject.getJSONObject("tags"));
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
UpdateTingTagsMessage.super.writeExternal(out);
SerializeUtils.writeKeyValue(tags,out);
SerializeUtils.writeKeyValue(tags, out);
}

@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
UpdateTingTagsMessage.super.readExternal(in);
SerializeUtils.readKeyValue(in,this::tag);
SerializeUtils.readKeyValue(in, this::tag);
}
}
19 changes: 19 additions & 0 deletions src/test/java/org/jetlinks/core/message/UpdateTagMessageTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jetlinks.core.message;

import org.junit.Test;

import java.util.Collections;

import static org.junit.Assert.*;

public class UpdateTagMessageTest {


@Test
public void testNull(){
UpdateTagMessage message = new UpdateTagMessage();
message.tag("null",null);
message.tags(Collections.singletonMap("null",null));

}
}

0 comments on commit 303c69e

Please sign in to comment.