Skip to content

Package: TimeProvider

TimeProvider

nameinstructionbranchcomplexitylinemethod
currentLocalDateTime()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
currentZonedDateTime()
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
get()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
getInstance()
M: 0 C: 18
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 0 C: 4
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util;
28:
29: import javax.annotation.Nonnull;
30: import java.time.Instant;
31: import java.time.LocalDateTime;
32: import java.time.ZoneId;
33: import java.time.ZonedDateTime;
34: import java.util.concurrent.atomic.AtomicReference;
35: import java.util.function.Supplier;
36:
37: /***********************************************************************************************************************
38: *
39: * A provider of current time. It should be used by code requiring a timestamp, so it can be mocked during tests.
40: * {@code MockTimeProvider} in module "Test Utilities" is a suitable mock for performing tests.
41: *
42: * @author Fabrizio Giudici
43: * @since 3.2-ALPHA-1 (was previously InstantProvider since 1.39)
44: *
45: **********************************************************************************************************************/
46: @FunctionalInterface
47: public interface TimeProvider extends Supplier<Instant>
48: {
49: // FIXME: should be private
50: public static AtomicReference<TimeProvider> __INSTANCE = new AtomicReference<>();
51:
52: /*******************************************************************************************************************
53: *
54: * Returns the current time.
55: *
56: * @return the current time as an {@link Instant}
57: * @since 3.2-ALPHA-2
58: *
59: ******************************************************************************************************************/
60: @Nonnull
61: public Instant currentInstant();
62:
63: /*******************************************************************************************************************
64: *
65: * Returns the current time. This method is provided to implement {@link Supplier}{@code <Instant>}.
66: *
67: * @return the current time as an {@link Instant}
68: * @since 3.2-ALPHA-2
69: *
70: ******************************************************************************************************************/
71: @Override @Nonnull
72: public default Instant get()
73: {
74: return currentInstant();
75: }
76:
77: /*******************************************************************************************************************
78: *
79: * Returns the current time.
80: *
81: * @return the current time as a {@link ZonedDateTime} in the default zone.
82: * @since 3.2-ALPHA-2
83: *
84: ******************************************************************************************************************/
85: @Nonnull
86: public default ZonedDateTime currentZonedDateTime()
87: {
88: return ZonedDateTime.ofInstant(currentInstant(), ZoneId.systemDefault());
89: }
90:
91: /*******************************************************************************************************************
92: *
93: * Returns the current time.
94: *
95: * @return the current time as a {@link LocalDateTime} in the default zone.
96: * @since 3.2-ALPHA-2
97: *
98: ******************************************************************************************************************/
99: @Nonnull
100: public default LocalDateTime currentLocalDateTime()
101: {
102: return LocalDateTime.ofInstant(currentInstant(), ZoneId.systemDefault());
103: }
104:
105: /*******************************************************************************************************************
106: *
107: * Returns the default instance.
108: *
109: * @return the default instance
110: * @since 3.2-ALPHA-2
111: *
112: ******************************************************************************************************************/
113: @Nonnull
114: public static TimeProvider getInstance()
115: {
116: synchronized (TimeProvider.class)
117: {
118:• if (__INSTANCE.get() == null)
119: {
120: __INSTANCE.set(new DefaultTimeProvider());
121: }
122:
123: return __INSTANCE.get();
124: }
125: }
126:
127: /*******************************************************************************************************************
128: *
129: ******************************************************************************************************************/
130: static class DefaultTimeProvider implements TimeProvider
131: {
132: @Override @Nonnull
133: public Instant currentInstant()
134: {
135: return Instant.now();
136: }
137: }
138: }