diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/helper/FileHelper.java b/li.strolch.utils/src/main/java/li/strolch/utils/helper/FileHelper.java
index 5839bf2be..23350f59c 100644
--- a/li.strolch.utils/src/main/java/li/strolch/utils/helper/FileHelper.java
+++ b/li.strolch.utils/src/main/java/li/strolch/utils/helper/FileHelper.java
@@ -15,8 +15,10 @@
*/
package li.strolch.utils.helper;
+import static java.util.Collections.emptySet;
import static li.strolch.utils.helper.StringHelper.isEmpty;
import static li.strolch.utils.helper.StringHelper.normalizeLength;
+import static li.strolch.utils.helper.TempFileOptions.*;
import java.io.*;
import java.nio.file.Files;
@@ -27,9 +29,7 @@ import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
import li.strolch.utils.DataUnit;
import org.slf4j.Logger;
@@ -49,7 +49,8 @@ public class FileHelper {
private static final DateTimeFormatter nonSeparatedDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
/**
- * Generates a path to the given temporary path to write a file separated in sub folders by day using current date
+ *
Generates a path to the given temporary path to write a file separated in sub folders by day using current
+ * date without any options enabled
*
* @param tempPath
* the directory to which to write the file
@@ -57,20 +58,24 @@ public class FileHelper {
* the prefix of the file name
* @param suffix
* the prefix of the file name
- * @param separateDateSegments
- * true to separate date segments in sub folders
- * @param separateHours
- * true to have sub folders for each hour
*
* @return the temporary file
*/
- public static File getTempFile(File tempPath, String prefix, String suffix, boolean separateDateSegments,
- boolean separateHours) {
- return getTempFile(tempPath, prefix, suffix, separateDateSegments, separateHours, true);
+ public static File getTempFile(File tempPath, String prefix, String suffix) {
+ return getTempFile(tempPath, prefix, suffix, emptySet());
}
/**
- * Generates a path to the given temporary path to write a file separated in sub folders by day using current date
+ * Generates a path to the given temporary path to write a file separated in sub folders by day using current
+ * date
+ *
+ * The options can be one of:
+ *
+ * - {@link TempFileOptions#APPEND_MILLIS}
+ * - {@link TempFileOptions#SEPARATE_DATE_SEGMENTS}
+ * - {@link TempFileOptions#SEPARATE_HOURS}
+ * - {@link TempFileOptions#WITH_HOURS}
+ *
*
* @param tempPath
* the directory to which to write the file
@@ -78,30 +83,33 @@ public class FileHelper {
* the prefix of the file name
* @param suffix
* the prefix of the file name
- * @param separateDateSegments
- * true to separate date segments in sub folders
- * @param separateHours
- * true to have sub folders for each hour
- * @param appendMillis
- * true of append the current milliseconds to the file name
+ * @param options
+ * the option bits
*
* @return the temporary file
*/
- public static File getTempFile(File tempPath, String prefix, String suffix, boolean separateDateSegments,
- boolean separateHours, boolean appendMillis) {
+ public static File getTempFile(File tempPath, String prefix, String suffix, Set options) {
LocalDateTime dateTime = LocalDateTime.now();
LocalDate localDate = dateTime.toLocalDate();
+ boolean separateDateSegments = options.contains(SEPARATE_DATE_SEGMENTS);
String date = separateDateSegments ? localDate.format(separatedDtf) : localDate.format(nonSeparatedDtf);
- String currentHour = normalizeLength(String.valueOf(LocalTime.now().getHour()), 2, true, '0');
- String pathS = date + (separateHours ? "/" : "_") + currentHour;
+ String pathS;
+ if (options.contains(WITH_HOURS)) {
+ boolean separateHours = options.contains(SEPARATE_HOURS);
+ String currentHour = normalizeLength(String.valueOf(LocalTime.now().getHour()), 2, true, '0');
+ pathS = date + (separateHours ? "/" : "_") + currentHour;
+ } else {
+ pathS = date;
+ }
File path = new File(tempPath, pathS);
if (!path.exists() && !path.mkdirs())
throw new IllegalStateException("Failed to create path " + path.getAbsolutePath());
+ boolean appendMillis = options.contains(APPEND_MILLIS);
if (appendMillis)
prefix = (isEmpty(prefix) ? "" : prefix + "_");
else
diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/helper/TempFileOptions.java b/li.strolch.utils/src/main/java/li/strolch/utils/helper/TempFileOptions.java
new file mode 100644
index 000000000..318843529
--- /dev/null
+++ b/li.strolch.utils/src/main/java/li/strolch/utils/helper/TempFileOptions.java
@@ -0,0 +1,14 @@
+package li.strolch.utils.helper;
+
+import java.io.File;
+
+/**
+ * The options to create the path for a temporary file using {@link FileHelper#getTempFile(File, String, String, TempFileOptions)}
+ */
+public enum TempFileOptions {
+ NONE,
+ SEPARATE_DATE_SEGMENTS,
+ WITH_HOURS,
+ SEPARATE_HOURS,
+ APPEND_MILLIS
+}
diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/helper/XmlHelper.java b/li.strolch.utils/src/main/java/li/strolch/utils/helper/XmlHelper.java
index 11b637d50..b8c637be8 100644
--- a/li.strolch.utils/src/main/java/li/strolch/utils/helper/XmlHelper.java
+++ b/li.strolch.utils/src/main/java/li/strolch/utils/helper/XmlHelper.java
@@ -15,6 +15,8 @@
*/
package li.strolch.utils.helper;
+import static li.strolch.utils.helper.FileHelper.getTempFile;
+
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
@@ -29,6 +31,7 @@ import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.nio.file.Files;
import java.text.MessageFormat;
+import java.util.Set;
import li.strolch.utils.RemoveCRFilterWriter;
import li.strolch.utils.exceptions.XmlException;
@@ -290,8 +293,8 @@ public class XmlHelper {
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
transformer.setOutputProperty(OutputKeys.ENCODING, docEncoding);
- transformer
- .setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$
+ transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount",
+ "2"); //$NON-NLS-1$ //$NON-NLS-2$
// transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\t");
// Transform to file
@@ -329,11 +332,9 @@ public class XmlHelper {
}
}
- public static void marshallTempFile(File tempPath, String prefix, boolean separateDateSegments,
- boolean separateHours, Object object) throws Exception {
-
- File dataFile = FileHelper.getTempFile(tempPath, prefix, ".xml", separateDateSegments, separateHours);
- marshall(dataFile, object);
+ public static void marshallTempFile(File tempPath, String prefix, Set options, Object object)
+ throws Exception {
+ marshall(getTempFile(tempPath, prefix, ".xml", options), object);
}
/**
diff --git a/li.strolch.utils/src/test/java/li/strolch/utils/helper/FileHelperTest.java b/li.strolch.utils/src/test/java/li/strolch/utils/helper/FileHelperTest.java
new file mode 100644
index 000000000..2dc234288
--- /dev/null
+++ b/li.strolch.utils/src/test/java/li/strolch/utils/helper/FileHelperTest.java
@@ -0,0 +1,66 @@
+package li.strolch.utils.helper;
+
+import static li.strolch.utils.helper.FileHelper.*;
+import static li.strolch.utils.helper.StringHelper.*;
+import static li.strolch.utils.helper.TempFileOptions.*;
+import static org.junit.Assert.assertEquals;
+
+import java.io.File;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.Set;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class FileHelperTest {
+
+ private static File tempPath;
+
+ @BeforeClass
+ public static void beforeClass() {
+ tempPath = new File(System.getProperty("java.io.tmpdir"));
+ }
+
+ @Test
+ public void shouldCreateTempFileNoOptions() {
+ File path = getTempFile(tempPath, "NoOptions", ".txt");
+ LocalDate today = LocalDate.now();
+ String expected = today + "/NoOptions.txt";
+ assertEquals(new File(tempPath, expected), path);
+ }
+
+ @Test
+ public void shouldCreateTempFileSeparateDateSegments() {
+ File path = getTempFile(tempPath, "SeparateDateSegments", ".txt", Set.of(SEPARATE_DATE_SEGMENTS));
+ LocalDate today = LocalDate.now();
+ String expected =
+ today.getYear() + "/" + normalizeLength(String.valueOf(today.getMonthValue()), 2, true, '0') + "/"
+ + normalizeLength(String.valueOf(today.getDayOfMonth()), 2, true, '0')
+ + "/SeparateDateSegments.txt";
+ assertEquals(new File(tempPath, expected), path);
+ }
+
+ @Test
+ public void shouldCreateTempFileSeparateDateSegments_WithHours() {
+ File path = getTempFile(tempPath, "SeparateDateSegments", ".txt", Set.of(SEPARATE_DATE_SEGMENTS, WITH_HOURS));
+ LocalDateTime today = LocalDateTime.now();
+ String expected =
+ today.getYear() + "/" + normalizeLength(String.valueOf(today.getMonthValue()), 2, true, '0') + "/"
+ + normalizeLength(String.valueOf(today.getDayOfMonth()), 2, true, '0') + "_" + normalizeLength(
+ String.valueOf(today.getHour()), 2, true, '0') + "/SeparateDateSegments.txt";
+ assertEquals(new File(tempPath, expected), path);
+ }
+
+ @Test
+ public void shouldCreateTempFileSeparateDateSegments_WithHours_SeparateHours() {
+ File path = getTempFile(tempPath, "SeparateDateSegments", ".txt",
+ Set.of(SEPARATE_DATE_SEGMENTS, WITH_HOURS, SEPARATE_HOURS));
+ LocalDateTime today = LocalDateTime.now();
+ String expected =
+ today.getYear() + "/" + normalizeLength(String.valueOf(today.getMonthValue()), 2, true, '0') + "/"
+ + normalizeLength(String.valueOf(today.getDayOfMonth()), 2, true, '0') + "/" + normalizeLength(
+ String.valueOf(today.getHour()), 2, true, '0') + "/SeparateDateSegments.txt";
+ assertEquals(new File(tempPath, expected), path);
+ }
+}