From 76fdc5cd5b2738eb75010b13585ebb2f0ca676bc Mon Sep 17 00:00:00 2001 From: Richard J Hancock Date: Mon, 22 Jul 2024 22:00:04 -0500 Subject: [PATCH] Refactoring of utility --- .../utilities/NameChangesValidator.java | 130 ++++++++++-------- 1 file changed, 69 insertions(+), 61 deletions(-) diff --git a/megamek/src/megamek/utilities/NameChangesValidator.java b/megamek/src/megamek/utilities/NameChangesValidator.java index 0908500db95..d90abb03861 100644 --- a/megamek/src/megamek/utilities/NameChangesValidator.java +++ b/megamek/src/megamek/utilities/NameChangesValidator.java @@ -21,8 +21,12 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; import megamek.common.Configuration; import megamek.common.MechSummaryCache; @@ -47,7 +51,6 @@ public class NameChangesValidator { private static final MMLogger logger = MMLogger.create(NameChangesValidator.class); private static final String STRING_FINISHED = "Finished."; - private static final String STRING_EXCEPTION = "Exception"; private MechSummaryCache mechSummaryCache = null; private int errors; @@ -62,33 +65,57 @@ public static void main(String... args) { validator.testRightSide(); } - private void testEqualSides() { - // Find equal left and right sides - logger.info("Looking for equal left and right sides..."); - try (FileInputStream fis = new FileInputStream(lookupNames); - InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8); - BufferedReader br = new BufferedReader(isr)) { - String line; - while (null != (line = br.readLine())) { + private List loadFile(File fileName) { + String message = String.format("Collecting lines from file %s", fileName); + logger.info(message); + + List lines = new ArrayList<>(); + + try { + FileInputStream fis = new FileInputStream(fileName); + InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8); + BufferedReader br = new BufferedReader(isr); + + String line = ""; + + while ((line = br.readLine()) != null) { if (line.startsWith("#")) { continue; } - int index = line.indexOf('|'); - if (index > 0) { - String lookupName = line.substring(0, index); - String entryName = line.substring(index + 1); - if (lookupName.equals(entryName)) { - String message = String.format("Equal lookup name and cache entry in line: %s", line); - logger.info(message); - errors++; - } - } + + lines.add(line); } - } catch (Exception ex) { - logger.error(ex, STRING_EXCEPTION); + + br.close(); + } catch (FileNotFoundException exception) { + logger.error(exception, "File was not found"); + System.exit(64); + } catch (IOException exception) { + logger.error(exception, "IO Exception"); System.exit(64); } + return lines; + } + + private void testEqualSides() { + // Find equal left and right sides + logger.info("Looking for equal left and right sides..."); + List lines = loadFile(lookupNames); + for (String line : lines) { + int index = line.indexOf('|'); + + if (index > 0) { + String lookupName = line.substring(0, index); + String entryName = line.substring(index + 1); + if (lookupName.equals(entryName)) { + String message = String.format("Equal lookup name and cache entry in line: %s", line); + logger.info(message); + errors++; + } + } + } + logger.info(STRING_FINISHED); } @@ -104,27 +131,17 @@ private void testLeftSide() { mechSummaryCache.getAllMechs(); logger.info("Rename successful. Testing lookup names..."); - try (FileInputStream fis = new FileInputStream(lookupNamesHidden); - InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8); - BufferedReader br = new BufferedReader(isr)) { - String line; - while (null != (line = br.readLine())) { - if (line.startsWith("#")) { - continue; - } - int index = line.indexOf('|'); - if (index > 0) { - String lookupName = line.substring(0, index); - if (mechSummaryCache.getMech(lookupName) != null) { - message = String.format("Lookup name (left side) is an existing unit in line: %s", line); - logger.info(message); - errors++; - } + List lines = loadFile(lookupNamesHidden); + for (String line : lines) { + int index = line.indexOf('|'); + if (index > 0) { + String lookupName = line.substring(0, index); + if (mechSummaryCache.getMech(lookupName) != null) { + message = String.format("Lookup name (left side) is an existing unit in line: %s", line); + logger.info(message); + errors++; } } - } catch (Exception ex) { - logger.error(ex, STRING_EXCEPTION); - System.exit(64); } } @@ -146,34 +163,25 @@ private void testRightSide() { logger.info("Reloading Unit Cache..."); mechSummaryCache.loadMechData(true); mechSummaryCache.getAllMechs(); - try (FileInputStream fis = new FileInputStream(lookupNames); - InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8); - BufferedReader br = new BufferedReader(isr)) { - String line; - - while (null != (line = br.readLine())) { - if (line.startsWith("#")) { - continue; - } - int index = line.indexOf('|'); - if (index > 0) { - String entryName = line.substring(index + 1); - if (mechSummaryCache.getMech(entryName) == null) { - String message = String.format("Actual name (right side) not found in line: %s", line); - logger.info(message); - errors++; - } + List lines = loadFile(lookupNames); + for (String line : lines) { + int index = line.indexOf('|'); + if (index > 0) { + String entryName = line.substring(index + 1); + if (mechSummaryCache.getMech(entryName) == null) { + String message = String.format("Actual name (right side) not found in line: %s", line); + logger.error(message); + errors++; } } - } catch (Exception ex) { - logger.error(ex, STRING_EXCEPTION); - System.exit(64); + } } else { String message = String.format("Cannot find the name-changes file %s", MechSummaryCache.FILENAME_LOOKUP); - logger.info(message); + logger.error(message); System.exit(64); } + logger.info(STRING_FINISHED); System.exit(errors > 0 ? 1 : 0); }