Using Java SDK v2.7
I have an enum that hold an internal value - a Number.
When the enum is written into cb it is converted to its value. using a cb Converter
. This works fine.
On read however, the sdk uses org.springframework.core.convert.support.IntegerToEnumConverterFactory
instead of my customer converter and throws :
java.lang.ArrayIndexOutOfBoundsException: Index 50 out of bounds for length 3
In this exemple the value stored is an int 50.
Is there a way to give some precedence to my converter or to specifically set the converter to use ?
Converter declaration
@Bean(name = BeanNames.COUCHBASE_CUSTOM_CONVERSIONS)
public CouchbaseCustomConversions customConversions() {
List<Converter<?, ?>> converters = new ArrayList<>();
converters.add(TaskPriorityToNumberConverter.INSTANCE);
converters.add(NumberToTaskPriorityConverter.INSTANCE);
return new CouchbaseCustomConversions(converters);
}
@WritingConverter
public enum TaskPriorityToNumberConverter implements Converter<TaskPriority, Number> {
INSTANCE;
@Override
public Number convert(TaskPriority source) {
return source == null ? null : source.getOrder();
}
}
/**
* Simple singleton to convert from {@link Number} {@link BigDecimal} representation.
*/
@ReadingConverter
public enum NumberToTaskPriorityConverter implements Converter<Number, TaskPriority> {
INSTANCE;
@Override
public TaskPriority convert(Number source) {
return source == null ? null : TaskPriority.forValue(source);
}
}
TaskPriority
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.base.CaseFormat;
import java.util.stream.Stream;
public enum TaskPriority implements OrderedEnum {
High(75L),
Medium(50L),
Low(25L)
;
private final long order;
TaskPriority(long order) {
this.order = order;
}
public long getOrder() {
return order;
}
@JsonCreator
public static TaskPriority forValue(String value) {
return valueOf(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, value));
}
public static TaskPriority forValue(Number order) {
return Stream.of(TaskPriority.values())
.filter(c -> order.equals(c.getOrder()))
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
@JsonValue
public String toValue() {
return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, name());
}
}