You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2021/11/10 14:13:34 UTC
[httpcomponents-core] branch master updated: Fixed #format in
Deadline; improved #hashCode;
replaced SimpleDatteFormat with Java 8 Time APIs
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
The following commit(s) were added to refs/heads/master by this push:
new 5b6f3dd Fixed #format in Deadline; improved #hashCode; replaced SimpleDatteFormat with Java 8 Time APIs
5b6f3dd is described below
commit 5b6f3dd75ea0d05bff5ad9ae18f9fe7f40640867
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Wed Nov 10 15:01:52 2021 +0100
Fixed #format in Deadline; improved #hashCode; replaced SimpleDatteFormat with Java 8 Time APIs
---
.../java/org/apache/hc/core5/util/Deadline.java | 34 ++++++++++++++--------
.../org/apache/hc/core5/util/TestDeadline.java | 2 +-
2 files changed, 23 insertions(+), 13 deletions(-)
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/util/Deadline.java b/httpcore5/src/main/java/org/apache/hc/core5/util/Deadline.java
index ad88bd2..851a9a8 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/util/Deadline.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/util/Deadline.java
@@ -28,8 +28,10 @@
package org.apache.hc.core5.util;
import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Objects;
+import java.time.Instant;
+import java.time.ZoneOffset;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeFormatterBuilder;
import java.util.concurrent.TimeUnit;
/**
@@ -65,7 +67,11 @@ public class Deadline {
*/
public static Deadline MIN_VALUE = new Deadline(INTERNAL_MIN_VALUE);
- private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
+ private static final DateTimeFormatter DATE_TIME_FORMATTER = new DateTimeFormatterBuilder()
+ .parseLenient()
+ .parseCaseInsensitive()
+ .appendPattern(DATE_FORMAT)
+ .toFormatter();
/**
* Calculates a deadline with a given time in milliseconds plus a given time value. Non-positive time values
@@ -119,7 +125,11 @@ public class Deadline {
* @throws ParseException if the specified source string cannot be parsed.
*/
public static Deadline parse(final String source) throws ParseException {
- return fromUnixMilliseconds(simpleDateFormat.parse(source).getTime());
+ if (source == null) {
+ return null;
+ }
+ final Instant instant = Instant.from(DATE_TIME_FORMATTER.parse(source));
+ return fromUnixMilliseconds(instant.toEpochMilli());
}
private volatile boolean frozen;
@@ -158,6 +168,12 @@ public class Deadline {
return value == other.value;
}
+ @Override
+ public int hashCode() {
+ // Only take into account the deadline value.
+ return Long.hashCode(value);
+ }
+
/**
* Formats this deadline.
*
@@ -165,7 +181,7 @@ public class Deadline {
* @return a formatted string.
*/
public String format(final TimeUnit overdueTimeUnit) {
- return String.format("Deadline: %s, %s overdue", formatTarget(), remainingTimeValue());
+ return String.format("Deadline: %s, %s overdue", formatTarget(), TimeValue.of(remaining(), overdueTimeUnit));
}
/**
@@ -174,7 +190,7 @@ public class Deadline {
* @return a formatted string in the format {@value #DATE_FORMAT}.
*/
public String formatTarget() {
- return simpleDateFormat.format(value);
+ return DATE_TIME_FORMATTER.format(Instant.ofEpochMilli(value).atOffset(ZoneOffset.UTC));
}
public Deadline freeze() {
@@ -200,12 +216,6 @@ public class Deadline {
return value;
}
- @Override
- public int hashCode() {
- // Only take into account the deadline value.
- return Objects.hash(value);
- }
-
/**
* Returns whether this deadline occurs before the given time in milliseconds.
*
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/util/TestDeadline.java b/httpcore5/src/test/java/org/apache/hc/core5/util/TestDeadline.java
index 6f0f0b7..d6b1d11 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/util/TestDeadline.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/util/TestDeadline.java
@@ -99,7 +99,7 @@ public class TestDeadline {
@Test
public void testParse() throws ParseException {
- final Deadline deadline = Deadline.parse("1969-12-31T17:00:01.000 MST");
+ final Deadline deadline = Deadline.parse("1969-12-31T17:00:01.000-0700");
Assert.assertEquals(1000, deadline.getValue());
}