Skip to content

Commit 2329ee3

Browse files
committed
Merge remote-tracking branch 'origin/57-null-to-empty-string-converter' into dev
2 parents e22dfe3 + d3794ae commit 2329ee3

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.objectbox.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* Defines the Java code of the default value to use for a property, when getting an existing entity and the database
10+
* value for the property is null.
11+
* <p>
12+
* Currently only {@code @DefaultValue("")} is supported.
13+
*/
14+
@Retention(RetentionPolicy.CLASS)
15+
@Target({ElementType.FIELD})
16+
public @interface DefaultValue {
17+
String value();
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.objectbox.converter;
2+
3+
import javax.annotation.Nullable;
4+
5+
/**
6+
* Used as a converter if a property is annotated with {@link io.objectbox.annotation.DefaultValue @DefaultValue("")}.
7+
*/
8+
public class NullToEmptyStringConverter implements PropertyConverter<String, String> {
9+
10+
@Override
11+
public String convertToDatabaseValue(String entityProperty) {
12+
return entityProperty;
13+
}
14+
15+
@Override
16+
public String convertToEntityProperty(@Nullable String databaseValue) {
17+
if (databaseValue == null) {
18+
return "";
19+
}
20+
return databaseValue;
21+
}
22+
}

0 commit comments

Comments
 (0)