All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hubspot.singularity.data.transcoders.IdTranscoder 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 static java.lang.reflect.Modifier.isStatic;

import com.hubspot.singularity.SingularityId;
import java.lang.reflect.Method;
import javax.annotation.Nullable;

public class IdTranscoder implements Transcoder {
  private static final byte[] EMPTY_BYTES = new byte[0];

  private final Method valueOfMethod;

  IdTranscoder(final Class clazz) {
    checkNotNull(clazz, "clazz is null");
    try {
      // SingularityId classes must have a static "valueOf" method for object construction.
      this.valueOfMethod = clazz.getMethod("valueOf", String.class);
    } catch (ReflectiveOperationException e) {
      throw new IllegalStateException(e);
    }
    checkState(
      isStatic(valueOfMethod.getModifiers()),
      "found valueOf method in %s but it is not static!",
      clazz.getSimpleName()
    );
  }

  @Override
  public T fromBytes(@Nullable byte[] data) throws SingularityTranscoderException {
    return fromString(StringTranscoder.INSTANCE.fromBytes(data));
  }

  @SuppressWarnings("unchecked")
  public T fromString(@Nullable String id) throws SingularityTranscoderException {
    if (id == null) {
      return null;
    }

    try {
      return (T) valueOfMethod.invoke(null, id);
    } catch (ReflectiveOperationException e) {
      throw new SingularityTranscoderException(e);
    }
  }

  @Override
  public byte[] toBytes(@Nullable T object) throws SingularityTranscoderException {
    return object == null
      ? EMPTY_BYTES
      : StringTranscoder.INSTANCE.toBytes(object.getId());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy