This commit is contained in:
Redkale
2020-05-27 18:02:51 +08:00
parent 0f7520e67b
commit 72a618a0b7
2 changed files with 88 additions and 11 deletions

View File

@@ -28,9 +28,19 @@ public enum ConvertType {
this.value = v;
}
public int getValue() {
return value;
}
public boolean contains(ConvertType type) {
if (type == null) return false;
return this.value >= type.value && (this.value & type.value) > 0;
}
public static ConvertType find(int value) {
for (ConvertType t : ConvertType.values()) {
if (value == t.value) return t;
}
return null;
}
}

View File

@@ -21,48 +21,88 @@ import org.redkale.util.Comment;
*/
public class MessageRecord implements Serializable {
public static final byte FORMAT_TEXT = 1;
public static final byte FORMAT_BINARY = 2;
@ConvertColumn(index = 1)
@Comment("消息序列号")
protected long seqid;
@ConvertColumn(index = 2)
@Comment("内容的格式")
protected byte format;
@ConvertColumn(index = 3)
@Comment("标记位, 自定义时使用")
protected int flag;
@ConvertColumn(index = 4)
@Comment("用户ID无用户信息视为0")
protected int userid;
@ConvertColumn(index = 5)
@Comment("组ID")
protected String groupid;
@ConvertColumn(index = 6)
@Comment("当前topic")
protected String topic;
@ConvertColumn(index = 7)
@Comment("目标topic, 为空表示无目标topic")
protected String resptopic;
@ConvertColumn(index = 8)
@Comment("消息内容")
protected byte[] content;
public MessageRecord() {
}
public MessageRecord(String resptopic, String content) {
this(System.nanoTime(), content == null ? 0 : FORMAT_TEXT, 0, 0, null, null, resptopic, content == null ? null : content.getBytes(StandardCharsets.UTF_8));
}
public MessageRecord(String topic, String resptopic, String content) {
this(System.nanoTime(), content == null ? 0 : FORMAT_TEXT, 0, 0, null, topic, resptopic, content == null ? null : content.getBytes(StandardCharsets.UTF_8));
}
public MessageRecord(int userid, String topic, String resptopic, String content) {
this(System.nanoTime(), content == null ? 0 : FORMAT_TEXT, 0, userid, null, topic, resptopic, content == null ? null : content.getBytes(StandardCharsets.UTF_8));
}
public MessageRecord(byte format, String topic, String resptopic, byte[] content) {
this(System.nanoTime(), format, 0, 0, null, topic, resptopic, content);
}
public MessageRecord(long seqid, byte format, String topic, String resptopic, byte[] content) {
this(seqid, format, 0, null, topic, resptopic, content);
}
public MessageRecord(long seqid, byte format, int userid, String groupid, String topic, String resptopic, byte[] content) {
this(seqid, format, 0, userid, groupid, topic, resptopic, content);
}
public MessageRecord(String topic, String resptopic, Convert convert, Object bean) {
this(System.nanoTime(), 0, 0, null, topic, resptopic, convert.convertToBytes(bean));
this(0, null, topic, resptopic, convert, bean);
}
public MessageRecord(String topic, String resptopic, byte[] content) {
this(System.nanoTime(), 0, 0, null, topic, resptopic, content);
public MessageRecord(int userid, String topic, String resptopic, Convert convert, Object bean) {
this(userid, null, topic, resptopic, convert, bean);
}
public MessageRecord(long seqid, String topic, String resptopic, byte[] content) {
this(seqid, 0, 0, null, topic, resptopic, content);
public MessageRecord(int userid, String groupid, String topic, String resptopic, Convert convert, Object bean) {
this(0, userid, groupid, topic, resptopic, convert, bean);
}
public MessageRecord(long seqid, int userid, String groupid, String topic, String resptopic, byte[] content) {
this(seqid, 0, userid, groupid, topic, resptopic, content);
public MessageRecord(int flag, int userid, String groupid, String topic, String resptopic, Convert convert, Object bean) {
this(System.nanoTime(), convert instanceof TextConvert ? FORMAT_TEXT : FORMAT_BINARY, flag, userid, groupid, topic, resptopic, convert.convertToBytes(bean));
}
public MessageRecord(long seqid, int flag, int userid, String groupid, String topic, String resptopic, byte[] content) {
public MessageRecord(long seqid, byte format, int flag, int userid, String groupid, String topic, String resptopic, byte[] content) {
this.seqid = seqid;
this.format = format;
this.flag = flag;
this.userid = userid;
this.groupid = groupid;
@@ -71,7 +111,7 @@ public class MessageRecord implements Serializable {
this.content = content;
}
public String contentUTF8String() {
public String contentString() {
return content == null ? null : new String(content, StandardCharsets.UTF_8);
}
@@ -85,6 +125,11 @@ public class MessageRecord implements Serializable {
return this.resptopic == null || this.resptopic.isEmpty();
}
public MessageRecord format(byte format) {
this.format = format;
return this;
}
public MessageRecord flag(int flag) {
this.flag = flag;
return this;
@@ -115,7 +160,7 @@ public class MessageRecord implements Serializable {
return this;
}
public MessageRecord contentUTF8String(String content) {
public MessageRecord contentString(String content) {
this.content = content == null ? null : content.getBytes(StandardCharsets.UTF_8);
return this;
}
@@ -128,6 +173,14 @@ public class MessageRecord implements Serializable {
this.seqid = seqid;
}
public byte getFormat() {
return format;
}
public void setFormat(byte format) {
this.format = format;
}
public int getFlag() {
return flag;
}
@@ -178,7 +231,21 @@ public class MessageRecord implements Serializable {
@Override
public String toString() {
return JsonConvert.root().convertTo(this);
//return JsonConvert.root().convertTo(this);
StringBuilder sb = new StringBuilder(128);
sb.append("{\"seqid\":").append(this.seqid);
if (this.format != 0) sb.append(",\"format\":").append(this.format);
if (this.flag != 0) sb.append(",\"flag\":").append(this.flag);
if (this.userid != 0) sb.append(",\"userid\":").append(this.userid);
if (this.groupid != null) sb.append(",\"groupid\":\"").append(this.groupid).append("\"");
if (this.topic != null) sb.append(",\"topic\":\"").append(this.topic).append("\"");
if (this.resptopic != null) sb.append(",\"resptopic\":\"").append(this.resptopic).append("\"");
if (this.content != null) sb.append(",\"content\":").append(this.format == FORMAT_TEXT ? ("\"" + new String(this.content, StandardCharsets.UTF_8) + "\"") : JsonConvert.root().convertTo(this.content));
sb.append("}");
return sb.toString();
}
public static void main(String[] args) throws Throwable {
System.out.println(new MessageRecord(333, FORMAT_TEXT, 2, 3, null, "tt", null, "xxx".getBytes()));
}
}