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

com.google.protobuf.ProtobufToStringOutput Maven / Gradle / Ivy

Go to download

Core Protocol Buffers library. Protocol Buffers are a way of encoding structured data in an efficient yet extensible format.

There is a newer version: 4.29.0-RC2
Show newest version
package com.google.protobuf;

/**
 * ProtobufToStringOutput controls the output format of {@link Message#toString()}. Specifically, for
 * the Runnable object passed to `callWithDebugFormat` and `callWithTextFormat`, Message.toString()
 * will always output the specified format unless ProtobufToStringOutput is used again to change the
 * output format.
 */
public final class ProtobufToStringOutput {
  private enum OutputMode {
    DEBUG_FORMAT,
    TEXT_FORMAT
  }

  private static final ThreadLocal outputMode =
      new ThreadLocal() {
        @Override
        protected OutputMode initialValue() {
          return OutputMode.TEXT_FORMAT;
        }
      };

  private ProtobufToStringOutput() {}

  @CanIgnoreReturnValue
  private static OutputMode setOutputMode(OutputMode newMode) {
    OutputMode oldMode = outputMode.get();
    outputMode.set(newMode);
    return oldMode;
  }

  private static void callWithSpecificFormat(Runnable impl, OutputMode mode) {
    OutputMode oldMode = setOutputMode(mode);
    try {
      impl.run();
    } finally {
      OutputMode unused = setOutputMode(oldMode);
    }
  }

  public static void callWithDebugFormat(Runnable impl) {
    callWithSpecificFormat(impl, OutputMode.DEBUG_FORMAT);
  }

  public static void callWithTextFormat(Runnable impl) {
    callWithSpecificFormat(impl, OutputMode.TEXT_FORMAT);
  }

  public static boolean shouldOutputDebugFormat() {
    return outputMode.get() == OutputMode.DEBUG_FORMAT;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy