Added calc and modify functions

This commit is contained in:
Erik C. Thauvin 2023-04-04 09:01:12 -07:00
parent 3fe54858fa
commit 753f3bcf45
19 changed files with 382 additions and 343 deletions

View file

@ -0,0 +1,50 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rife.bld.extension.propertyfile;
import java.util.function.IntFunction;
/**
* Implements the calculation functions.
*
* @author <a href="https://github.com/gbevin">Geert Bevin</a>
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public final class Calc {
public static final IntFunction<Integer> ADD = Calc::add;
public static final IntFunction<Integer> SUB = Calc::sub;
/**
* Adds {@code 1} to the value.
*
* @param v the value
*/
public static Integer add(int v) {
return v + 1;
}
/**
* Subtracts {@code 1} to the value.
*
* @param v the value
*/
public static Integer sub(int v) {
return v - 1;
}
}

View file

@ -16,35 +16,30 @@
package rife.bld.extension.propertyfile;
import java.util.function.BiFunction;
import java.util.function.IntFunction;
/**
* <p>Declares the edits to be made to a {@link java.util.Properties Properties} file.</p>
*
* <p>The rules used when setting a {@link java.util.Properties property} value are:</p>
*
* <ul>
* <li>If only value is specified, the property is set to it regardless of its previous value.</li>
* <li>If only default value is specified and the property previously existed, it is unchanged.</li>
* <li>If only default value is specified and the property did not exist, the property is set to the default value.</li>
* <li>If value and default value are both specified and the property previously existed, the property is set to value.</li>
* <li>If value and default value are both specified and the property did not exist, the property is set to the default value.</li>
* </ul>
*
* <p>{@link Operations Operations} occur after the rules are evaluated.</p>
* Declares the modifications to be made to a {@link java.util.Properties Properties} file.
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @author <a href="https://github.com/gbevin">Geert Bevin</a>
* @since 1.0
*/
public class Entry {
private String key;
private String value;
private String defaultValue;
private String newValue;
private String modifyValue;
private boolean isDelete;
private Types type = Types.STRING;
private Operations operation = Operations.SET;
private String pattern = "";
private Units unit = Units.DAY;
private IntFunction<Integer> calc;
private BiFunction<String, String, String> modify;
/**
* Creates a new {@link Entry entry} Entry.
* Creates a new {@link Entry entry}.
*
* @param key the required property key
*/
@ -53,14 +48,116 @@ public class Entry {
}
/**
* Returns the name of the {@link java.util.Properties property} name/value pair.
* Creates a new {@link Entry entry}.
*
* @param key the required property key
* @param type the value {@link Types Type}
*/
public Entry(String key, Types type) {
this.key = key;
this.type = type;
}
/**
* Returns the value to be used in the {@link #modify} function.
*/
public String getModifyValue() {
return modifyValue;
}
/**
* Returns the modify function.
*/
public BiFunction<String, String, String> getModify() {
return modify;
}
/**
* Set the modify function.
*/
public void setModify(BiFunction<String, String, String> modify) {
this.modify = modify;
}
/**
* Set the modify function.
*
* @param value the value to perform a modification with
*/
public void setModify(String value, BiFunction<String, String, String> modify) {
this.modifyValue = value;
this.modify = modify;
}
/**
* Creates a new {@link Entry entry}.
*
* @param value the value to perform a modification with
* @param modify the modification function
*/
public Entry modify(String value, BiFunction<String, String, String> modify) {
modifyValue = value;
setModify(modify);
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;
}
/**
* Returns {@code true} if the {@link Entry} is to be deleted.
*/
public boolean isDelete() {
return isDelete;
}
/**
* Sets whether the {@link Entry} should be deleted.
*/
public void setDelete(boolean delete) {
isDelete = delete;
}
/**
* Returns the calculation function.
*/
public IntFunction<Integer> getCalc() {
return calc;
}
/**
* Sets the calculation function.
*/
public void setCalc(IntFunction<Integer> calc) {
this.calc = calc;
}
/**
* Creates a new {@link Entry entry}.
*
* @param calc the calculation function.
*/
public Entry calc(IntFunction<Integer> calc) {
setCalc(calc);
return this;
}
/**
* Returns the key of the {@link java.util.Properties property}.
*/
public String getKey() {
return key;
}
/**
* Sets the name of the {@link java.util.Properties property} name/value pair.
* Sets the key of the {@link java.util.Properties property}.
*
* @param key the {@link java.util.Properties property} key
*/
@ -68,22 +165,6 @@ public class Entry {
this.key = key;
}
/**
* Returns the value of the {@link java.util.Properties property}.
*/
public String getValue() {
return value;
}
/**
* Sets the value of the {@link java.util.Properties property}.
*
* @param value the {@link java.util.Properties property} value
*/
public void setValue(String value) {
this.value = value;
}
/**
* Returns the default value.
*/
@ -92,7 +173,7 @@ public class Entry {
}
/**
* <p>Sets the initial value to set for the {@link java.util.Properties property} if not already defined.</p>
* <p>Sets the initial value to set the {@link java.util.Properties property} to, if not already defined.</p>
*
* <p>The {@code now} keyword can be used for {@link Types#DATE Types.DATE}</p>
*
@ -103,7 +184,7 @@ public class Entry {
}
/**
* Return the value {@link Types Type}/
* Return the value {@link Types Type}.
*/
public Types getType() {
return type;
@ -118,22 +199,6 @@ public class Entry {
this.type = type;
}
/**
* Return the {@link Operations Operation}.
*/
public Operations getOperation() {
return operation;
}
/**
* Sets the {@link Operations Operation} to be performed on the {@link java.util.Properties property} value,
*
* @param operation the entry {@link Operations Operation}
*/
public void setOperation(Operations operation) {
this.operation = operation;
}
/**
* Returns the pattern.
*/
@ -142,9 +207,8 @@ public class Entry {
}
/**
* <p>Parses the value of {@link Types#INT Types.INT} and {@link Types#DATE Types.DATE} to
* {@link java.text.DecimalFormat DecimalFormat} and {@link java.text.SimpleDateFormat SimpleDateFormat}
* respectively.</p>
* Sets the {@link java.text.DecimalFormat DecimalFormat} or {@link java.text.SimpleDateFormat SimpleDateFormat}
* pattern to be used with {@link Types#INT Types.INT} or {@link Types#DATE Types.DATE} respectively.
*
* @param pattern the pattern
*/
@ -160,8 +224,8 @@ public class Entry {
}
/**
* Sets the {@link Units unit} value to apply to {@link Operations#ADD Operations.ADD}
* and {@link Operations#SUBTRACT Operations.SUBTRACT} for {@link Types#DATE Types.DATE}.
* Sets the {@link Units unit} value to apply to {@link Calc#ADD add} and {@link Calc#SUB subtract} calculations
* for {@link Types#DATE Types.DATE}.
*
* @param unit the {@link Units unit}
*/
@ -170,11 +234,10 @@ public class Entry {
}
/**
* Sets the name of the {@link java.util.Properties property} name/value pair.
* Sets the key of the {@link java.util.Properties property}.
*
* @param key the {@link java.util.Properties property} key
*/
@SuppressWarnings("unused")
public Entry key(String key) {
setKey(key);
@ -182,22 +245,31 @@ public class Entry {
}
/**
* Sets the value of the {@link java.util.Properties property}.
* Returns the new value to set the {@link java.util.Properties property)} to.
*/
public String getNewValue() {
return newValue;
}
/**
* Sets a new value to set the {@link java.util.Properties property} to, regardless of its previous value.
*
* @param value the {@link java.util.Properties property} value
* <p>The {@code now} keyword can be used for {@link Types#DATE Types.DATE}</p>
*
* @param newValue the {@link java.util.Properties property} new value
*/
@SuppressWarnings("unused")
public Entry value(Object value) {
if (value != null) {
setValue(String.valueOf(value));
public Entry set(Object newValue) {
if (newValue != null) {
this.newValue = String.valueOf(newValue);
} else {
setValue(null);
this.newValue = null;
}
return this;
}
/**
* <p>Sets the initial value to set for the {@link java.util.Properties property} if not already defined.</p>
* <p>Sets the initial value to set the {@link java.util.Properties property} to, if not already defined.</p>
*
* <p>The {@code now} keyword can be used for {@link Types#DATE Types.DATE}</p>
*
@ -224,18 +296,7 @@ public class Entry {
}
/**
* Sets the {@link Operations Operation} to be performed on the {@link java.util.Properties property} value,
*
* @param operation the entry {@link Operations Operation}
*/
@SuppressWarnings("unused")
public Entry operation(Operations operation) {
setOperation(operation);
return this;
}
/**
* <p>Parses the value of {@link Types#INT Types.INT} and {@link Types#DATE Types.DATE} to
* <p>Sets the pattern for {@link Types#INT Types.INT} and {@link Types#DATE Types.DATE} to
* {@link java.text.DecimalFormat DecimalFormat} and {@link java.text.SimpleDateFormat SimpleDateFormat}
* respectively.</p>
*
@ -247,8 +308,8 @@ public class Entry {
}
/**
* Sets the {@link Units unit} value to apply to {@link Operations#ADD Operations.ADD}
* and {@link Operations#SUBTRACT Operations.SUBTRACT} for {@link Types#DATE Types.DATE}.
* Sets the {@link Units unit} value to apply to {@link Calc#ADD add} and {@link Calc#SUB subtract} calculations
* for {@link Types#DATE Types.DATE}.
*
* @param unit the {@link Units unit}
*/
@ -258,6 +319,14 @@ public class Entry {
return this;
}
/**
* Sets the {@link Entry entry} up for deletion.
*/
public Entry delete() {
isDelete = true;
return this;
}
/**
* The available datatypes.
*
@ -272,23 +341,8 @@ public class Entry {
}
/**
* The operations available for all {@link Types Types}.
*
* <uL>
* <li>{@link Operations#ADD ADD} adds a value to an {@link Entry entry}</li>
* <li>{@link Operations#DELETE DELETE} deletes an entry</li>
* <li>{@link Operations#SET SET} sets the entry value. This is the default operation</li>
* <li>{@link Operations#SUBTRACT SUBTRACT} subtracts a value from the {@link Entry entry}.
* For {@link Types#INT Types.INT} and {@link Types#DATE Types.DATE} only.</li>
* </uL>
*/
public enum Operations {
ADD, DELETE, SET, SUBTRACT
}
/**
* The units available for {@link Types#DATE Type.DATE} with {@link Operations#ADD Operations>ADD}
* and {@link Operations#SUBTRACT Operations.SUBTRACT}.
* The units available for {@link Types#DATE Type.DATE} {@link Calc#ADD add}
* and {@link Calc#SUB subtract} calculations.
*
* <uL>
* <li>{@link Units#SECOND SECOND}</li>

View file

@ -17,8 +17,6 @@
package rife.bld.extension.propertyfile;
import rife.bld.Project;
import rife.bld.extension.propertyfile.Entry.Operations;
import rife.bld.extension.propertyfile.Entry.Types;
import rife.bld.operations.AbstractOperation;
import java.io.File;
@ -36,9 +34,9 @@ import java.util.Properties;
public class PropertyFileOperation extends AbstractOperation<PropertyFileOperation> {
private final List<Entry> entries = new ArrayList<>();
private final Project project;
private File file = null;
private File file;
private String comment = "";
private boolean failOnWarning = false;
private boolean failOnWarning;
public PropertyFileOperation(Project project) {
this.project = project;
@ -122,17 +120,13 @@ public class PropertyFileOperation extends AbstractOperation<PropertyFileOperati
success = false;
} else {
var key = entry.getKey();
var value = entry.getValue();
var value = entry.getNewValue();
var defaultValue = entry.getDefaultValue();
if ((value == null || value.isBlank()) && (defaultValue == null || defaultValue.isBlank())
&& entry.getOperation() != Operations.DELETE) {
PropertyFileUtils.warn(commandName, "An entry value or default must be specified: " + key);
success = false;
} else if (entry.getType() == Types.STRING && entry.getOperation() == Operations.SUBTRACT) {
PropertyFileUtils.warn(commandName, "Subtraction is not supported for String properties: " + key);
success = false;
} else if (entry.getOperation() == Operations.DELETE) {
if (entry.isDelete()) {
properties.remove(key);
} else if ((value == null || value.isBlank()) && (defaultValue == null || defaultValue.isBlank())) {
PropertyFileUtils.warn(commandName, "An entry must be set or have a default value: " + key);
success = false;
} else {
switch (entry.getType()) {
case DATE ->

View file

@ -16,7 +16,6 @@
package rife.bld.extension.propertyfile;
import rife.bld.extension.propertyfile.Entry.Operations;
import rife.bld.extension.propertyfile.Entry.Units;
import rife.tools.Localization;
@ -45,7 +44,7 @@ import java.util.logging.Logger;
public final class PropertyFileUtils {
private final static Logger LOGGER = Logger.getLogger(PropertyFileUtils.class.getName());
private final static Map<Units, Integer> calendarFields =
private final static Map<Units, Integer> CALENDAR_FIELDS =
Map.of(Units.MILLISECOND, Calendar.MILLISECOND,
Units.SECOND, Calendar.SECOND,
Units.MINUTE, Calendar.MINUTE,
@ -70,8 +69,8 @@ public final class PropertyFileUtils {
public static boolean processDate(String command, Properties p, Entry entry, boolean failOnWarning) {
var success = true;
var cal = Calendar.getInstance();
var value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getValue(),
entry.getDefaultValue(), entry.getOperation());
String value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getDefaultValue(),
entry.getNewValue());
var pattern = entry.getPattern();
SimpleDateFormat fmt;
@ -80,6 +79,7 @@ public final class PropertyFileUtils {
} else {
fmt = new SimpleDateFormat(entry.getPattern(), Localization.getLocale());
}
if ("now".equalsIgnoreCase(value) || value.isBlank()) {
cal.setTime(new Date());
} else {
@ -92,74 +92,36 @@ public final class PropertyFileUtils {
}
}
if (entry.getOperation() != Entry.Operations.SET) {
var offset = 0;
var offset = 0;
try {
offset = Integer.parseInt(entry.getValue());
if (entry.getOperation() == Entry.Operations.SUBTRACT) {
offset *= -1;
}
} catch (NumberFormatException nfe) {
warn(command, "Non-date value for \"" + entry.getKey() + "\" --> " + nfe.getMessage(), nfe,
failOnWarning);
success = false;
}
//noinspection MagicConstant
cal.add(calendarFields.getOrDefault(entry.getUnit(), Calendar.DATE), offset);
if (entry.getCalc() != null) {
offset = entry.getCalc().apply(offset);
}
//noinspection MagicConstant
cal.add(CALENDAR_FIELDS.getOrDefault(entry.getUnit(), Calendar.DATE), offset);
p.setProperty(entry.getKey(), fmt.format(cal.getTime()));
return success;
}
/**
* Return the current value, new value or default value based on the specified {@link Operations operation}.
* 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
* @param operation the {@link Operations operation}
* @return the current value
*/
public static String currentValue(String value, String newValue, String defaultValue, Operations operation) {
String result = null;
if (operation == Entry.Operations.SET) {
if (newValue != null && defaultValue == null) {
result = newValue;
}
if (defaultValue != null) {
if (newValue == null && value != null) {
result = value;
}
if (newValue == null && value == null) {
result = defaultValue;
}
if (newValue != null && value != null) {
result = newValue;
}
if (newValue != null && value == null) {
result = defaultValue;
}
}
public static String currentValue(String value, String defaultValue, String newValue) {
if (newValue != null) {
return newValue;
} else if (value == null) {
return defaultValue;
} else {
if (value == null) {
result = defaultValue;
} else {
result = value;
}
return value;
}
if (result == null) {
result = "";
}
return result;
}
/**
@ -172,28 +134,18 @@ public final class PropertyFileUtils {
*/
public static boolean processInt(String command, Properties p, Entry entry, boolean failOnWarning) {
var success = true;
int intValue;
int intValue = 0;
try {
var fmt = new DecimalFormat(entry.getPattern());
var value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getValue(),
entry.getDefaultValue(), entry.getOperation());
String value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getDefaultValue(),
entry.getNewValue());
if (value.isBlank()) {
intValue = fmt.parse("0").intValue();
} else {
if (value != null) {
intValue = fmt.parse(value).intValue();
}
if (entry.getOperation() != Entry.Operations.SET) {
var opValue = 1;
if (entry.getValue() != null) {
opValue = fmt.parse(entry.getValue()).intValue();
}
if (entry.getOperation() == Entry.Operations.ADD) {
intValue += opValue;
} else if (entry.getOperation() == Entry.Operations.SUBTRACT) {
intValue -= opValue;
}
if (entry.getCalc() != null) {
intValue = entry.getCalc().apply(intValue);
}
p.setProperty(entry.getKey(), fmt.format(intValue));
} catch (NumberFormatException | ParseException e) {
@ -213,15 +165,13 @@ public final class PropertyFileUtils {
* @return {@code true} if successful
*/
public static boolean processString(Properties p, Entry entry) {
var value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getValue(),
entry.getDefaultValue(), entry.getOperation());
var value = PropertyFileUtils.currentValue(p.getProperty(entry.getKey()), entry.getDefaultValue(),
entry.getNewValue());
if (entry.getOperation() == Entry.Operations.SET) {
p.setProperty(entry.getKey(), value);
} else if (entry.getOperation() == Entry.Operations.ADD) {
if (entry.getValue() != null) {
p.setProperty(entry.getValue(), "$value${entry.value}");
}
p.setProperty(entry.getKey(), value);
if (entry.getModify() != null && entry.getModifyValue() != null) {
p.setProperty(entry.getKey(), entry.getModify().apply(p.getProperty(entry.getKey()), entry.getModifyValue()));
}
return true;