From 3613bacea8042aaf59dc9b0bbf68dcc11a020b61 Mon Sep 17 00:00:00 2001 From: Dominic Burger Date: Tue, 19 Nov 2024 08:37:09 +0100 Subject: [PATCH 1/2] Log summary of mapped objects and duration --- .../java/ch/geowerkstatt/lk2dxf/Main.java | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/geowerkstatt/lk2dxf/Main.java b/src/main/java/ch/geowerkstatt/lk2dxf/Main.java index ff48026..861e381 100644 --- a/src/main/java/ch/geowerkstatt/lk2dxf/Main.java +++ b/src/main/java/ch/geowerkstatt/lk2dxf/Main.java @@ -20,8 +20,13 @@ import org.apache.logging.log4j.core.layout.PatternLayout; import java.io.File; +import java.time.Duration; +import java.time.Instant; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Stream; public final class Main { @@ -46,6 +51,7 @@ private Main() { } * Application entry point. */ public static void main(String[] args) { + Instant start = Instant.now(); Options cliOptions = createCliOptions(); CommandLine commandLine = parseCommandLine(cliOptions, args); @@ -65,6 +71,11 @@ public static void main(String[] args) { if (!processFiles(options.get())) { System.exit(1); } + + Instant end = Instant.now(); + Duration duration = Duration.between(start, end); + String formattedDuration = String.format("%02dh:%02dm:%02ds.%03dms", duration.toHours(), duration.toMinutesPart(), duration.toSecondsPart(), duration.toMillisPart()); + LOGGER.info("Processing took {}", formattedDuration); } } } @@ -76,6 +87,8 @@ public static void main(String[] args) { */ private static boolean processFiles(LK2DxfOptions options) { Optional perimeter = options.parsePerimeter(); + AtomicInteger objectCounter = new AtomicInteger(); + Map layerCounters = new HashMap<>(); ObjectMapper objectMapper; try { @@ -93,7 +106,12 @@ private static boolean processFiles(LK2DxfOptions options) { objects = objects.filter(o -> perimeter.get().intersects(o.geometry())); } - objects.forEach(o -> o.writeToDxf(dxfWriter)); + objects.forEach(o -> { + o.writeToDxf(dxfWriter); + + objectCounter.incrementAndGet(); + incrementLayerCounter(layerCounters, o.layerMapping().layer()); + }); } catch (Exception e) { LOGGER.error("Failed to process file: {}", xtfFile, e); return false; @@ -104,9 +122,24 @@ private static boolean processFiles(LK2DxfOptions options) { return false; } + LOGGER.info("The output DXF file contains {} mapped objects", objectCounter.get()); + layerCounters.entrySet() + .stream() + .sorted(Map.Entry.comparingByKey()) + .forEach(entry -> LOGGER.info("Layer {}: {} objects", entry.getKey(), entry.getValue().get())); + return true; } + private static void incrementLayerCounter(Map layerCounters, String layer) { + AtomicInteger counter = layerCounters.get(layer); + if (counter == null) { + layerCounters.put(layer, new AtomicInteger(1)); + } else { + counter.incrementAndGet(); + } + } + private static void configureLogging(LK2DxfOptions lk2DxfOptions) { Level logLevel = lk2DxfOptions.trace() ? Level.TRACE : Level.INFO; Configurator.setRootLevel(logLevel); From b614d8e54c1d53c007a57a7e549c18b099c15801 Mon Sep 17 00:00:00 2001 From: Dominic Burger Date: Thu, 21 Nov 2024 08:30:47 +0100 Subject: [PATCH 2/2] Simplify object counter per layer --- src/main/java/ch/geowerkstatt/lk2dxf/Main.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/main/java/ch/geowerkstatt/lk2dxf/Main.java b/src/main/java/ch/geowerkstatt/lk2dxf/Main.java index 861e381..481334c 100644 --- a/src/main/java/ch/geowerkstatt/lk2dxf/Main.java +++ b/src/main/java/ch/geowerkstatt/lk2dxf/Main.java @@ -110,7 +110,8 @@ private static boolean processFiles(LK2DxfOptions options) { o.writeToDxf(dxfWriter); objectCounter.incrementAndGet(); - incrementLayerCounter(layerCounters, o.layerMapping().layer()); + String layer = o.layerMapping().layer(); + layerCounters.computeIfAbsent(layer, k -> new AtomicInteger()).incrementAndGet(); }); } catch (Exception e) { LOGGER.error("Failed to process file: {}", xtfFile, e); @@ -131,15 +132,6 @@ private static boolean processFiles(LK2DxfOptions options) { return true; } - private static void incrementLayerCounter(Map layerCounters, String layer) { - AtomicInteger counter = layerCounters.get(layer); - if (counter == null) { - layerCounters.put(layer, new AtomicInteger(1)); - } else { - counter.incrementAndGet(); - } - } - private static void configureLogging(LK2DxfOptions lk2DxfOptions) { Level logLevel = lk2DxfOptions.trace() ? Level.TRACE : Level.INFO; Configurator.setRootLevel(logLevel);