Class PropertyFileUtils
PropertyFileOperation.- Since:
- 1.0
- Author:
- Erik C. Thauvin
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidloadProperties(File file, Properties p) Loads apropertiesfile.static voidprocessDate(Properties p, EntryDate entry) Processes a dateproperties.static voidprocessInt(Properties p, EntryInt entry) Processes an integerproperties.static voidprocessString(Properties p, Entry entry) Processes a stringproperty.static ObjectresolveValue(String existing, Object defaultValue, Object newValue) Resolves the effective value using precedence:newValue→ existing →defaultValue.static voidsaveProperties(File file, String comment, Properties props) Saves apropertiesfile atomically when possible.
-
Method Details
-
loadProperties
Loads apropertiesfile.If the file does not exist, no properties are loaded. The file will be created when
saveProperties(File, String, Properties)is called.- Parameters:
file- the file locationp- thepropertiesto load into- Throws:
IOException- if an error occurred while reading the fileNullPointerException- iffileorpisnull
-
processDate
Processes a dateproperties.If no value is present and
calcis defined, the calculation starts fromZonedDateTime.now(). This matches Ant<propertyfile>behavior. To fail on missing values, do not usecalcwithout also settingdefaultValueornewValue.- Parameters:
p- thepropertiesentry- theEntryDatecontaining thepropertiesedits- Throws:
DateTimeException- if a parsing or arithmetic error occursIllegalArgumentException- if no value is configured and nocalcis set
-
processInt
Processes an integerproperties.If no value is present and
calcis defined, the calculation starts from0. This matches Ant<propertyfile>behavior. To fail on missing values, do not usecalcwithout also settingdefaultValueornewValue.- Parameters:
p- thepropertiesentry- theEntryIntcontaining thepropertiesedits- Throws:
IllegalArgumentException- if no value is configured and nocalcis set, or the value cannot be parsed as an integer
-
processString
Processes a stringproperty.- Parameters:
p- thepropertyentry- theEntrycontaining thepropertyedits- Throws:
IllegalArgumentException- if no value is configured or the modify function fails/returnsnull
-
resolveValue
@NullUnmarked public static Object resolveValue(String existing, Object defaultValue, Object newValue) Resolves the effective value using precedence:newValue→ existing →defaultValue.nullmeans the value is missing. Empty string""is considered a valid value.- Parameters:
existing- the current persisted value; may benullif the key is absentdefaultValue- the fallback used when bothexistingandnewValuearenullnewValue- when non-null, always takes precedence- Returns:
- the resolved object;
nullif all three arguments arenull
-
saveProperties
Saves apropertiesfile atomically when possible.The method writes to a temporary file in the same directory, calls
fsync, then performs an atomic move to replace the target. If the filesystem does not support atomic move or the directory does not allow renames, it falls back to a non-atomic overwrite.- Parameters:
file- the file locationcomment- the header commentprops- thePropertiesto save into the file- Throws:
IOException- if an IO error occurs while writing the file
-