com.meltmedia.dropwizard.jongo.JongoBuilder Maven / Gradle / Ivy
The newest version!
package com.meltmedia.dropwizard.jongo;
import java.util.function.Consumer;
import java.util.function.Supplier;
import org.jongo.Jongo;
import org.jongo.marshall.jackson.JacksonMapper;
import org.jongo.marshall.jackson.configuration.MapperModifier;
import com.meltmedia.dropwizard.jongo.JongoBuilder;
import com.mongodb.MongoClient;
public class JongoBuilder {
Supplier client = Functions.supplierRequired("client");
Supplier databaseName = Functions.supplierRequired("databaseName");
Consumer mapperOps = m->{};
JacksonMapper.Builder builder = new JacksonMapper.Builder();
public JongoBuilder withClient( Supplier client ) {
this.client = client;
return this;
}
public JongoBuilder withDatabaseName( Supplier databaseName ) {
this.databaseName = databaseName;
return this;
}
public JongoBuilder addMapperOps( Consumer mapperOp ) {
this.mapperOps = mapperOps.andThen(mapperOp);
return this;
}
public JongoBuilder addMapperModifier( MapperModifier m ) {
this.mapperOps = mapperOps.andThen(b->b.addModifier(m));
return this;
}
public JongoBuilder addMixin( Class> target, Class> mixin ) {
addMapperModifier(m->m.addMixIn(target, mixin));
return this;
}
@SuppressWarnings("deprecation")
public Jongo build() {
String resolvedName = databaseName.get();
JacksonMapper.Builder builder = new JacksonMapper.Builder();
mapperOps.accept(builder);
return new Jongo(
client.get().getDB(resolvedName),
builder.build());
}
}