Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time values #147

Merged
merged 1 commit into from Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions httpcore5/src/main/java/org/apache/hc/core5/util/TimeValue.java
Expand Up @@ -41,7 +41,7 @@
* @since 5.0
*/
@Contract(threading = ThreadingBehavior.IMMUTABLE)
public class TimeValue {
public class TimeValue implements Comparable<TimeValue> {

static final int INT_UNDEFINED = -1;

Expand Down Expand Up @@ -217,6 +217,7 @@ public static TimeValue parse(final String value) throws ParseException {
}

public long convert(final TimeUnit targetTimeUnit) {
Args.notNull(targetTimeUnit, "timeUnit");
return targetTimeUnit.convert(duration, timeUnit);
}

Expand Down Expand Up @@ -278,12 +279,7 @@ public int hashCode() {
}

public TimeValue min(final TimeValue other) {
return isGreaterThan(other) ? other : this;
}

private boolean isGreaterThan(final TimeValue other) {
final TimeUnit targetTimeUnit = min(other.getTimeUnit());
return convert(targetTimeUnit) > other.convert(targetTimeUnit);
return this.compareTo(other) > 0 ? other : this;
}

private TimeUnit min(final TimeUnit other) {
Expand Down Expand Up @@ -371,6 +367,12 @@ public int toSecondsIntBound() {
return asBoundInt(toSeconds());
}

@Override
public int compareTo(final TimeValue other) {
final TimeUnit targetTimeUnit = min(other.getTimeUnit());
return Long.compare(convert(targetTimeUnit), other.convert(targetTimeUnit));
}

@Override
public String toString() {
return String.format(Locale.ROOT, "%,d %s", duration, timeUnit);
michael-o marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Expand Up @@ -30,6 +30,7 @@
import java.text.ParseException;
import java.util.concurrent.TimeUnit;

import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -262,4 +263,48 @@ public void testFromString() throws ParseException {
Assert.assertEquals(TimeValue.ofMilliseconds(1), TimeValue.parse("1 MILLISECOND"));
}

@Test
public void testEqualsAndHashCode() throws ParseException {
final TimeValue tv1 = TimeValue.ofMilliseconds(1000L);
final TimeValue tv2 = TimeValue.ofMilliseconds(1001L);
final TimeValue tv3 = TimeValue.ofMilliseconds(1000L);
final TimeValue tv4 = TimeValue.ofSeconds(1L);
final TimeValue tv5 = TimeValue.ofSeconds(1000L);

Assert.assertThat(tv1.equals(tv1), CoreMatchers.equalTo(true));
Assert.assertThat(tv1.equals(null), CoreMatchers.equalTo(false));
Assert.assertThat(tv1.equals(tv2), CoreMatchers.equalTo(false));
Assert.assertThat(tv1.equals(tv3), CoreMatchers.equalTo(true));
Assert.assertThat(tv1.equals(tv4), CoreMatchers.equalTo(false));
Assert.assertThat(tv1.equals(tv5), CoreMatchers.equalTo(false));

Assert.assertThat(tv1.hashCode() == tv2.hashCode(), CoreMatchers.equalTo(false));
Assert.assertThat(tv1.hashCode() == tv3.hashCode(), CoreMatchers.equalTo(true));
Assert.assertThat(tv1.hashCode() == tv4.hashCode(), CoreMatchers.equalTo(false));
Assert.assertThat(tv1.hashCode() == tv5.hashCode(), CoreMatchers.equalTo(false));
}

@Test
public void testCompareTo() throws ParseException {
final TimeValue tv1 = TimeValue.ofMilliseconds(1000L);
final TimeValue tv2 = TimeValue.ofMilliseconds(1001L);
final TimeValue tv3 = TimeValue.ofMilliseconds(1000L);
final TimeValue tv4 = TimeValue.ofSeconds(1L);
final TimeValue tv5 = TimeValue.ofSeconds(60L);
final TimeValue tv6 = TimeValue.ofMinutes(1L);

Assert.assertThat(tv1.compareTo(tv1) == 0, CoreMatchers.equalTo(true));
michael-o marked this conversation as resolved.
Show resolved Hide resolved
Assert.assertThat(tv1.compareTo(tv2) < 0, CoreMatchers.equalTo(true));
Assert.assertThat(tv1.compareTo(tv3) == 0, CoreMatchers.equalTo(true));
Assert.assertThat(tv1.compareTo(tv4) == 0, CoreMatchers.equalTo(true));
Assert.assertThat(tv1.compareTo(tv5) < 0, CoreMatchers.equalTo(true));
Assert.assertThat(tv6.compareTo(tv5) == 0, CoreMatchers.equalTo(true));
Assert.assertThat(tv6.compareTo(tv4) > 0, CoreMatchers.equalTo(true));
try {
tv1.compareTo(null);
Assert.fail("NullPointerException expected");
} catch (final NullPointerException expected) {
}
}

}