[New] Added new FileHelper.getTempFile() option MILLIS_FIRST, code cleanup
This commit is contained in:
parent
a07bd83249
commit
f3adc63af2
|
@ -82,6 +82,7 @@ public class FileHelper {
|
|||
* <li>{@link TempFileOptions#SEPARATE_DATE_SEGMENTS}</li>
|
||||
* <li>{@link TempFileOptions#SEPARATE_HOURS}</li>
|
||||
* <li>{@link TempFileOptions#WITH_HOURS}</li>
|
||||
* <li>{@link TempFileOptions#MILLIS_FIRST}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param tempPath the directory to which to write the file
|
||||
|
@ -92,8 +93,14 @@ public class FileHelper {
|
|||
* @return the temporary file
|
||||
*/
|
||||
public static File getTempFile(File tempPath, String prefix, String suffix, Set<TempFileOptions> options) {
|
||||
return getTempFile(tempPath, prefix, suffix, options, LocalDateTime.now(), System.currentTimeMillis());
|
||||
}
|
||||
|
||||
static File getTempFile(File tempPath, String prefix, String suffix, Set<TempFileOptions> options,
|
||||
LocalDateTime dateTime, long timestamp) {
|
||||
prefix = StringHelper.trimOrEmpty(prefix);
|
||||
suffix = StringHelper.trimOrEmpty(suffix);
|
||||
|
||||
LocalDateTime dateTime = LocalDateTime.now();
|
||||
LocalDate localDate = dateTime.toLocalDate();
|
||||
|
||||
boolean separateDateSegments = options.contains(SEPARATE_DATE_SEGMENTS);
|
||||
|
@ -113,21 +120,22 @@ public class FileHelper {
|
|||
throw new IllegalStateException("Failed to create path " + path.getAbsolutePath());
|
||||
|
||||
boolean appendMillis = options.contains(APPEND_MILLIS);
|
||||
if (appendMillis)
|
||||
prefix = (isEmpty(prefix) ? "" : prefix + "_");
|
||||
else
|
||||
prefix = (isEmpty(prefix) ? "" : prefix);
|
||||
boolean millisFirst = appendMillis && options.contains(MILLIS_FIRST);
|
||||
if (appendMillis) {
|
||||
if (isEmpty(prefix)) {
|
||||
prefix = String.valueOf(timestamp);
|
||||
} else {
|
||||
if (millisFirst)
|
||||
prefix = timestamp + "_" + prefix;
|
||||
else
|
||||
prefix = prefix + "_" + timestamp;
|
||||
}
|
||||
|
||||
suffix = (isEmpty(suffix) ? "" : suffix);
|
||||
if (!suffix.startsWith(".") && appendMillis)
|
||||
suffix = "_" + suffix;
|
||||
|
||||
String fileName;
|
||||
if (appendMillis)
|
||||
fileName = prefix + System.currentTimeMillis() + suffix;
|
||||
else
|
||||
fileName = prefix + suffix;
|
||||
if (!suffix.startsWith("."))
|
||||
prefix = prefix + "_";
|
||||
}
|
||||
|
||||
String fileName = prefix + suffix;
|
||||
return new File(path, fileName);
|
||||
}
|
||||
|
||||
|
|
|
@ -10,5 +10,6 @@ public enum TempFileOptions {
|
|||
SEPARATE_DATE_SEGMENTS,
|
||||
WITH_HOURS,
|
||||
SEPARATE_HOURS,
|
||||
APPEND_MILLIS
|
||||
APPEND_MILLIS,
|
||||
MILLIS_FIRST
|
||||
}
|
||||
|
|
|
@ -6,14 +6,18 @@ import static li.strolch.utils.helper.TempFileOptions.*;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.File;
|
||||
import java.text.MessageFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class FileHelperTest {
|
||||
private static final Logger logger = LoggerFactory.getLogger(FileHelperTest.class);
|
||||
|
||||
private static File tempPath;
|
||||
|
||||
|
@ -25,6 +29,8 @@ public class FileHelperTest {
|
|||
@Test
|
||||
public void shouldCreateTempFileNoOptions() {
|
||||
File path = getTempFile(tempPath, "NoOptions", ".txt");
|
||||
logger.info("Temp path: " + path.getAbsolutePath());
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
String expected = today + "/NoOptions.txt";
|
||||
assertEquals(new File(tempPath, expected), path);
|
||||
|
@ -32,35 +38,82 @@ public class FileHelperTest {
|
|||
|
||||
@Test
|
||||
public void shouldCreateTempFileSeparateDateSegments() {
|
||||
File path = getTempFile(tempPath, "SeparateDateSegments", ".txt", Set.of(SEPARATE_DATE_SEGMENTS));
|
||||
Set<TempFileOptions> options = Set.of(SEPARATE_DATE_SEGMENTS);
|
||||
File path = getTempFile(tempPath, "SeparateDateSegments", ".txt", options);
|
||||
logger.info("Temp path: " + path.getAbsolutePath());
|
||||
|
||||
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";
|
||||
String month = normalizeLength(String.valueOf(today.getMonthValue()), 2, true, '0');
|
||||
String day = normalizeLength(String.valueOf(today.getDayOfMonth()), 2, true, '0');
|
||||
String expected = MessageFormat.format("{0,number,#}/{1}/{2}/SeparateDateSegments.txt", today.getYear(), month,
|
||||
day);
|
||||
assertEquals(new File(tempPath, expected), path);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateTempFileSeparateDateSegments_WithHours() {
|
||||
File path = getTempFile(tempPath, "SeparateDateSegments", ".txt", Set.of(SEPARATE_DATE_SEGMENTS, WITH_HOURS));
|
||||
Set<TempFileOptions> options = Set.of(SEPARATE_DATE_SEGMENTS, WITH_HOURS);
|
||||
File path = getTempFile(tempPath, "SeparateDateSegments", ".txt", options);
|
||||
logger.info("Temp path: " + path.getAbsolutePath());
|
||||
|
||||
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";
|
||||
String month = normalizeLength(String.valueOf(today.getMonthValue()), 2, true, '0');
|
||||
String day = normalizeLength(String.valueOf(today.getDayOfMonth()), 2, true, '0');
|
||||
String hour = normalizeLength(String.valueOf(today.getHour()), 2, true, '0');
|
||||
String expected = MessageFormat.format("{0,number,#}/{1}/{2}_{3}/SeparateDateSegments.txt", today.getYear(),
|
||||
month, day, hour);
|
||||
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));
|
||||
Set<TempFileOptions> options = Set.of(SEPARATE_DATE_SEGMENTS, WITH_HOURS, SEPARATE_HOURS);
|
||||
File path = getTempFile(tempPath, "SeparateDateSegments", ".txt", options);
|
||||
logger.info("Temp path: " + path.getAbsolutePath());
|
||||
|
||||
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";
|
||||
String month = normalizeLength(String.valueOf(today.getMonthValue()), 2, true, '0');
|
||||
String day = normalizeLength(String.valueOf(today.getDayOfMonth()), 2, true, '0');
|
||||
String hour = normalizeLength(String.valueOf(today.getHour()), 2, true, '0');
|
||||
String expected = MessageFormat.format("{0,number,#}/{1}/{2}/{3}/SeparateDateSegments.txt", today.getYear(),
|
||||
month, day, hour);
|
||||
assertEquals(new File(tempPath, expected), path);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateTempFileSeparateDateSegments_WithHours_SeparateHoursWithMillis() {
|
||||
LocalDateTime today = LocalDateTime.now();
|
||||
long timestamp = System.currentTimeMillis();
|
||||
|
||||
Set<TempFileOptions> options = Set.of(SEPARATE_DATE_SEGMENTS, WITH_HOURS, SEPARATE_HOURS, APPEND_MILLIS);
|
||||
File path = getTempFile(tempPath, "SeparateDateSegments", ".txt", options, today, timestamp);
|
||||
logger.info("Temp path: " + path.getAbsolutePath());
|
||||
|
||||
String month = normalizeLength(String.valueOf(today.getMonthValue()), 2, true, '0');
|
||||
String day = normalizeLength(String.valueOf(today.getDayOfMonth()), 2, true, '0');
|
||||
String hour = normalizeLength(String.valueOf(today.getHour()), 2, true, '0');
|
||||
int year = today.getYear();
|
||||
String expected = MessageFormat.format("{0,number,#}/{1}/{2}/{3}/SeparateDateSegments_{4,number,#}.txt", year,
|
||||
month, day, hour, timestamp);
|
||||
assertEquals(new File(tempPath, expected), path);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateTempFileSeparateDateSegments_WithHours_SeparateHoursWithMillisFirst() {
|
||||
LocalDateTime today = LocalDateTime.now();
|
||||
long timestamp = System.currentTimeMillis();
|
||||
|
||||
Set<TempFileOptions> options = Set.of(SEPARATE_DATE_SEGMENTS, WITH_HOURS, SEPARATE_HOURS, APPEND_MILLIS,
|
||||
MILLIS_FIRST);
|
||||
File path = getTempFile(tempPath, "SeparateDateSegments", ".txt", options, today, timestamp);
|
||||
logger.info("Temp path: " + path.getAbsolutePath());
|
||||
|
||||
String month = normalizeLength(String.valueOf(today.getMonthValue()), 2, true, '0');
|
||||
String day = normalizeLength(String.valueOf(today.getDayOfMonth()), 2, true, '0');
|
||||
String hour = normalizeLength(String.valueOf(today.getHour()), 2, true, '0');
|
||||
int year = today.getYear();
|
||||
String expected = MessageFormat.format("{0,number,#}/{1}/{2}/{3}/{4,number,#}_SeparateDateSegments.txt", year,
|
||||
month, day, hour, timestamp);
|
||||
assertEquals(new File(tempPath, expected), path);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue