Skip to content

Commit eb05d3f

Browse files
BAEL-5178:An introduction to InstantSource (eugenp#11646)
* BAEL-5178:An introduction to InstantSource * BAEL-5178:PR fixes Co-authored-by: Pablo Ubal <[email protected]>
1 parent 4000ebc commit eb05d3f

3 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.instantsource;
2+
3+
import java.time.Instant;
4+
import java.time.InstantSource;
5+
6+
public class InstantExample {
7+
InstantWrapper instantWrapper;
8+
InstantSource instantSource;
9+
10+
public InstantExample(InstantWrapper instantWrapper, InstantSource instantSource) {
11+
this.instantWrapper = instantWrapper;
12+
this.instantSource = instantSource;
13+
}
14+
15+
public Instant getCurrentInstantFromWrapper() {
16+
return instantWrapper.instant();
17+
}
18+
public Instant getCurrentInstantFromInstantSource() {
19+
return instantSource.instant();
20+
}
21+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.instantsource;
2+
3+
import java.time.Clock;
4+
import java.time.Instant;
5+
import java.time.LocalDateTime;
6+
import java.time.ZoneOffset;
7+
import java.time.ZonedDateTime;
8+
9+
public class InstantWrapper {
10+
Clock clock;
11+
12+
private InstantWrapper() {
13+
this.clock = Clock.systemDefaultZone();
14+
}
15+
16+
private InstantWrapper(ZonedDateTime zonedDateTime) {
17+
this.clock = Clock.fixed(zonedDateTime.toInstant(), zonedDateTime.getZone());
18+
}
19+
20+
private InstantWrapper(LocalDateTime localDateTime) {
21+
this.clock = Clock.fixed(localDateTime.toInstant(ZoneOffset.UTC), ZoneOffset.UTC);
22+
}
23+
24+
public static InstantWrapper of() {
25+
return new InstantWrapper();
26+
}
27+
28+
public static InstantWrapper of(ZonedDateTime zonedDateTime) {
29+
return new InstantWrapper(zonedDateTime);
30+
}
31+
32+
public static InstantWrapper of(LocalDateTime localDateTime) {
33+
return new InstantWrapper(localDateTime);
34+
}
35+
36+
public Instant instant() {
37+
return clock.instant();
38+
}
39+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.baeldung.instantsource;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.time.Clock;
6+
import java.time.Instant;
7+
import java.time.InstantSource;
8+
import java.time.LocalDateTime;
9+
import java.time.ZoneOffset;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
14+
class InstantExampleUnitTest {
15+
16+
@Test
17+
void getCurrentInstantFromWrapper_shouldReturnCurrentInstantBasedOnSystemClock() {
18+
//given
19+
InstantExample tested = new InstantExample(InstantWrapper.of(), null);
20+
Instant currentInstant = Clock.systemDefaultZone().instant();
21+
//when
22+
Instant returnedInstant = tested.getCurrentInstantFromWrapper();
23+
24+
//then
25+
assertTrue(returnedInstant.isAfter(currentInstant));
26+
}
27+
28+
@Test
29+
void getCurrentInstantFromWrapper_shouldReturnFixedInstant() {
30+
//given
31+
LocalDateTime now = LocalDateTime.now();
32+
InstantExample tested = new InstantExample(InstantWrapper.of(now), null);
33+
Instant currentInstant = now.toInstant(ZoneOffset.UTC);
34+
//when
35+
Instant returnedInstant = tested.getCurrentInstantFromWrapper();
36+
37+
//then
38+
assertEquals(currentInstant, returnedInstant);
39+
}
40+
41+
@Test
42+
void getCurrentInstantFromInstantSource_shouldReturnCurrentInstantBasedOnSystemClock() {
43+
//given
44+
InstantSource instantSource = InstantSource.system();
45+
InstantExample tested = new InstantExample(null, instantSource);
46+
Instant currentInstant = instantSource.instant();
47+
48+
//when
49+
Instant returnedInstant = tested.getCurrentInstantFromInstantSource();
50+
51+
//then
52+
assertTrue(returnedInstant.isAfter(currentInstant));
53+
}
54+
55+
@Test
56+
void getCurrentInstantFromInstantSource_shouldReturnFixedInstant() {
57+
//given
58+
LocalDateTime now = LocalDateTime.now();
59+
InstantSource instantSource = InstantSource.fixed(now.toInstant(ZoneOffset.UTC));
60+
InstantExample tested = new InstantExample(null, instantSource);
61+
Instant currentInstant = instantSource.instant();
62+
63+
LocalDateTime fixed = LocalDateTime.of(2022, 01, 01, 00, 00);
64+
Instant i = InstantSource.fixed(fixed.toInstant(ZoneOffset.UTC)).instant();
65+
System.out.println(i);
66+
67+
//when
68+
Instant returnedInstant = tested.getCurrentInstantFromInstantSource();
69+
70+
//then
71+
assertEquals(currentInstant, returnedInstant);
72+
}
73+
74+
@Test
75+
void getCurrentInstantFromInstantSource_shouldMatchGivenTimezone() {
76+
//given
77+
LocalDateTime now = LocalDateTime.now();
78+
InstantSource instantSource = InstantSource.fixed(now.toInstant(ZoneOffset.of("-4")));
79+
InstantExample tested = new InstantExample(null, instantSource);
80+
81+
//when
82+
Instant returnedInstant = tested.getCurrentInstantFromInstantSource();
83+
84+
//then
85+
assertEquals(now.atOffset(ZoneOffset.of("-4")).toInstant(), returnedInstant);
86+
}
87+
}

0 commit comments

Comments
 (0)