Class LazyString


  • @ThreadSafe
    public final class LazyString
    extends java.lang.Object
    Delays String generation until toString() method is called. This class is used to support lazy text generation in org.slf4j.Logger prior to version 2. (Note: slf4j does support lambda in version 2). Consider the following example:
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    private static final Logger sLogger = LoggerFactory.getLogger(MyClass.class);
    
    final byte[] data = new byte[50_000];
    // Initialize data byte array.
    
    sLogger.debug("data contents:\n{}", new LazyString(() -> HexDump.dump(data, "  ");
    Author:
    Charles W. Rapp
    • Constructor Summary

      Constructors 
      Constructor Description
      LazyString​(java.util.function.Supplier<java.lang.String> initializer)
      Creates a new, un-initialized lazy string instance using the given String supplier to generate the text when needed.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String get()
      Returns initialized text.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • LazyString

        public LazyString​(java.util.function.Supplier<java.lang.String> initializer)
        Creates a new, un-initialized lazy string instance using the given String supplier to generate the text when needed.
        Parameters:
        initializer - generates text on demand.
    • Method Detail

      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • get

        @Nullable
        public java.lang.String get()
        Returns initialized text. If text has not been initialized yet then performs the initialization now.
        Returns:
        initialized text.