Skip to content

Commit

Permalink
Refactoring of utility
Browse files Browse the repository at this point in the history
  • Loading branch information
rjhancock committed Jul 23, 2024
1 parent 1b2334f commit 76fdc5c
Showing 1 changed file with 69 additions and 61 deletions.
130 changes: 69 additions & 61 deletions megamek/src/megamek/utilities/NameChangesValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<String> loadFile(File fileName) {
String message = String.format("Collecting lines from file %s", fileName);
logger.info(message);

List<String> 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<String> 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);
}

Expand All @@ -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<String> 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);
}
}

Expand All @@ -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<String> 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);
}
Expand Down

0 comments on commit 76fdc5c

Please sign in to comment.