/
TypeCodec.java
52 lines (43 loc) · 1.47 KB
/
TypeCodec.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package de.bild.codec;
import de.bild.codec.annotations.DecodingPojoFailureStrategy;
import org.bson.BsonNull;
import org.bson.BsonValue;
import org.bson.codecs.Codec;
import org.bson.conversions.Bson;
import org.slf4j.LoggerFactory;
/**
* All codecs used within polymorphia need to implement this interface.
*
*
* @param <T> the value type
*/
public interface TypeCodec<T> extends Codec<T> {
/**
* Override this method if your Codec needs to supply default values as replacements for null values.
* @return null or a default value
*/
default T defaultInstance() {
return null;
}
default boolean isCollectible() {
return false;
}
default T generateIdIfAbsentFromDocument(T document) {
LoggerFactory.getLogger(TypeCodec.class).warn("generateIdIfAbsentFromDocument() should be overridden if used!");
return document;
}
default boolean documentHasId(T document) {
LoggerFactory.getLogger(TypeCodec.class).warn("documentHasId() should be overridden if used!");
return true;
}
default BsonValue getDocumentId(T document) {
LoggerFactory.getLogger(TypeCodec.class).warn("getDocumentId() should be overridden if used!");
return BsonNull.VALUE;
}
default Bson getTypeFilter() {
return null;
}
default DecodingPojoFailureStrategy.Strategy getDecodingPojoFailureStrategy() {
return DecodingPojoFailureStrategy.Strategy.RETHROW_EXCEPTION;
}
}