-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathMessage.java
More file actions
174 lines (149 loc) · 4.76 KB
/
Message.java
File metadata and controls
174 lines (149 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package com.timgroup.statsd;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Set;
public abstract class Message implements Comparable<Message> {
final String aspect;
final Message.Type type;
final String[] tags;
protected boolean done;
// borrowed from Array.hashCode implementation:
// https://github.com/openjdk/jdk11/blob/master/src/java.base/share/classes/java/util/Arrays.java#L4454-L4465
protected static final int HASH_MULTIPLIER = 31;
public enum Type {
GAUGE("g"),
COUNT("c"),
TIME("ms"),
SET("s"),
HISTOGRAM("h"),
DISTRIBUTION("d"),
EVENT("_e"),
SERVICE_CHECK("_sc"),
SKETCH("S");
private final String type;
Type(String type) {
this.type = type;
}
public String toString() {
return this.type;
}
}
protected static final Set<Type> AGGREGATE_SET = EnumSet.of(Type.COUNT, Type.GAUGE, Type.SET);
protected Message(Message.Type type) {
this("", type, null);
}
protected Message(String aspect, Message.Type type, String[] tags) {
this.aspect = aspect == null ? "" : aspect;
this.type = type;
this.done = false;
this.tags = tags;
}
/**
* Write this message to the provided {@link StringBuilder}. Will
* be called from the sender threads.
*
* @param builder StringBuilder the text representation will be written to.
* @param capacity The capacity of the send buffer.
* @param containerID The container ID to be appended to the message.
* @return boolean indicating whether the message was partially written to the builder.
* If true, the method will be called again with the same arguments to continue writing.
*/
abstract boolean writeTo(StringBuilder builder, int capacity, String containerID);
/**
* Aggregate message.
*
* @param message
* Message to aggregate.
*/
public abstract void aggregate(Message message);
/**
* Return the message aspect.
*
* @return returns the string representing the Message aspect
*/
public final String getAspect() {
return this.aspect;
}
/**
* Return the message type.
*
* @return returns the dogstatsd type for the Message
*/
public final Type getType() {
return this.type;
}
/**
* Return the array of tags for the message.
*
* @return returns the string array of tags for the Message
*/
public String[] getTags() {
return this.tags;
}
/**
* Return whether a message can be aggregated.
*
* @return boolean on whether or not this message type may be aggregated.
*/
public boolean canAggregate() {
return AGGREGATE_SET.contains(type);
}
public void setDone(boolean done) {
this.done = done;
}
public boolean getDone() {
return this.done;
}
/**
* Messages must implement hashCode.
*/
@Override
public int hashCode() {
return type.hashCode() * HASH_MULTIPLIER * HASH_MULTIPLIER
+ aspect.hashCode() * HASH_MULTIPLIER
+ Arrays.hashCode(this.tags);
}
/**
* Messages must implement hashCode.
*/
@Override
public boolean equals(Object object) {
if (object == this) {
return true;
}
if (object instanceof Message) {
final Message msg = (Message)object;
return (Objects.equals(this.getAspect(), msg.getAspect()))
&& (this.getType() == msg.getType())
&& Arrays.equals(this.tags, msg.getTags());
}
return false;
}
@Override
public int compareTo(Message message) {
int typeComparison = getType().compareTo(message.getType());
if (typeComparison == 0) {
int aspectComparison = getAspect().compareTo(message.getAspect());
if (aspectComparison == 0) {
if (tags == null && message.tags == null) {
return 0;
} else if (tags == null) {
return 1;
} else if (message.tags == null) {
return -1;
}
if (tags.length == message.tags.length) {
int comparison = 0;
for (int i = 0; i < tags.length && comparison == 0; i++) {
comparison = tags[i].compareTo(message.tags[i]);
}
return comparison;
}
return tags.length < message.tags.length ? 1 : -1;
}
return aspectComparison;
}
return typeComparison;
}
}