9
9
import java .util .Objects ;
10
10
import java .util .TreeMap ;
11
11
import java .util .concurrent .TimeUnit ;
12
+ import java .util .function .Function ;
12
13
13
- import com .google .common .base .Preconditions ;
14
- import com .google .common .base .Strings ;
15
- import com .google .common .escape .Escaper ;
16
- import com .google .common .escape .Escapers ;
14
+ import org .influxdb .impl .Preconditions ;
17
15
18
16
/**
19
17
* Representation of a InfluxDB database Point.
@@ -28,15 +26,10 @@ public class Point {
28
26
private TimeUnit precision = TimeUnit .NANOSECONDS ;
29
27
private Map <String , Object > fields ;
30
28
31
- private static final Escaper FIELD_ESCAPER = Escapers .builder ()
32
- .addEscape ('\\' , "\\ \\ " )
33
- .addEscape ('"' , "\\ \" " )
34
- .build ();
35
- private static final Escaper KEY_ESCAPER = Escapers .builder ()
36
- .addEscape (' ' , "\\ " )
37
- .addEscape (',' , "\\ ," )
38
- .addEscape ('=' , "\\ =" )
39
- .build ();
29
+ private static final Function <String , String > FIELD_ESCAPER = s ->
30
+ s .replace ("\\ " , "\\ \\ " ).replace ("\" " , "\\ \" " );
31
+ private static final Function <String , String > KEY_ESCAPER = s ->
32
+ s .replace (" " , "\\ " ).replace ("," , "\\ ," ).replace ("=" , "\\ =" );
40
33
private static final int MAX_FRACTION_DIGITS = 340 ;
41
34
42
35
Point () {
@@ -84,8 +77,8 @@ public static final class Builder {
84
77
* @return the Builder instance.
85
78
*/
86
79
public Builder tag (final String tagName , final String value ) {
87
- Preconditions . checkArgument (tagName != null );
88
- Preconditions . checkArgument (value != null );
80
+ Objects . requireNonNull (tagName , "tagName" );
81
+ Objects . requireNonNull (value , "value" );
89
82
if (!tagName .isEmpty () && !value .isEmpty ()) {
90
83
tags .put (tagName , value );
91
84
}
@@ -161,9 +154,7 @@ public Builder addField(final String field, final Number value) {
161
154
}
162
155
163
156
public Builder addField (final String field , final String value ) {
164
- if (value == null ) {
165
- throw new IllegalArgumentException ("Field value cannot be null" );
166
- }
157
+ Objects .requireNonNull (value , "value" );
167
158
168
159
fields .put (field , value );
169
160
return this ;
@@ -189,7 +180,7 @@ public Builder fields(final Map<String, Object> fieldsToAdd) {
189
180
* @return the Builder instance.
190
181
*/
191
182
public Builder time (final long timeToSet , final TimeUnit precisionToSet ) {
192
- Preconditions . checkNotNull (precisionToSet , "Precision must be not null! " );
183
+ Objects . requireNonNull (precisionToSet , "precisionToSet " );
193
184
this .time = timeToSet ;
194
185
this .precision = precisionToSet ;
195
186
return this ;
@@ -201,12 +192,8 @@ public Builder time(final long timeToSet, final TimeUnit precisionToSet) {
201
192
* @return the newly created Point.
202
193
*/
203
194
public Point build () {
204
- Preconditions
205
- .checkArgument (!Strings .isNullOrEmpty (this .measurement ),
206
- "Point name must not be null or empty." );
207
- Preconditions
208
- .checkArgument (this .fields .size () > 0 ,
209
- "Point must have at least one field specified." );
195
+ Preconditions .checkNonEmptyString (this .measurement , "measurement" );
196
+ Preconditions .checkPositiveNumber (this .fields .size (), "fields size" );
210
197
Point point = new Point ();
211
198
point .setFields (this .fields );
212
199
point .setMeasurement (this .measurement );
@@ -321,7 +308,7 @@ public String toString() {
321
308
*/
322
309
public String lineProtocol () {
323
310
final StringBuilder sb = new StringBuilder ();
324
- sb .append (KEY_ESCAPER .escape (this .measurement ));
311
+ sb .append (KEY_ESCAPER .apply (this .measurement ));
325
312
sb .append (concatenatedTags ());
326
313
sb .append (concatenateFields ());
327
314
sb .append (formatedTime ());
@@ -332,9 +319,9 @@ private StringBuilder concatenatedTags() {
332
319
final StringBuilder sb = new StringBuilder ();
333
320
for (Entry <String , String > tag : this .tags .entrySet ()) {
334
321
sb .append ("," )
335
- .append (KEY_ESCAPER .escape (tag .getKey ()))
322
+ .append (KEY_ESCAPER .apply (tag .getKey ()))
336
323
.append ("=" )
337
- .append (KEY_ESCAPER .escape (tag .getValue ()));
324
+ .append (KEY_ESCAPER .apply (tag .getValue ()));
338
325
}
339
326
sb .append (" " );
340
327
return sb ;
@@ -357,10 +344,10 @@ private StringBuilder concatenateFields() {
357
344
continue ;
358
345
}
359
346
360
- sb .append (KEY_ESCAPER .escape (field .getKey ())).append ("=" );
347
+ sb .append (KEY_ESCAPER .apply (field .getKey ())).append ("=" );
361
348
if (value instanceof String ) {
362
349
String stringValue = (String ) value ;
363
- sb .append ("\" " ).append (FIELD_ESCAPER .escape (stringValue )).append ("\" " );
350
+ sb .append ("\" " ).append (FIELD_ESCAPER .apply (stringValue )).append ("\" " );
364
351
} else if (value instanceof Number ) {
365
352
if (value instanceof Double || value instanceof Float || value instanceof BigDecimal ) {
366
353
sb .append (numberFormat .format (value ));
0 commit comments