Added pmd extension

Added fromProject instead of using the constructor
Improved tests
This commit is contained in:
Erik C. Thauvin 2023-04-15 20:31:13 -07:00
parent b5af26c074
commit 9dfde85473
18 changed files with 573 additions and 401 deletions

View file

@ -29,6 +29,11 @@ public final class Calc {
public static final IntFunction<Integer> ADD = Calc::add;
public static final IntFunction<Integer> SUB = Calc::sub;
private Calc() {
// no-op
}
/**
* Adds {@code 1} to the value.
*

View file

@ -30,16 +30,6 @@ public class Entry extends EntryBase {
super(key);
}
/**
* Sets the new {@link java.util.Properties property} value.
*
* @param s The new value
*/
public Entry set(Object s) {
setNewValue(s);
return this;
}
/**
* <p>Sets the initial value to set the {@link java.util.Properties property} to, if not already defined.</p>
*
@ -51,6 +41,24 @@ public class Entry extends EntryBase {
return this;
}
/**
* Sets the {@link Entry entry} up for deletion.
*/
public Entry delete() {
setDelete(true);
return this;
}
/**
* Creates a new {@link Entry entry}.
*
* @param modify the modification function
*/
public Entry modify(BiFunction<String, String, String> modify) {
setModify(modify);
return this;
}
/**
* Creates a new {@link Entry entry}.
*
@ -64,20 +72,12 @@ public class Entry extends EntryBase {
}
/**
* Creates a new {@link Entry entry}.
* Sets the new {@link java.util.Properties property} value.
*
* @param modify the modification function
* @param s The new value
*/
public Entry modify(BiFunction<String, String, String> modify) {
setModify(modify);
return this;
}
/**
* Sets the {@link Entry entry} up for deletion.
*/
public Entry delete() {
setDelete(true);
public Entry set(Object s) {
setNewValue(s);
return this;
}
}

View file

@ -27,15 +27,15 @@ import java.util.function.IntFunction;
* @since 1.0
*/
public class EntryBase {
private String key;
private IntFunction<Integer> calc;
private Object defaultValue;
private Object newValue;
private String modifyValue = "";
private boolean isDelete;
private String key;
private BiFunction<String, String, String> modify;
private String modifyValue = "";
private Object newValue;
private String pattern = "";
private EntryDate.Units unit = EntryDate.Units.DAY;
private IntFunction<Integer> calc;
private BiFunction<String, String, String> modify;
/**
* Creates a new {@link EntryBase entry}.
@ -46,60 +46,6 @@ public class EntryBase {
this.key = key;
}
/**
* Returns the value to be used in the {@link #modify} function.
*/
protected String getModifyValue() {
return modifyValue;
}
/**
* Sets the modify value.
*
* @param value the modify value.
*/
protected void setModifyValue(String value) {
this.modifyValue = value;
}
/**
* Returns the modify function.
*/
protected BiFunction<String, String, String> getModify() {
return modify;
}
/**
* Sets the modify function.
*/
protected void setModify(BiFunction<String, String, String> modify) {
this.modify = modify;
}
/**
* Sets the modify function.
*
* @param value the value to perform a modification with
*/
protected void setModify(String value, BiFunction<String, String, String> modify) {
this.modifyValue = value;
this.modify = modify;
}
/**
* Returns {@code true} if the {@link java.util.Properties property} is to be deleted.
*/
protected boolean isDelete() {
return isDelete;
}
/**
* Sets whether the {@link java.util.Properties property} should be deleted.
*/
protected void setDelete(boolean delete) {
isDelete = delete;
}
/**
* Returns the calculation function.
*/
@ -114,6 +60,22 @@ public class EntryBase {
this.calc = calc;
}
/**
* Returns the default value.
*/
protected Object getDefaultValue() {
return defaultValue;
}
/**
* Sets the initial value to set the {@link java.util.Properties property} to, if not already defined.
*
* @param defaultValue the default value
*/
protected void setDefaultValue(Object defaultValue) {
this.defaultValue = defaultValue;
}
/**
* Returns the key of the {@link java.util.Properties property}.
*/
@ -131,19 +93,49 @@ public class EntryBase {
}
/**
* Returns the default value.
* Returns the modify function.
*/
protected Object getDefaultValue() {
return defaultValue;
protected BiFunction<String, String, String> getModify() {
return modify;
}
/**
* Sets the initial value to set the {@link java.util.Properties property} to, if not already defined.
*
* @param defaultValue the default value
* Sets the modify function.
*/
protected void setDefaultValue(Object defaultValue) {
this.defaultValue = defaultValue;
protected void setModify(BiFunction<String, String, String> modify) {
this.modify = modify;
}
/**
* Returns the value to be used in the {@link #modify} function.
*/
protected String getModifyValue() {
return modifyValue;
}
/**
* Sets the modify value.
*
* @param value the modify value.
*/
protected void setModifyValue(String value) {
this.modifyValue = value;
}
/**
* Returns the new value to set the {@link java.util.Properties property)} to.
*/
public Object getNewValue() {
return newValue;
}
/**
* Sets a new value for {@link java.util.Properties property}.
*
* @param newValue the new value
*/
public void setNewValue(Object newValue) {
this.newValue = newValue;
}
/**
@ -179,6 +171,20 @@ public class EntryBase {
this.unit = unit;
}
/**
* Returns {@code true} if the {@link java.util.Properties property} is to be deleted.
*/
protected boolean isDelete() {
return isDelete;
}
/**
* Sets whether the {@link java.util.Properties property} should be deleted.
*/
protected void setDelete(boolean delete) {
isDelete = delete;
}
/**
* Sets the key of the {@link java.util.Properties property}.
*
@ -191,18 +197,12 @@ public class EntryBase {
}
/**
* Returns the new value to set the {@link java.util.Properties property)} to.
*/
public Object getNewValue() {
return newValue;
}
/**
* Sets a new value for {@link java.util.Properties property}.
* Sets the modify function.
*
* @param newValue the new value
* @param value the value to perform a modification with
*/
public void setNewValue(Object newValue) {
this.newValue = newValue;
protected void setModify(String value, BiFunction<String, String, String> modify) {
this.modifyValue = value;
this.modify = modify;
}
}

View file

@ -37,6 +37,43 @@ public class EntryDate extends EntryBase {
super(key);
}
/**
* Creates a new {@link EntryDate entry}.
*
* @param calc the calculation function
*/
public EntryDate calc(IntFunction<Integer> calc) {
setCalc(calc);
return this;
}
/**
* Sets the {@link EntryDate entry} up for deletion.
*/
public EntryDate delete() {
setDelete(true);
return this;
}
/**
* Sets the new {@link java.util.Properties property} value to now.
*/
public EntryDate now() {
setNewValue("now");
return this;
}
/**
* Sets the pattern for {@link EntryInt} and {@link EntryDate} to{@link java.text.DecimalFormat DecimalFormat} and
* {@link java.time.format.DateTimeFormatter DateTimeFormatter} respectively.
*
* @param pattern the pattern
*/
public EntryDate pattern(String pattern) {
setPattern(pattern);
return this;
}
/**
* Sets the new {@link java.util.Properties property} value to an {@link Instant}
*
@ -107,35 +144,6 @@ public class EntryDate extends EntryBase {
return this;
}
/**
* Sets the new {@link java.util.Properties property} value to now.
*/
public EntryDate now() {
setNewValue("now");
return this;
}
/**
* Creates a new {@link EntryDate entry}.
*
* @param calc the calculation function
*/
public EntryDate calc(IntFunction<Integer> calc) {
setCalc(calc);
return this;
}
/**
* Sets the pattern for {@link EntryInt} and {@link EntryDate} to{@link java.text.DecimalFormat DecimalFormat} and
* {@link java.time.format.DateTimeFormatter DateTimeFormatter} respectively.
*
* @param pattern the pattern
*/
public EntryDate pattern(String pattern) {
setPattern(pattern);
return this;
}
/**
* Sets the {@link Units unit} value to apply to calculations for {@link EntryDate}.
*
@ -146,14 +154,6 @@ public class EntryDate extends EntryBase {
return this;
}
/**
* Sets the {@link EntryDate entry} up for deletion.
*/
public EntryDate delete() {
setDelete(true);
return this;
}
/**
* The units available for {@link EntryDate} calculations.
*

View file

@ -35,12 +35,12 @@ public class EntryInt extends EntryBase {
}
/**
* Sets the new {@link java.util.Properties property} value to an integer.
* Creates a new {@link EntryInt entry}.
*
* @param i The integer to set the value to
* @param calc the calculation function.
*/
public EntryInt set(int i) {
setNewValue(i);
public EntryInt calc(IntFunction<Integer> calc) {
setCalc(calc);
return this;
}
@ -55,16 +55,6 @@ public class EntryInt extends EntryBase {
return this;
}
/**
* Creates a new {@link EntryInt entry}.
*
* @param calc the calculation function.
*/
public EntryInt calc(IntFunction<Integer> calc) {
setCalc(calc);
return this;
}
/**
* Sets the {@link EntryInt entry} up for deletion.
*/
@ -72,4 +62,14 @@ public class EntryInt extends EntryBase {
setDelete(true);
return this;
}
/**
* Sets the new {@link java.util.Properties property} value to an integer.
*
* @param i The integer to set the value to
*/
public EntryInt set(int i) {
setNewValue(i);
return this;
}
}

View file

@ -16,7 +16,7 @@
package rife.bld.extension.propertyfile;
import rife.bld.Project;
import rife.bld.BaseProject;
import rife.bld.operations.AbstractOperation;
import java.io.File;
@ -32,13 +32,20 @@ import java.util.Properties;
*/
public class PropertyFileOperation extends AbstractOperation<PropertyFileOperation> {
private final List<EntryBase> entries = new ArrayList<>();
private final Project project;
private File file;
private String comment = "";
private boolean failOnWarning;
private File file;
private BaseProject project;
public PropertyFileOperation(Project project) {
this.project = project;
/**
* Sets the comment to be inserted at the top of the {@link java.util.Properties} file.
*
* @param comment the header comment
*/
@SuppressWarnings("unused")
public PropertyFileOperation comment(String comment) {
this.comment = comment;
return this;
}
/**
@ -53,50 +60,6 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
return this;
}
/**
* Sets the location of the {@link java.util.Properties} file to be edited.
*
* @param file the file to be edited
*/
@SuppressWarnings("unused")
public PropertyFileOperation file(String file) {
this.file = new File(file);
return this;
}
/**
* Sets the location of the {@link java.util.Properties} file to be edited.
*
* @param file the file to be edited
*/
@SuppressWarnings("unused")
public PropertyFileOperation file(File file) {
this.file = file;
return this;
}
/**
* Sets the {@link #execute() execution} to return a failure on any warnings.
*
* @param failOnWarning if set to {@code true}, the execution will fail on any warnings.
*/
@SuppressWarnings("unused")
public PropertyFileOperation failOnWarning(boolean failOnWarning) {
this.failOnWarning = failOnWarning;
return this;
}
/**
* Sets the comment to be inserted at the top of the {@link java.util.Properties} file.
*
* @param comment the header comment
*/
@SuppressWarnings("unused")
public PropertyFileOperation comment(String comment) {
this.comment = comment;
return this;
}
/**
* Performs the modification(s) to the {@link java.util.Properties properties} file.
*/
@ -128,12 +91,13 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
&& (p == null || p.isBlank())) {
PropertyFileUtils.warn(commandName, "An entry must be set or have a default value: " + key);
} else {
if (entry instanceof EntryDate)
if (entry instanceof EntryDate) {
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);
else
} else {
success = PropertyFileUtils.processString(properties, (Entry) entry);
}
}
}
}
@ -143,4 +107,45 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
PropertyFileUtils.saveProperties(file, comment, properties);
}
}
/**
* Sets the {@link #execute() execution} to return a failure on any warnings.
*
* @param failOnWarning if set to {@code true}, the execution will fail on any warnings.
*/
@SuppressWarnings("unused")
public PropertyFileOperation failOnWarning(boolean failOnWarning) {
this.failOnWarning = failOnWarning;
return this;
}
/**
* Sets the location of the {@link java.util.Properties} file to be edited.
*
* @param file the file to be edited
*/
@SuppressWarnings("unused")
public PropertyFileOperation file(File file) {
this.file = file;
return this;
}
/**
* Sets the location of the {@link java.util.Properties} file to be edited.
*
* @param file the file to be edited
*/
@SuppressWarnings("unused")
public PropertyFileOperation file(String file) {
this.file = new File(file);
return this;
}
/**
* Creates a new operation.
*/
public PropertyFileOperation fromProject(BaseProject project) {
this.project = project;
return this;
}
}

