com.hubspot.singularity.data.transcoders.SingularityJsonTranscoderBinder Maven / Gradle / Ivy
package com.hubspot.singularity.data.transcoders;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.TypeParameter;
import com.google.common.reflect.TypeToken;
import com.google.inject.Binder;
import com.google.inject.Inject;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.Scopes;
import com.hubspot.singularity.SingularityId;
import com.hubspot.singularity.config.SingularityConfiguration;
@SuppressWarnings("serial")
public final class SingularityJsonTranscoderBinder {
public static SingularityJsonTranscoderBinder bindTranscoder(Binder binder) {
return new SingularityJsonTranscoderBinder(binder);
}
private final Binder binder;
public SingularityJsonTranscoderBinder(Binder binder) {
this.binder = binder;
}
public void asJson(Class clazz) {
TypeToken> typeToken = new TypeToken>() {}.where(new TypeParameter() {}, clazz);
@SuppressWarnings("unchecked")
Key> key = (Key>) Key.get(typeToken.getType());
binder.bind(key).toProvider(new JsonTranscoderProvider(clazz)).in(Scopes.SINGLETON);
}
public void asCompressedJson(Class clazz) {
TypeToken> typeToken = new TypeToken>() {}.where(new TypeParameter() {}, clazz);
@SuppressWarnings("unchecked")
Key> key = (Key>) Key.get(typeToken.getType());
binder.bind(key).toProvider(new CompressingJsonTranscoderProvider(clazz)).in(Scopes.SINGLETON);
}
public void asSingularityId(Class clazz) {
TypeToken> typeToken = new TypeToken>() {}.where(new TypeParameter() {}, clazz);
@SuppressWarnings("unchecked")
Key> key = (Key>) Key.get(typeToken.getType());
binder.bind(key).toInstance(new IdTranscoder(clazz));
}
static class JsonTranscoderProvider implements Provider> {
private final Class clazz;
private ObjectMapper objectMapper;
JsonTranscoderProvider(Class clazz) {
this.clazz = clazz;
}
@Inject
void inject(ObjectMapper objectMapper) {
this.objectMapper = checkNotNull(objectMapper, "objectMapper is null");
}
@Override
public JsonTranscoder get() {
checkState(objectMapper != null, "objectMapper was never injected!");
return new JsonTranscoder(objectMapper, clazz);
}
}
static class CompressingJsonTranscoderProvider implements Provider> {
private final Class clazz;
private ObjectMapper objectMapper;
private SingularityConfiguration singularityConfiguration;
CompressingJsonTranscoderProvider(Class clazz) {
this.clazz = clazz;
}
@Inject
void inject(ObjectMapper objectMapper, SingularityConfiguration singularityConfiguration) {
this.objectMapper = checkNotNull(objectMapper, "objectMapper is null");
this.singularityConfiguration = checkNotNull(singularityConfiguration, "singularityConfiguration is null");
}
@Override
public CompressingJsonTranscoder get() {
checkState(objectMapper != null, "objectMapper was never injected!");
checkState(singularityConfiguration != null, "singularityConfiguration was never injected!");
return new CompressingJsonTranscoder(singularityConfiguration, objectMapper, clazz);
}
}
}