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

com.hubspot.singularity.data.transcoders.Transcoders Maven / Gradle / Ivy

package com.hubspot.singularity.data.transcoders;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.base.Function;
import javax.annotation.Nullable;

public final class Transcoders {

  private Transcoders() {
    throw new AssertionError("do not instantiate");
  }

  public static  Function getToBytesFunction(
    final Transcoder transcoder
  ) {
    checkNotNull(transcoder, "transcoder is null");

    return new Function() {

      @Override
      public byte[] apply(@Nullable T value) {
        try {
          return transcoder.toBytes(value);
        } catch (Throwable e) {
          throw new RuntimeException(e);
        }
      }
    };
  }

  public static  Function getFromBytesFunction(
    final Transcoder transcoder
  ) {
    checkNotNull(transcoder, "transcoder is null");

    return new Function() {

      @Override
      public T apply(@Nullable byte[] value) {
        try {
          return transcoder.fromBytes(value);
        } catch (Throwable e) {
          throw new RuntimeException(e);
        }
      }
    };
  }

  public static  Function getFromStringFunction(
    final Transcoder transcoder
  ) {
    checkNotNull(transcoder, "transcoder is null");

    return new Function() {

      @Override
      public T apply(@Nullable String value) {
        if (value == null) {
          return null;
        }
        try {
          return transcoder.fromBytes(value.getBytes(UTF_8));
        } catch (Throwable e) {
          throw new RuntimeException(e);
        }
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy