EntityCodec implements CollectibleCodec<T> { @Override public BsonValue getDocumentId(T document) { Object id = idHandler.get(document); if (id instanceof ObjectId) return new BsonObjectId((ObjectId) id); if (id instanceof String) new BsonString((String) id); throw new Error("unsupported id type, id=" + id); } EntityCodec(Class<T> entityClass, EntityIdHandler<T> idHandler, EntityEncoder<T> encoder, EntityDecoder<T> decoder); @Override T generateIdIfAbsentFromDocument(T document); @Override boolean documentHasId(T document); @Override BsonValue getDocumentId(T document); @Override void encode(BsonWriter writer, T value, EncoderContext encoderContext); @Override T decode(BsonReader reader, DecoderContext decoderContext); @Override Class<T> getEncoderClass();  }
@Test void getDocumentId() { TestEntity entity = new TestEntity(); entity.id = new ObjectId(); assertEquals(new BsonObjectId(entity.id), entityCodec.getDocumentId(entity)); }