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
*/
public class PropertyFileOperation extends AbstractOperation<PropertyFileOperation> {
private final List<EntryBase> entries = new ArrayList<>();
private String comment = "";
private boolean failOnWarning;
private File file;
private BaseProject project;
private final List<EntryBase> entries_ = new ArrayList<>();
private String comment_ = "";
private boolean failOnWarning_;
private File file_;
private BaseProject project_;
/**
* 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
* @return this instance
*/
@SuppressWarnings("unused")
public PropertyFileOperation comment(String comment) {
this.comment = comment;
comment_ = comment;
return this;
}
@ -58,7 +57,7 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
*/
@SuppressWarnings("unused")
public PropertyFileOperation entry(EntryBase entry) {
entries.add(entry);
entries_.add(entry);
return this;
}
@ -67,18 +66,18 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
*/
@Override
public void execute() throws Exception {
var commandName = project.getCurrentCommandName();
var commandName = project_.getCurrentCommandName();
var properties = new Properties();
var success = false;
if (file == null) {
if (file_ == null) {
PropertyFileUtils.warn(commandName, "A properties file must be specified.");
} else {
success = PropertyFileUtils.loadProperties(commandName, file, properties);
success = PropertyFileUtils.loadProperties(commandName, file_, properties);
}
if (success) {
for (var entry : entries) {
for (var entry : entries_) {
if (entry.getKey().isBlank()) {
PropertyFileUtils.warn(commandName, "An entry key must specified.");
} 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);
} else {
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) {
success = PropertyFileUtils.processInt(commandName, properties, (EntryInt) entry, failOnWarning);
success = PropertyFileUtils.processInt(commandName, properties, (EntryInt) entry, failOnWarning_);
} else {
success = PropertyFileUtils.processString(properties, (Entry) entry);
}
@ -106,7 +105,7 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
}
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.
* @return this instance
*/
@SuppressWarnings("unused")
public PropertyFileOperation failOnWarning(boolean failOnWarning) {
this.failOnWarning = failOnWarning;
failOnWarning_ = failOnWarning;
return this;
}
@ -128,9 +126,8 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* @param file the file to be edited
* @return this instance
*/
@SuppressWarnings("unused")
public PropertyFileOperation file(File file) {
this.file = file;
file_ = file;
return this;
}
@ -140,9 +137,8 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* @param file the file to be edited
* @return this instance
*/
@SuppressWarnings("unused")
public PropertyFileOperation file(String file) {
this.file = new File(file);
file_ = new File(file);
return this;
}
@ -153,7 +149,7 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
* @return this instance
*/
public PropertyFileOperation fromProject(BaseProject project) {
this.project = project;
project_ = project;
return this;
}
}

View file

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

View file

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