10
10
import java .util .Objects ;
11
11
import java .util .TreeMap ;
12
12
import java .util .concurrent .TimeUnit ;
13
+ import java .util .function .Function ;
13
14
14
- import com .google .common .base .Preconditions ;
15
- import com .google .common .base .Strings ;
16
- import com .google .common .escape .Escaper ;
17
- import com .google .common .escape .Escapers ;
15
+ import org .influxdb .impl .Preconditions ;
18
16
19
17
/**
20
18
* Representation of a InfluxDB database Point.
@@ -29,16 +27,10 @@ public class Point {
29
27
private TimeUnit precision = TimeUnit .NANOSECONDS ;
30
28
private Map <String , Object > fields ;
31
29
32
- private static final Escaper FIELD_ESCAPER = Escapers .builder ()
33
- .addEscape ('\\' , "\\ \\ " )
34
- .addEscape ('"' , "\\ \" " )
35
- .build ();
36
- private static final Escaper KEY_ESCAPER = Escapers .builder ()
37
- .addEscape (' ' , "\\ " )
38
- .addEscape (',' , "\\ ," )
39
- .addEscape ('=' , "\\ =" )
40
- .build ();
41
-
30
+ private static final Function <String , String > FIELD_ESCAPER = s ->
31
+ s .replace ("\\ " , "\\ \\ " ).replace ("\" " , "\\ \" " );
32
+ private static final Function <String , String > KEY_ESCAPER = s ->
33
+ s .replace (" " , "\\ " ).replace ("," , "\\ ," ).replace ("=" , "\\ =" );
42
34
private static final int MAX_FRACTION_DIGITS = 340 ;
43
35
private static final ThreadLocal <NumberFormat > NUMBER_FORMATTER =
44
36
ThreadLocal .withInitial (() -> {
@@ -97,8 +89,8 @@ public static final class Builder {
97
89
* @return the Builder instance.
98
90
*/
99
91
public Builder tag (final String tagName , final String value ) {
100
- Preconditions . checkArgument (tagName != null );
101
- Preconditions . checkArgument (value != null );
92
+ Objects . requireNonNull (tagName , "tagName" );
93
+ Objects . requireNonNull (value , "value" );
102
94
if (!tagName .isEmpty () && !value .isEmpty ()) {
103
95
tags .put (tagName , value );
104
96
}
@@ -169,9 +161,7 @@ public Builder addField(final String field, final Number value) {
169
161
}
170
162
171
163
public Builder addField (final String field , final String value ) {
172
- if (value == null ) {
173
- throw new IllegalArgumentException ("Field value cannot be null" );
174
- }
164
+ Objects .requireNonNull (value , "value" );
175
165
176
166
fields .put (field , value );
177
167
return this ;
@@ -197,7 +187,7 @@ public Builder fields(final Map<String, Object> fieldsToAdd) {
197
187
* @return the Builder instance.
198
188
*/
199
189
public Builder time (final long timeToSet , final TimeUnit precisionToSet ) {
200
- Preconditions . checkNotNull (precisionToSet , "Precision must be not null! " );
190
+ Objects . requireNonNull (precisionToSet , "precisionToSet " );
201
191
this .time = timeToSet ;
202
192
this .precision = precisionToSet ;
203
193
return this ;
@@ -209,12 +199,8 @@ public Builder time(final long timeToSet, final TimeUnit precisionToSet) {
209
199
* @return the newly created Point.
210
200
*/
211
201
public Point build () {
212
- Preconditions
213
- .checkArgument (!Strings .isNullOrEmpty (this .measurement ),
214
- "Point name must not be null or empty." );
215
- Preconditions
216
- .checkArgument (this .fields .size () > 0 ,
217
- "Point must have at least one field specified." );
202
+ Preconditions .checkNonEmptyString (this .measurement , "measurement" );
203
+ Preconditions .checkPositiveNumber (this .fields .size (), "fields size" );
218
204
Point point = new Point ();
219
205
point .setFields (this .fields );
220
206
point .setMeasurement (this .measurement );
@@ -343,9 +329,9 @@ public String lineProtocol() {
343
329
private void concatenatedTags (final StringBuilder sb ) {
344
330
for (Entry <String , String > tag : this .tags .entrySet ()) {
345
331
sb .append (',' )
346
- .append (KEY_ESCAPER .escape (tag .getKey ()))
332
+ .append (KEY_ESCAPER .apply (tag .getKey ()))
347
333
.append ('=' )
348
- .append (KEY_ESCAPER .escape (tag .getValue ()));
334
+ .append (KEY_ESCAPER .apply (tag .getValue ()));
349
335
}
350
336
sb .append (' ' );
351
337
}
@@ -357,7 +343,7 @@ private void concatenatedFields(final StringBuilder sb) {
357
343
continue ;
358
344
}
359
345
360
- sb .append (KEY_ESCAPER .escape (field .getKey ())).append ('=' );
346
+ sb .append (KEY_ESCAPER .apply (field .getKey ())).append ('=' );
361
347
if (value instanceof Number ) {
362
348
if (value instanceof Double || value instanceof Float || value instanceof BigDecimal ) {
363
349
sb .append (NUMBER_FORMATTER .get ().format (value ));
@@ -366,7 +352,7 @@ private void concatenatedFields(final StringBuilder sb) {
366
352
}
367
353
} else if (value instanceof String ) {
368
354
String stringValue = (String ) value ;
369
- sb .append ('"' ).append (FIELD_ESCAPER .escape (stringValue )).append ('"' );
355
+ sb .append ('"' ).append (FIELD_ESCAPER .apply (stringValue )).append ('"' );
370
356
} else {
371
357
sb .append (value );
372
358
}
@@ -390,7 +376,7 @@ private static class MeasurementStringBuilder {
390
376
private final int length ;
391
377
392
378
MeasurementStringBuilder (final String measurement ) {
393
- this .sb .append (KEY_ESCAPER .escape (measurement ));
379
+ this .sb .append (KEY_ESCAPER .apply (measurement ));
394
380
this .length = sb .length ();
395
381
}
396
382
0 commit comments