Skip to content

Instantly share code, notes, and snippets.

@gilgamez
Created January 8, 2019 15:04
Show Gist options
  • Save gilgamez/63429f4af4a9caccc83badb881fac9a8 to your computer and use it in GitHub Desktop.
Save gilgamez/63429f4af4a9caccc83badb881fac9a8 to your computer and use it in GitHub Desktop.
wrapper method that evaluates toString dynamically, useful in performance sensitive logging
#!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