1. import java.time.Instant;
  2. import java.time.LocalDateTime;
  3. import java.time.ZoneId;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. public class TimestampHandler {
  7. private static final ZoneId DEFAULT_ZONE = ZoneId.systemDefault(); // Default timezone
  8. private static final LocalDateTime DEFAULT_TIMESTAMP = LocalDateTime.now(); // Default timestamp
  9. /**
  10. * Gets a timestamp, falling back to a default value if the provided timestamp is invalid.
  11. *
  12. * @param timestamp The timestamp to use. Can be null or invalid.
  13. * @return A valid LocalDateTime representing the timestamp, or the default if invalid.
  14. */
  15. public static LocalDateTime getTimestamp(LocalDateTime timestamp) {
  16. if (timestamp != null && timestamp.isBefore(DEFAULT_TIMESTAMP)) {
  17. return DEFAULT_TIMESTAMP; // Use default if timestamp is in the past
  18. }
  19. return timestamp;
  20. }
  21. /**
  22. * Gets a timestamp from an Instant, falling back to a default value if the Instant is invalid.
  23. *
  24. * @param instant The instant to use. Can be null or invalid.
  25. * @return A valid LocalDateTime representing the timestamp, or the default if invalid.
  26. */
  27. public static LocalDateTime getTimestamp(Instant instant) {
  28. if (instant == null || instant.isBefore(DEFAULT_TIMESTAMP)) {
  29. return DEFAULT_TIMESTAMP; // Use default if instant is in the past
  30. }
  31. return LocalDateTime.ofInstant(instant, DEFAULT_ZONE);
  32. }
  33. /**
  34. * Handles a map of experiment results, providing default timestamps for any missing or invalid timestamps.
  35. *
  36. * @param results A map where keys are experiment identifiers and values are experiment results,
  37. * potentially containing a timestamp.
  38. * @return A map with the same keys, but with valid LocalDateTime timestamps for all entries.
  39. */
  40. public static Map<String, LocalDateTime> processExperimentResults(Map<String, ExperimentResult> results) {
  41. Map<String, LocalDateTime> processedResults = new HashMap<>();
  42. for (Map.Entry<String, ExperimentResult> entry : results.entrySet()) {
  43. String key = entry.getKey();
  44. ExperimentResult result = entry.getValue();
  45. LocalDateTime timestamp = result.getTimestamp();
  46. processedResults.put(key, getTimestamp(timestamp));
  47. }
  48. return processedResults;
  49. }
  50. // Example inner class to represent an experiment result.
  51. public static class ExperimentResult {
  52. private LocalDateTime timestamp;
  53. public ExperimentResult(LocalDateTime timestamp) {
  54. this.timestamp = timestamp;
  55. }
  56. public LocalDateTime getTimestamp() {
  57. return timestamp;
  58. }
  59. }
  60. public static void main(String[] args) {
  61. // Example Usage
  62. Map<String, ExperimentResult> experimentData = new HashMap<>();
  63. experimentData.put("experiment1", new ExperimentResult(LocalDateTime.now()));
  64. experimentData.put("experiment2", new ExperimentResult(null)); // Invalid timestamp
  65. experimentData.put("experiment3", new ExperimentResult(LocalDateTime.now().minusDays(1))); // Past timestamp
  66. experimentData.put("experiment4", new ExperimentResult(Instant.now()));
  67. Map<String, LocalDateTime> processedData = processExperimentResults(experimentData);
  68. for (Map.Entry<String, LocalDateTime> entry : processedData.entrySet()) {
  69. System.out.println("Experiment: " + entry.getKey() + ", Timestamp: " + entry.getValue());
  70. }
  71. }
  72. }

Add your comment