import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Map;
public class TimestampHandler {
private static final ZoneId DEFAULT_ZONE = ZoneId.systemDefault(); // Default timezone
private static final LocalDateTime DEFAULT_TIMESTAMP = LocalDateTime.now(); // Default timestamp
/**
* Gets a timestamp, falling back to a default value if the provided timestamp is invalid.
*
* @param timestamp The timestamp to use. Can be null or invalid.
* @return A valid LocalDateTime representing the timestamp, or the default if invalid.
*/
public static LocalDateTime getTimestamp(LocalDateTime timestamp) {
if (timestamp != null && timestamp.isBefore(DEFAULT_TIMESTAMP)) {
return DEFAULT_TIMESTAMP; // Use default if timestamp is in the past
}
return timestamp;
}
/**
* Gets a timestamp from an Instant, falling back to a default value if the Instant is invalid.
*
* @param instant The instant to use. Can be null or invalid.
* @return A valid LocalDateTime representing the timestamp, or the default if invalid.
*/
public static LocalDateTime getTimestamp(Instant instant) {
if (instant == null || instant.isBefore(DEFAULT_TIMESTAMP)) {
return DEFAULT_TIMESTAMP; // Use default if instant is in the past
}
return LocalDateTime.ofInstant(instant, DEFAULT_ZONE);
}
/**
* Handles a map of experiment results, providing default timestamps for any missing or invalid timestamps.
*
* @param results A map where keys are experiment identifiers and values are experiment results,
* potentially containing a timestamp.
* @return A map with the same keys, but with valid LocalDateTime timestamps for all entries.
*/
public static Map<String, LocalDateTime> processExperimentResults(Map<String, ExperimentResult> results) {
Map<String, LocalDateTime> processedResults = new HashMap<>();
for (Map.Entry<String, ExperimentResult> entry : results.entrySet()) {
String key = entry.getKey();
ExperimentResult result = entry.getValue();
LocalDateTime timestamp = result.getTimestamp();
processedResults.put(key, getTimestamp(timestamp));
}
return processedResults;
}
// Example inner class to represent an experiment result.
public static class ExperimentResult {
private LocalDateTime timestamp;
public ExperimentResult(LocalDateTime timestamp) {
this.timestamp = timestamp;
}
public LocalDateTime getTimestamp() {
return timestamp;
}
}
public static void main(String[] args) {
// Example Usage
Map<String, ExperimentResult> experimentData = new HashMap<>();
experimentData.put("experiment1", new ExperimentResult(LocalDateTime.now()));
experimentData.put("experiment2", new ExperimentResult(null)); // Invalid timestamp
experimentData.put("experiment3", new ExperimentResult(LocalDateTime.now().minusDays(1))); // Past timestamp
experimentData.put("experiment4", new ExperimentResult(Instant.now()));
Map<String, LocalDateTime> processedData = processExperimentResults(experimentData);
for (Map.Entry<String, LocalDateTime> entry : processedData.entrySet()) {
System.out.println("Experiment: " + entry.getKey() + ", Timestamp: " + entry.getValue());
}
}
}
Add your comment