Created
January 8, 2019 15:04
-
-
Save gilgamez/63429f4af4a9caccc83badb881fac9a8 to your computer and use it in GitHub Desktop.
wrapper method that evaluates toString dynamically, useful in performance sensitive logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!java --source 11 | |
import java.util.function.Supplier; | |
import java.util.List; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.util.stream.Stream; | |
public class LogWithSuplier { | |
public static void main(String... args) throws IOException { | |
for (var arg : args) { | |
List<String> lines = Files.readAllLines(new File(arg).toPath()); | |
System.out.printf("Lines read: %s", supplied(() -> lines.size())).println(); | |
} | |
} | |
private static Object supplied(Supplier s) { | |
return new Object() { | |
@Override | |
public String toString() { | |
return s.get().toString(); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment