Skip to content

Commit d9f81b3

Browse files
committed
Add TimeUnits, re #57
1 parent d461127 commit d9f81b3

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* Copyright (c) 2024 LibJ
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy
4+
* of this software and associated documentation files (the "Software"), to deal
5+
* in the Software without restriction, including without limitation the rights
6+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
* copies of the Software, and to permit persons to whom the Software is
8+
* furnished to do so, subject to the following conditions:
9+
*
10+
* The above copyright notice and this permission notice shall be included in
11+
* all copies or substantial portions of the Software.
12+
*
13+
* You should have received a copy of The MIT License (MIT) along with this
14+
* program. If not, see <http://opensource.org/licenses/MIT/>.
15+
*/
16+
17+
package org.libj.util.concurrent;
18+
19+
import java.util.concurrent.TimeUnit;
20+
21+
/**
22+
* Utility functions for operations pertaining to the {@link java.time} package.
23+
*/
24+
public class TimeUnits {
25+
/**
26+
* Compares two pairs of time values represented by a {@code long} scalar at a {@link TimeUnit}. Returns a negative integer, zero,
27+
* or a positive integer as the first time value object is less than, equal to, or greater than the second time value.
28+
*
29+
* @param time0 The scalar of first time value.
30+
* @param unit0 The {@link TimeUnit} of the first time value.
31+
* @param time1 The scalar of second time value.
32+
* @param unit1 The {@link TimeUnit} of the second time value.
33+
* @return A negative integer, zero, or a positive integer as the first time value object is less than, equal to, or greater than
34+
* the second time value.
35+
* @throws NullPointerException If {@code unit0} or {@code unit1} is null.
36+
*/
37+
public static int compare(long time0, final TimeUnit unit0, long time1, final TimeUnit unit1) {
38+
final int sign;
39+
if (time0 < 0) {
40+
if (time1 >= 0)
41+
return -1;
42+
43+
sign = -1;
44+
}
45+
else if (time0 > 0) {
46+
if (time1 <= 0)
47+
return 1;
48+
49+
sign = 1;
50+
}
51+
else {
52+
return time1 == 0 ? 0 : time1 > 0 ? -1 : 1;
53+
}
54+
55+
if (unit0 == unit1)
56+
return sign * Long.compare(time0, time1);
57+
58+
if (unit0.compareTo(unit1) < 0)
59+
time0 /= unit0.convert(1, unit1);
60+
else
61+
time1 /= unit1.convert(1, unit0);
62+
63+
return sign * Long.compare(time0, time1);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.libj.util.concurrent;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.util.concurrent.TimeUnit;
6+
7+
import org.junit.Test;
8+
9+
public class TimeUnitsTest {
10+
private static final long[] values = {Long.MIN_VALUE, Integer.MIN_VALUE, Short.MIN_VALUE, Byte.MIN_VALUE, -1, 1, Byte.MAX_VALUE, Short.MAX_VALUE, Integer.MAX_VALUE, Long.MAX_VALUE};
11+
12+
private static void test(final TimeUnit unit0, final TimeUnit unit1) {
13+
assertTrue(unit0.compareTo(unit1) < 0);
14+
final long time1 = 1;
15+
final long time0 = unit0.convert(time1, unit1);
16+
assertEquals(0, TimeUnits.compare(time0, unit0, time1, unit1));
17+
assertEquals(0, TimeUnits.compare(time1, unit1, time0, unit0));
18+
for (long value : values) {
19+
assertEquals(1, TimeUnits.compare(value, unit1, value, unit0));
20+
assertEquals(-1, TimeUnits.compare(value, unit0, value, unit1));
21+
}
22+
}
23+
24+
@Test
25+
public void test() {
26+
assertEquals(0, TimeUnits.compare(Long.MAX_VALUE, TimeUnit.MILLISECONDS, Long.MAX_VALUE, TimeUnit.MILLISECONDS));
27+
assertEquals(0, TimeUnits.compare(Long.MAX_VALUE, TimeUnit.MICROSECONDS, Long.MAX_VALUE, TimeUnit.MICROSECONDS));
28+
assertEquals(0, TimeUnits.compare(Long.MAX_VALUE, TimeUnit.NANOSECONDS, Long.MAX_VALUE, TimeUnit.NANOSECONDS));
29+
30+
assertEquals(0, TimeUnits.compare(Long.MAX_VALUE, TimeUnit.SECONDS, Long.MAX_VALUE, TimeUnit.SECONDS));
31+
assertEquals(1, TimeUnits.compare(Long.MAX_VALUE, TimeUnit.SECONDS, Long.MAX_VALUE - 1, TimeUnit.SECONDS));
32+
assertEquals(-1, TimeUnits.compare(Long.MAX_VALUE - 1, TimeUnit.SECONDS, Long.MAX_VALUE, TimeUnit.SECONDS));
33+
34+
test(TimeUnit.HOURS, TimeUnit.DAYS);
35+
36+
test(TimeUnit.MINUTES, TimeUnit.HOURS);
37+
test(TimeUnit.MINUTES, TimeUnit.DAYS);
38+
39+
test(TimeUnit.SECONDS, TimeUnit.MINUTES);
40+
test(TimeUnit.SECONDS, TimeUnit.HOURS);
41+
test(TimeUnit.SECONDS, TimeUnit.DAYS);
42+
43+
test(TimeUnit.MILLISECONDS, TimeUnit.SECONDS);
44+
test(TimeUnit.MILLISECONDS, TimeUnit.MINUTES);
45+
test(TimeUnit.MILLISECONDS, TimeUnit.HOURS);
46+
test(TimeUnit.MILLISECONDS, TimeUnit.DAYS);
47+
48+
test(TimeUnit.MICROSECONDS, TimeUnit.MILLISECONDS);
49+
test(TimeUnit.MICROSECONDS, TimeUnit.SECONDS);
50+
test(TimeUnit.MICROSECONDS, TimeUnit.MINUTES);
51+
test(TimeUnit.MICROSECONDS, TimeUnit.HOURS);
52+
test(TimeUnit.MICROSECONDS, TimeUnit.DAYS);
53+
54+
test(TimeUnit.NANOSECONDS, TimeUnit.MICROSECONDS);
55+
test(TimeUnit.NANOSECONDS, TimeUnit.MILLISECONDS);
56+
test(TimeUnit.NANOSECONDS, TimeUnit.SECONDS);
57+
test(TimeUnit.NANOSECONDS, TimeUnit.MINUTES);
58+
test(TimeUnit.NANOSECONDS, TimeUnit.HOURS);
59+
test(TimeUnit.NANOSECONDS, TimeUnit.DAYS);
60+
}
61+
}

0 commit comments

Comments
 (0)