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