View file

@ -45,6 +45,49 @@ public final class PropertyFileUtils {
// no-op
}
/**
* Returns the new value, value or default value depending on which is specified.
*
* @param value the value
* @param newValue the new value
* @param defaultValue the default value
*/
public static Object currentValue(String value, Object defaultValue, Object newValue) {
if (newValue != null) {
return newValue;
} else if (value == null) {
return defaultValue;
} else {
return value;
}
}
/**
* Loads a {@link Properties properties} file.
*
* @param command the issuing command
* @param file the file location
* @param p the {@link Properties properties} to load into.
*/
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public static boolean loadProperties(String command, File file, Properties p) throws Exception {
boolean success = true;
if (file != null) {
if (file.exists()) {
try (var propStream = Files.newInputStream(file.toPath(), StandardOpenOption.READ)) {
p.load(propStream);
} catch (IOException ioe) {
warn(command, "Could not load properties file: " + ioe.getMessage(), ioe, true);
success = false;
}
}
} else {
warn(command, "Please specify the properties file location.");
success = false;
}
return success;
}
/**
* Processes a date {@link Properties property}.
*
@ -52,6 +95,7 @@ public final class PropertyFileUtils {
* @param p the {@link Properties property}
* @param entry the {@link Entry} containing the {@link Properties property} edits
*/
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public static boolean processDate(String command, Properties p, EntryDate entry, boolean failOnWarning)
throws Exception {
var success = true;
@ -146,23 +190,6 @@ public final class PropertyFileUtils {
return success;
}
/**
* Returns the new value, value or default value depending on which is specified.
*
* @param value the value
* @param newValue the new value
* @param defaultValue the default value
*/
public static Object currentValue(String value, Object defaultValue, Object newValue) {
if (newValue != null) {
return newValue;
} else if (value == null) {
return defaultValue;
} else {
return value;
}
}
/**
* Processes an integer {@link Properties property}.
*
@ -170,6 +197,7 @@ public final class PropertyFileUtils {
* @param p the {@link Properties property}
* @param entry the {@link Entry} containing the {@link Properties property} edits
*/
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
public static boolean processInt(String command, Properties p, EntryInt entry, boolean failOnWarning)
throws Exception {
var success = true;
@ -215,6 +243,21 @@ public final class PropertyFileUtils {
return true;
}
/**
* Saves a {@link Properties properties} file.
*
* @param file the file location
* @param comment the header comment
* @param p the {@link Properties} to save into the file
*/
public static void saveProperties(File file, String comment, Properties p) throws IOException {
try (var output = Files.newOutputStream(file.toPath())) {
p.store(output, comment);
} catch (IIOException ioe) {
throw new IIOException("An IO error occurred while saving the Properties file: " + file, ioe);
}
}
/**
* Logs a warning.
*
@ -235,6 +278,7 @@ public final class PropertyFileUtils {
* @param e the related exception
* @param failOnWarning logs and throws exception if set to {@code true}
*/
@SuppressWarnings({"PMD.SignatureDeclareThrowsException"})
static void warn(String command, String message, Exception e, boolean failOnWarning) throws Exception {
if (failOnWarning) {
LOGGER.log(Level.SEVERE, '[' + command + "] " + message, e);
@ -243,44 +287,4 @@ public final class PropertyFileUtils {
warn(command, message);
}
}
/**
* Loads a {@link Properties properties} file.
*
* @param command the issuing command
* @param file the file location
* @param p the {@link Properties properties} to load into.
*/
public static boolean loadProperties(String command, File file, Properties p) throws Exception {
boolean success = true;
if (file != null) {
if (file.exists()) {
try (var propStream = Files.newInputStream(file.toPath(), StandardOpenOption.READ)) {
p.load(propStream);
} catch (IOException ioe) {
warn(command, "Could not load properties file: " + ioe.getMessage(), ioe, true);
success = false;
}
}
} else {
warn(command, "Please specify the properties file location.");
success = false;
}
return success;
}
/**
* Saves a {@link Properties properties} file.
*
* @param file the file location
* @param comment the header comment
* @param p the {@link Properties} to save into the file
*/
public static void saveProperties(File file, String comment, Properties p) throws IOException {
try (var output = Files.newOutputStream(file.toPath())) {
p.store(output, comment);
} catch (IIOException ioe) {
throw new IIOException("An IO error occurred while saving the Properties file: " + file, ioe);
}
}
}