Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for annotation for null values on pojo fields of type String. #693

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/org/influxdb/annotation/Default.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.influxdb.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.FIELD)
public @interface Default {
String value() default "";
long longValue() default 0L;
int intValue() default 0;
double doubleValue() default 0.0d;
float floatValue() default 0.0f;
boolean boolValue() default false;
}
22 changes: 21 additions & 1 deletion src/main/java/org/influxdb/dto/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.concurrent.TimeUnit;
import org.influxdb.BuilderException;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Default;
import org.influxdb.annotation.Measurement;
import org.influxdb.annotation.TimeColumn;
import org.influxdb.impl.Preconditions;
Expand Down Expand Up @@ -274,7 +275,6 @@ public Builder addFieldsFromPOJO(final Object pojo) {
if (column == null) {
continue;
}

field.setAccessible(true);
String fieldName = column.name();
addFieldByAttribute(pojo, field, column, fieldName);
Expand All @@ -293,6 +293,26 @@ public Builder addFieldsFromPOJO(final Object pojo) {
private void addFieldByAttribute(final Object pojo, final Field field, final Column column,
final String fieldName) {
try {
if (field.isAnnotationPresent(Default.class) && field.get(pojo) == null) {
Default val = field.getAnnotation(Default.class);
if (field.getType().equals(String.class)) {
field.set(pojo, val.value());
} else if (field.getType().equals(Long.class)) {
field.set(pojo, val.longValue());
} else if (field.getType().equals(Integer.class)) {
field.set(pojo, val.intValue());
} else if (field.getType().equals(BigDecimal.class)) {
field.set(pojo, BigDecimal.valueOf(0L));
} else if (field.getType().equals(BigInteger.class)) {
field.set(pojo, BigInteger.valueOf(0));
} else if (field.getType().equals(Boolean.class)) {
field.set(pojo, val.boolValue());
} else if (field.getType().equals(Float.class)) {
field.set(pojo, val.floatValue());
} else if (field.getType().equals(Double.class)) {
field.set(pojo, val.doubleValue());
}
}
Object fieldValue = field.get(pojo);

TimeColumn tc = field.getAnnotation(TimeColumn.class);
Expand Down
150 changes: 150 additions & 0 deletions src/test/java/org/influxdb/dto/PointTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.influxdb.BuilderException;
import org.influxdb.InfluxDB;
import org.influxdb.annotation.Column;
import org.influxdb.annotation.Default;
import org.influxdb.annotation.Measurement;
import org.influxdb.annotation.TimeColumn;
import org.influxdb.impl.InfluxDBImpl;
Expand Down Expand Up @@ -793,6 +794,50 @@ public void testAddFieldsFromPOJOWithPublicAttributes() {
Assertions.assertEquals(pojo.time, p.getFields().get("time"));
Assertions.assertEquals(pojo.uuid, p.getTags().get("uuid"));
}
@Test
public void testAddFieldsFromPOJOWithDefaultAnnotation() {
PojoWithDefaultAnnotation pojo = new PojoWithDefaultAnnotation();

Point p = Point.measurementByPOJO(pojo.getClass()).addFieldsFromPOJO(pojo).build();
Assertions.assertTrue(pojo.getISBN().equals(20L));
Assertions.assertTrue(pojo.booleanObject.equals(false));
Assertions.assertTrue(pojo.integerObject.equals(20));
Assertions.assertTrue(pojo.doubleObject.equals(20.0d));
Assertions.assertTrue(pojo.floatObject.equals(0.0f));
Assertions.assertTrue(pojo.bigDecimal.equals(BigDecimal.ZERO));
Assertions.assertTrue(pojo.bigInteger.equals(BigInteger.ZERO));

Assertions.assertTrue(p.getFields().get("author").equals(""));
Assertions.assertTrue(p.getFields().get("id").equals("2"));
Assertions.assertTrue(p.getFields().get("booleanObject").equals(false));
Assertions.assertTrue(p.getFields().get("doubleObject").equals(20.0d));
Assertions.assertTrue(p.getFields().get("floatObject").equals(0.0f));
Assertions.assertTrue(p.getFields().get("bigDecimal").equals(BigDecimal.ZERO));
Assertions.assertTrue(p.getFields().get("bigInteger").equals(BigInteger.ZERO));

//Soundness check
pojo.setId("41");
pojo.setAuthor("William Gibson");
pojo.setISBN(342134566545L);
pojo.setBigDecimal(BigDecimal.valueOf(12435125.435434));
pojo.setBooleanObject(true);
pojo.setBigInteger(new BigInteger(String.valueOf(54635265426256L)));
pojo.setDoubleObject(453.654d);
pojo.setFloatObject(5434.787f);
pojo.setIntegerObject(5346546);
p = Point.measurementByPOJO(pojo.getClass()).addFieldsFromPOJO(pojo).build();

Assertions.assertFalse(p.getFields().get("id").equals("2"));
Assertions.assertTrue(p.getFields().get("id").equals("41"));
Assertions.assertTrue(p.getFields().get("author").equals("William Gibson"));
Assertions.assertFalse(p.getFields().get("author").equals(""));
Assertions.assertFalse(p.getFields().get("booleanObject").equals(false));
Assertions.assertFalse(p.getFields().get("doubleObject").equals(20.0d));
Assertions.assertFalse(p.getFields().get("floatObject").equals(0.0f));
Assertions.assertFalse(p.getFields().get("bigDecimal").equals(BigDecimal.ZERO));
Assertions.assertFalse(p.getFields().get("bigInteger").equals(BigInteger.ZERO));

}

@Test
public void testInheritMeasurement() {
Expand Down Expand Up @@ -822,6 +867,111 @@ public void setId(String id) {
this.id = id;
}
}
@Measurement(name = "mymeasurement")
static class PojoWithDefaultAnnotation {

@Default("2")
@Column(name = "id")
private String id;
//check alternate order of annotations. ¯\_(ツ)_/¯
@Column(name = "author")
@Default
private String author;

@Default(doubleValue = 20.0d)
@Column(name = "doubleObject")
private Double doubleObject;

@Default(intValue = 20)
@Column(name = "integerObject")
private Integer integerObject;

@Default
@Column(name = "booleanObject")
private Boolean booleanObject;
@Default
@Column(name = "floatObject")
private Float floatObject;

@Default
@Column(name = "bigDecimal")
private BigDecimal bigDecimal;

@Default
@Column(name = "bigInteger")
private BigInteger bigInteger;

@Default(longValue = 20L)
@Column(name = "ISBN")
private Long ISBN;

public Double getDoubleObject() {
return doubleObject;
}

public Integer getIntegerObject() {
return integerObject;
}

public Boolean getBooleanObject() {
return booleanObject;
}

public Float getFloatObject() {
return floatObject;
}

public BigDecimal getBigDecimal() {
return bigDecimal;
}

public BigInteger getBigInteger() {
return bigInteger;
}

public void setDoubleObject(Double doubleObject) {
this.doubleObject = doubleObject;
}

public void setIntegerObject(Integer integerObject) {
this.integerObject = integerObject;
}

public void setBooleanObject(Boolean booleanObject) {
this.booleanObject = booleanObject;
}

public void setFloatObject(Float floatObject) {
this.floatObject = floatObject;
}

public void setBigDecimal(BigDecimal bigDecimal) {
this.bigDecimal = bigDecimal;
}

public void setBigInteger(BigInteger bigInteger) {
this.bigInteger = bigInteger;
}

public String getAuthor() {
return author;
}
public void setAuthor(String name) {
this.author = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Long getISBN() {
return ISBN;
}
public void setISBN(Long ISBN) {
this.ISBN = ISBN;
}
}

@Measurement(name = "mymeasurement")
static class PojoWithMeasurement {
Expand Down