Skip to content

Commit 97dcb46

Browse files
committed
Simpler Duration.toString impl. Formatting.
1 parent e606b3f commit 97dcb46

File tree

2 files changed

+29
-42
lines changed

2 files changed

+29
-42
lines changed

src/main/java/net/jodah/failsafe/util/Duration.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,19 @@ public Duration(long length, TimeUnit timeUnit) {
1919
this.timeUnit = timeUnit;
2020
}
2121

22-
@Override public boolean equals(Object o)
23-
{
24-
return this == o
25-
|| Duration.class.isInstance(o)
26-
&& toNanos() == Duration.class.cast(o).toNanos();
22+
@Override
23+
public boolean equals(Object o) {
24+
return this == o || Duration.class.isInstance(o) && toNanos() == Duration.class.cast(o).toNanos();
2725
}
2826

29-
@Override public int hashCode()
30-
{
31-
return Arrays.hashCode(new Object[] {toNanos(), NANOSECONDS});
27+
@Override
28+
public int hashCode() {
29+
return Arrays.hashCode(new Object[] { toNanos(), NANOSECONDS });
3230
}
3331

34-
@Override public String toString()
35-
{
36-
return "Duration{" + "length=" + length + ", timeUnit=" + timeUnit + '}';
32+
@Override
33+
public String toString() {
34+
return length + " " + timeUnit.toString().toLowerCase();
3735
}
3836

3937
/**

src/test/java/net/jodah/failsafe/util/DurationTest.java

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,30 @@
99

1010
package net.jodah.failsafe.util;
1111

12-
import org.testng.annotations.Test;
13-
1412
import static java.util.concurrent.TimeUnit.MILLISECONDS;
1513
import static java.util.concurrent.TimeUnit.SECONDS;
16-
1714
import static org.testng.Assert.assertEquals;
1815
import static org.testng.Assert.assertNotEquals;
16+
import static org.testng.Assert.assertTrue;
1917

20-
@Test public class DurationTest
21-
{
22-
23-
//~ Methods ----------------------------------------------------------------
24-
25-
public void testEquals()
26-
throws Exception
27-
{
28-
assertEquals(new Duration(30000, MILLISECONDS),
29-
new Duration(30, SECONDS));
30-
assertNotEquals(new Duration(30, MILLISECONDS),
31-
new Duration(30, SECONDS));
32-
}
33-
34-
@Test public void testHashCode()
35-
throws Exception
36-
{
37-
assertEquals(new Duration(30000, MILLISECONDS).hashCode(),
38-
new Duration(30, SECONDS).hashCode());
39-
assertNotEquals(new Duration(30, MILLISECONDS).hashCode(),
40-
new Duration(30, SECONDS).hashCode());
41-
}
18+
import org.testng.annotations.Test;
4219

43-
@Test public void testToString()
44-
throws Exception
45-
{
46-
assertEquals(new Duration(30, SECONDS).toString(),
47-
"Duration{length=30, timeUnit=SECONDS}");
48-
}
20+
@Test
21+
public class DurationTest {
22+
public void testEquals() throws Exception {
23+
assertTrue(!new Duration(30, SECONDS).equals(null));
24+
assertEquals(new Duration(30000, MILLISECONDS), new Duration(30, SECONDS));
25+
assertNotEquals(new Duration(30, MILLISECONDS), new Duration(30, SECONDS));
26+
}
27+
28+
@Test
29+
public void testHashCode() throws Exception {
30+
assertEquals(new Duration(30000, MILLISECONDS).hashCode(), new Duration(30, SECONDS).hashCode());
31+
assertNotEquals(new Duration(30, MILLISECONDS).hashCode(), new Duration(30, SECONDS).hashCode());
32+
}
33+
34+
@Test
35+
public void testToString() throws Exception {
36+
assertEquals(new Duration(30, SECONDS).toString(), "30 seconds");
37+
}
4938
}

0 commit comments

Comments
 (0)