|
1 | 1 | package org.influxdb.dto;
|
2 | 2 |
|
| 3 | +import java.lang.annotation.Annotation; |
| 4 | +import java.lang.reflect.Field; |
3 | 5 | import java.math.BigDecimal;
|
4 | 6 | import java.math.BigInteger;
|
5 | 7 | import java.text.NumberFormat;
|
|
9 | 11 | import java.util.Objects;
|
10 | 12 | import java.util.TreeMap;
|
11 | 13 | import java.util.concurrent.TimeUnit;
|
| 14 | +import org.influxdb.BuilderException; |
| 15 | +import org.influxdb.annotation.Column; |
| 16 | +import org.influxdb.annotation.Measurement; |
12 | 17 | import org.influxdb.impl.Preconditions;
|
13 | 18 |
|
14 | 19 | /**
|
@@ -52,6 +57,28 @@ public static Builder measurement(final String measurement) {
|
52 | 57 | return new Builder(measurement);
|
53 | 58 | }
|
54 | 59 |
|
| 60 | + /** |
| 61 | + * Create a new Point Build build to create a new Point in a fluent manner from a POJO. |
| 62 | + * |
| 63 | + * @param clazz Class of the POJO |
| 64 | + * @return the Builder instance |
| 65 | + */ |
| 66 | + |
| 67 | + public static Builder measurementByPOJO(final Class<?> clazz) { |
| 68 | + Objects.requireNonNull(clazz, "clazz"); |
| 69 | + throwExceptionIfMissingAnnotation(clazz, Measurement.class); |
| 70 | + String measurementName = findMeasurementName(clazz); |
| 71 | + return new Builder(measurementName); |
| 72 | + } |
| 73 | + |
| 74 | + private static void throwExceptionIfMissingAnnotation(final Class<?> clazz, |
| 75 | + final Class<? extends Annotation> expectedClass) { |
| 76 | + if (!clazz.isAnnotationPresent(expectedClass)) { |
| 77 | + throw new IllegalArgumentException("Class " + clazz.getName() + " is not annotated with @" |
| 78 | + + Measurement.class.getSimpleName()); |
| 79 | + } |
| 80 | + } |
| 81 | + |
55 | 82 | /**
|
56 | 83 | * Builder for a new Point.
|
57 | 84 | *
|
@@ -195,6 +222,56 @@ public boolean hasFields() {
|
195 | 222 | return !fields.isEmpty();
|
196 | 223 | }
|
197 | 224 |
|
| 225 | + /** |
| 226 | + * Adds field map from object by reflection using {@link org.influxdb.annotation.Column} |
| 227 | + * annotation. |
| 228 | + * |
| 229 | + * @param pojo POJO Object with annotation {@link org.influxdb.annotation.Column} on fields |
| 230 | + * @return the Builder instance |
| 231 | + */ |
| 232 | + public Builder addFieldsFromPOJO(final Object pojo) { |
| 233 | + |
| 234 | + Class<? extends Object> clazz = pojo.getClass(); |
| 235 | + |
| 236 | + for (Field field : clazz.getDeclaredFields()) { |
| 237 | + |
| 238 | + Column column = field.getAnnotation(Column.class); |
| 239 | + |
| 240 | + if (column == null) { |
| 241 | + continue; |
| 242 | + } |
| 243 | + |
| 244 | + field.setAccessible(true); |
| 245 | + String fieldName = column.name(); |
| 246 | + addFieldByAttribute(pojo, field, column, fieldName); |
| 247 | + } |
| 248 | + |
| 249 | + if (this.fields.isEmpty()) { |
| 250 | + throw new BuilderException("Class " + pojo.getClass().getName() |
| 251 | + + " has no @" + Column.class.getSimpleName() + " annotation"); |
| 252 | + } |
| 253 | + |
| 254 | + return this; |
| 255 | + } |
| 256 | + |
| 257 | + private void addFieldByAttribute(final Object pojo, final Field field, final Column column, |
| 258 | + final String fieldName) { |
| 259 | + try { |
| 260 | + Object fieldValue = field.get(pojo); |
| 261 | + |
| 262 | + if (column.tag()) { |
| 263 | + this.tags.put(fieldName, (String) fieldValue); |
| 264 | + } else { |
| 265 | + this.fields.put(fieldName, fieldValue); |
| 266 | + } |
| 267 | + |
| 268 | + } catch (IllegalArgumentException | IllegalAccessException e) { |
| 269 | + // Can not happen since we use metadata got from the object |
| 270 | + throw new BuilderException( |
| 271 | + "Field " + fieldName + " could not found on class " + pojo.getClass().getSimpleName()); |
| 272 | + } |
| 273 | + } |
| 274 | + |
198 | 275 | /**
|
199 | 276 | * Create a new Point.
|
200 | 277 | *
|
@@ -254,6 +331,13 @@ void setPrecision(final TimeUnit precision) {
|
254 | 331 | this.precision = precision;
|
255 | 332 | }
|
256 | 333 |
|
| 334 | + /** |
| 335 | + * @return the fields |
| 336 | + */ |
| 337 | + Map<String, Object> getFields() { |
| 338 | + return this.fields; |
| 339 | + } |
| 340 | + |
257 | 341 | /**
|
258 | 342 | * @param fields
|
259 | 343 | * the fields to set
|
@@ -418,4 +502,8 @@ private void formatedTime(final StringBuilder sb, final TimeUnit precision) {
|
418 | 502 | }
|
419 | 503 | sb.append(" ").append(precision.convert(this.time, this.precision));
|
420 | 504 | }
|
| 505 | + |
| 506 | + private static String findMeasurementName(final Class<?> clazz) { |
| 507 | + return clazz.getAnnotation(Measurement.class).name(); |
| 508 | + } |
421 | 509 | }
|
0 commit comments