Minor code cleanup

This commit is contained in:
Erik C. Thauvin 2024-06-23 10:47:18 -07:00
parent 3db6ca026d
commit bd27eac071
Signed by: erik
GPG key ID: 776702A6A2DA330E
3 changed files with 22 additions and 32 deletions

View file

@ -31,11 +31,11 @@ import java.util.Properties;
* @since 1.0 * @since 1.0
*/ */
public class PropertyFileOperation extends AbstractOperation<PropertyFileOperation> { public class PropertyFileOperation extends AbstractOperation<PropertyFileOperation> {
private final List<EntryBase> entries = new ArrayList<>(); private final List<EntryBase> entries_ = new ArrayList<>();
private String comment = ""; private String comment_ = "";
private boolean failOnWarning; private boolean failOnWarning_;
private File file; private File file_;
private BaseProject project; private BaseProject project_;
/** /**
* Sets the comment to be inserted at the top of the {@link java.util.Properties} file. * Sets the comment to be inserted at the top of the {@link java.util.Properties} file.
@ -43,9 +43,8 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* @param comment the header comment * @param comment the header comment
* @return this instance * @return this instance
*/ */
@SuppressWarnings("unused")
public PropertyFileOperation comment(String comment) { public PropertyFileOperation comment(String comment) {
this.comment = comment; comment_ = comment;
return this; return this;
} }
@ -58,7 +57,7 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
public PropertyFileOperation entry(EntryBase entry) { public PropertyFileOperation entry(EntryBase entry) {
entries.add(entry); entries_.add(entry);
return this; return this;
} }
@ -67,18 +66,18 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
*/ */
@Override @Override
public void execute() throws Exception { public void execute() throws Exception {
var commandName = project.getCurrentCommandName(); var commandName = project_.getCurrentCommandName();
var properties = new Properties(); var properties = new Properties();
var success = false; var success = false;
if (file == null) { if (file_ == null) {
PropertyFileUtils.warn(commandName, "A properties file must be specified."); PropertyFileUtils.warn(commandName, "A properties file must be specified.");
} else { } else {
success = PropertyFileUtils.loadProperties(commandName, file, properties); success = PropertyFileUtils.loadProperties(commandName, file_, properties);
} }
if (success) { if (success) {
for (var entry : entries) { for (var entry : entries_) {
if (entry.getKey().isBlank()) { if (entry.getKey().isBlank()) {
PropertyFileUtils.warn(commandName, "An entry key must specified."); PropertyFileUtils.warn(commandName, "An entry key must specified.");
} else { } else {
@ -94,9 +93,9 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
PropertyFileUtils.warn(commandName, "An entry must be set or have a default value: " + key); PropertyFileUtils.warn(commandName, "An entry must be set or have a default value: " + key);
} else { } else {
if (entry instanceof EntryDate) { if (entry instanceof EntryDate) {
success = PropertyFileUtils.processDate(commandName, properties, (EntryDate) entry, failOnWarning); success = PropertyFileUtils.processDate(commandName, properties, (EntryDate) entry, failOnWarning_);
} else if (entry instanceof EntryInt) { } else if (entry instanceof EntryInt) {
success = PropertyFileUtils.processInt(commandName, properties, (EntryInt) entry, failOnWarning); success = PropertyFileUtils.processInt(commandName, properties, (EntryInt) entry, failOnWarning_);
} else { } else {
success = PropertyFileUtils.processString(properties, (Entry) entry); success = PropertyFileUtils.processString(properties, (Entry) entry);
} }
@ -106,7 +105,7 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
} }
if (success) { if (success) {
PropertyFileUtils.saveProperties(file, comment, properties); PropertyFileUtils.saveProperties(file_, comment_, properties);
} }
} }
@ -116,9 +115,8 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* @param failOnWarning if set to {@code true}, the execution will fail on any warnings. * @param failOnWarning if set to {@code true}, the execution will fail on any warnings.
* @return this instance * @return this instance
*/ */
@SuppressWarnings("unused")
public PropertyFileOperation failOnWarning(boolean failOnWarning) { public PropertyFileOperation failOnWarning(boolean failOnWarning) {
this.failOnWarning = failOnWarning; failOnWarning_ = failOnWarning;
return this; return this;
} }
@ -128,9 +126,8 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* @param file the file to be edited * @param file the file to be edited
* @return this instance * @return this instance
*/ */
@SuppressWarnings("unused")
public PropertyFileOperation file(File file) { public PropertyFileOperation file(File file) {
this.file = file; file_ = file;
return this; return this;
} }
@ -140,9 +137,8 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* @param file the file to be edited * @param file the file to be edited
* @return this instance * @return this instance
*/ */
@SuppressWarnings("unused")
public PropertyFileOperation file(String file) { public PropertyFileOperation file(String file) {
this.file = new File(file); file_ = new File(file);
return this; return this;
} }
@ -153,7 +149,7 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* @return this instance * @return this instance
*/ */
public PropertyFileOperation fromProject(BaseProject project) { public PropertyFileOperation fromProject(BaseProject project) {
this.project = project; project_ = project;
return this; return this;
} }
} }

View file

@ -104,12 +104,10 @@ public final class PropertyFileUtils {
public static boolean processDate(String command, Properties p, EntryDate entry, boolean failOnWarning) public static boolean processDate(String command, Properties p, EntryDate entry, boolean failOnWarning)
throws Exception { throws Exception {
var success = true; var success = true;
var value = currentValue(null, entry.getDefaultValue(), var value = currentValue(null, entry.getDefaultValue(), entry.getNewValue());
entry.getNewValue());
var pattern = entry.getPattern(); var pattern = entry.getPattern();
String parsedValue = String.valueOf(value); var parsedValue = String.valueOf(value);
if (pattern != null && !pattern.isBlank()) { if (pattern != null && !pattern.isBlank()) {
var offset = 0; var offset = 0;
@ -239,6 +237,7 @@ public final class PropertyFileUtils {
* @param entry the {@link Entry} containing the {@link Properties property} edits * @param entry the {@link Entry} containing the {@link Properties property} edits
* @return the boolean * @return the boolean
*/ */
@SuppressWarnings("SameReturnValue")
public static boolean processString(Properties p, Entry entry) { public static boolean processString(Properties p, Entry entry) {
var value = currentValue(p.getProperty(entry.getKey()), entry.getDefaultValue(), entry.getNewValue()); var value = currentValue(p.getProperty(entry.getKey()), entry.getDefaultValue(), entry.getNewValue());
@ -290,6 +289,7 @@ public final class PropertyFileUtils {
*/ */
@SuppressWarnings({"PMD.SignatureDeclareThrowsException"}) @SuppressWarnings({"PMD.SignatureDeclareThrowsException"})
static void warn(String command, String message, Exception e, boolean failOnWarning) throws Exception { static void warn(String command, String message, Exception e, boolean failOnWarning) throws Exception {
LOGGER.warning("ahah");
if (failOnWarning) { if (failOnWarning) {
LOGGER.log(Level.SEVERE, '[' + command + "] " + message, e); LOGGER.log(Level.SEVERE, '[' + command + "] " + message, e);
throw e; throw e;

View file

@ -58,7 +58,6 @@ class PropertyFileUtilsTest {
} }
@Test @Test
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
void parseDateSub() throws Exception { void parseDateSub() throws Exception {
var entryDate = newEntryDate(); var entryDate = newEntryDate();
entryDate.setCalc(SUB); entryDate.setCalc(SUB);
@ -84,7 +83,6 @@ class PropertyFileUtilsTest {
} }
@Test @Test
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
void parseIntSubTest() throws Exception { void parseIntSubTest() throws Exception {
var entryInt = newEntryInt(); var entryInt = newEntryInt();
entryInt.calc(SUB); entryInt.calc(SUB);
@ -148,7 +146,6 @@ class PropertyFileUtilsTest {
} }
@Test @Test
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
void parseTimeTest() throws Exception { void parseTimeTest() throws Exception {
var entry = new EntryDate("time").pattern("m"); var entry = new EntryDate("time").pattern("m");
var time = LocalTime.now(); var time = LocalTime.now();
@ -165,7 +162,6 @@ class PropertyFileUtilsTest {
} }
@Test @Test
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
void processDateAddTest() throws Exception { void processDateAddTest() throws Exception {
var entryDate = newEntryDate(); var entryDate = newEntryDate();
entryDate.setCalc(ADD); entryDate.setCalc(ADD);
@ -189,7 +185,6 @@ class PropertyFileUtilsTest {
} }
@Test @Test
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
void processIntAddTest() throws Exception { void processIntAddTest() throws Exception {
var entryInt = newEntryInt(); var entryInt = newEntryInt();
entryInt.calc(ADD); entryInt.calc(ADD);
@ -233,7 +228,6 @@ class PropertyFileUtilsTest {
} }
@Test @Test
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
void savePropertiesTest() throws Exception { void savePropertiesTest() throws Exception {
var p = new Properties(); var p = new Properties();
var test = "test"; var test = "test";