Пример #1
0
 @Override
 public void encode(ReadableDuration value, OutputStream outStream, Context context)
     throws CoderException, IOException {
   if (value == null) {
     throw new CoderException("cannot encode a null ReadableDuration");
   }
   longCoder.encode(toLong(value), outStream, context);
 }
Пример #2
0
 @Override
 public void registerByteSizeObserver(
     ReadableDuration value, ElementByteSizeObserver observer, Context context) throws Exception {
   longCoder.registerByteSizeObserver(toLong(value), observer, context);
 }
Пример #3
0
 /**
  * {@inheritDoc}
  *
  * @return {@code true}, because it is cheap to ascertain the byte size of a long.
  */
 @Override
 public boolean isRegisterByteSizeObserverCheap(ReadableDuration value, Context context) {
   return longCoder.isRegisterByteSizeObserverCheap(toLong(value), context);
 }
Пример #4
0
 @Override
 public ReadableDuration decode(InputStream inStream, Context context)
     throws CoderException, IOException {
   return fromLong(longCoder.decode(inStream, context));
 }
Пример #5
0
/**
 * A {@link Coder} that encodes a joda {@link Duration} as a {@link Long} using the format of {@link
 * VarLongCoder}.
 */
public class DurationCoder extends AtomicCoder<ReadableDuration> {

  @JsonCreator
  public static DurationCoder of() {
    return INSTANCE;
  }

  /////////////////////////////////////////////////////////////////////////////

  private static final DurationCoder INSTANCE = new DurationCoder();

  private final VarLongCoder longCoder = VarLongCoder.of();

  private DurationCoder() {}

  private Long toLong(ReadableDuration value) {
    return value.getMillis();
  }

  private ReadableDuration fromLong(Long decoded) {
    return Duration.millis(decoded);
  }

  @Override
  public void encode(ReadableDuration value, OutputStream outStream, Context context)
      throws CoderException, IOException {
    if (value == null) {
      throw new CoderException("cannot encode a null ReadableDuration");
    }
    longCoder.encode(toLong(value), outStream, context);
  }

  @Override
  public ReadableDuration decode(InputStream inStream, Context context)
      throws CoderException, IOException {
    return fromLong(longCoder.decode(inStream, context));
  }

  /**
   * {@inheritDoc}
   *
   * @return {@code true}. This coder is injective.
   */
  @Override
  public boolean consistentWithEquals() {
    return true;
  }

  /**
   * {@inheritDoc}
   *
   * @return {@code true}, because it is cheap to ascertain the byte size of a long.
   */
  @Override
  public boolean isRegisterByteSizeObserverCheap(ReadableDuration value, Context context) {
    return longCoder.isRegisterByteSizeObserverCheap(toLong(value), context);
  }

  @Override
  public void registerByteSizeObserver(
      ReadableDuration value, ElementByteSizeObserver observer, Context context) throws Exception {
    longCoder.registerByteSizeObserver(toLong(value), observer, context);
  }
}