@@ -9,6 +9,19 @@ namespace ByteAether.Ulid;
99
1010public readonly partial struct Ulid
1111{
12+ /// <summary>
13+ /// Whether <see cref="Ulid"/>s should be generated in a monotonic manner by default.<br />
14+ /// Initial value is set to <c>true</c>.<br/>
15+ /// <b>This setting applies globally without any scoping.</b>
16+ /// </summary>
17+ /// <remarks>
18+ /// When set to <c>true</c> (default), <see cref="Ulid"/>s generated without explicitly specifying monotonicity
19+ /// will ensure that they are monotonically increasing.<br />
20+ /// When set to <c>false</c>, <see cref="Ulid"/>s generated without explicitly specifying monotonicity will be
21+ /// generated with random <see cref="Random" /> value.
22+ /// </remarks>
23+ public static bool DefaultIsMonotonic { get ; set ; } = true ;
24+
1225 private static readonly byte [ ] _lastUlid = new byte [ _ulidSize ] ;
1326 private static readonly RandomNumberGenerator _rng = RandomNumberGenerator . Create ( ) ;
1427
@@ -19,56 +32,69 @@ public readonly partial struct Ulid
1932#endif
2033
2134 /// <summary>
22- /// Initializes a new instance of the <see cref="ByteAether. Ulid"/> struct using the specified byte array.
35+ /// Initializes a new instance of the <see cref="Ulid"/> struct using the specified byte array.
2336 /// </summary>
24- /// <param name="bytes">The byte array to initialize the ULID with.</param>
37+ /// <param name="bytes">The byte array to initialize the <see cref="Ulid"/> with.</param>
38+ /// <returns>Given bytes as an <see cref="Ulid"/> instance.</returns>
2539 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
2640 public static Ulid New ( ReadOnlySpan < byte > bytes )
2741 => MemoryMarshal . Read < Ulid > ( bytes ) ;
2842
2943 /// <summary>
30- /// Creates a new ULID with the current timestamp.
44+ /// Creates a new <see cref="Ulid"/> with the current timestamp.
3145 /// </summary>
32- /// <param name="isMonotonic">If true, ensures the ULID is monotonically increasing.</param>
33- /// <returns>A new ULID instance.</returns>
46+ /// <param name="isMonotonic">
47+ /// If <c>null</c> (default), the value of <see cref="DefaultIsMonotonic"/> is used to determine monotonicity.<br />
48+ /// If <c>true</c>, ensures the <see cref="Ulid"/> is monotonically increasing.<br />
49+ /// If <c>false</c>, generates a random <see cref="Random" /> part in <see cref="Ulid"/>.
50+ /// </param>
51+ /// <returns>A new <see cref="Ulid"/> instance.</returns>
3452 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
35- public static Ulid New ( bool isMonotonic = true )
53+ public static Ulid New ( bool ? isMonotonic = null )
3654 => New ( DateTimeOffset . UtcNow , isMonotonic ) ;
3755
3856 /// <summary>
39- /// Creates a new ULID with the specified timestamp.
57+ /// Creates a new <see cref="Ulid"/> with the specified timestamp.
4058 /// </summary>
41- /// <param name="dateTimeOffset">The timestamp to use for the ULID.</param>
42- /// <param name="isMonotonic">If true, ensures the ULID is monotonically increasing.</param>
43- /// <returns>A new ULID instance.</returns>
59+ /// <param name="dateTimeOffset">The timestamp to use for the <see cref="Ulid"/>.</param>
60+ /// <param name="isMonotonic">
61+ /// If <c>null</c> (default), the value of <see cref="DefaultIsMonotonic"/> is used to determine monotonicity.<br />
62+ /// If <c>true</c>, ensures the <see cref="Ulid"/> is monotonically increasing.<br />
63+ /// If <c>false</c>, generates a random <see cref="Random" /> part in <see cref="Ulid"/>.
64+ /// </param>
65+ /// <returns>A new <see cref="Ulid"/> instance.</returns>
4466 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
45- public static Ulid New ( DateTimeOffset dateTimeOffset , bool isMonotonic = true )
67+ public static Ulid New ( DateTimeOffset dateTimeOffset , bool ? isMonotonic = null )
4668 => New ( dateTimeOffset . ToUnixTimeMilliseconds ( ) , isMonotonic ) ;
4769
4870 /// <summary>
49- /// Creates a new ULID with the specified timestamp.
71+ /// Creates a new <see cref="Ulid"/> with the specified timestamp.
5072 /// </summary>
51- /// <param name="dateTimeOffset">The timestamp to use for the ULID .</param>
73+ /// <param name="dateTimeOffset">The timestamp to use for the <see cref="Ulid"/> .</param>
5274 /// <param name="random" >
53- /// A span containing the random component of the ULID .
54- /// It must be at least 10 bytes long, as the last 10 bytes of the ULID are derived from this span.
75+ /// A span containing the random component of the <see cref="Ulid"/> .
76+ /// It must be at least 10 bytes long, as the last 10 bytes of the <see cref="Ulid"/> are derived from this span.
5577 /// </param>
56- /// <returns>A new ULID instance.</returns>
78+ /// <returns>A new <see cref="Ulid"/> instance.</returns>
5779 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
5880 public static Ulid New ( DateTimeOffset dateTimeOffset , Span < byte > random )
5981 => New ( dateTimeOffset . ToUnixTimeMilliseconds ( ) , random ) ;
6082
6183 /// <summary>
62- /// Creates a new ULID with the specified timestamp in milliseconds.
84+ /// Creates a new <see cref="Ulid"/> with the specified timestamp in milliseconds.
6385 /// </summary>
64- /// <param name="timestamp">The timestamp in milliseconds to use for the ULID.</param>
65- /// <param name="isMonotonic">If true, ensures the ULID is monotonically increasing.</param>
66- /// <returns>A new ULID instance.</returns>
86+ /// <param name="timestamp">The timestamp in milliseconds to use for the <see cref="Ulid"/>.</param>
87+ /// <param name="isMonotonic">
88+ /// If <c>null</c> (default), the value of <see cref="DefaultIsMonotonic"/> is used to determine monotonicity.<br />
89+ /// If <c>true</c>, ensures the <see cref="Ulid"/> is monotonically increasing.<br />
90+ /// If <c>false</c>, generates a random <see cref="Random" /> part in <see cref="Ulid"/>.
91+ /// </param>
92+ /// <returns>A new <see cref="Ulid"/> instance.</returns>
6793#if NET5_0_OR_GREATER
6894 [ SkipLocalsInit ]
6995#endif
7096 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
71- public static Ulid New ( long timestamp , bool isMonotonic = true )
97+ public static Ulid New ( long timestamp , bool ? isMonotonic = null )
7298 {
7399 Ulid ulid = default ;
74100
@@ -77,7 +103,7 @@ public static Ulid New(long timestamp, bool isMonotonic = true)
77103 var ulidBytes = new Span < byte > ( Unsafe . AsPointer ( ref Unsafe . AsRef ( in ulid ) ) , _ulidSize ) ;
78104
79105 FillTime ( ulidBytes , timestamp ) ;
80- FillRandom ( ulidBytes , isMonotonic ) ;
106+ FillRandom ( ulidBytes , isMonotonic ?? DefaultIsMonotonic ) ;
81107 }
82108
83109 return ulid ;
@@ -88,11 +114,11 @@ public static Ulid New(long timestamp, bool isMonotonic = true)
88114 /// </summary>
89115 /// <param name="timestamp">
90116 /// A 64-bit integer representing the timestamp in milliseconds since the Unix epoch (1970-01-01T00:00:00Z).
91- /// This value will be encoded into the first 6 bytes of the ULID .
117+ /// This value will be encoded into the first 6 bytes of the <see cref="Ulid"/> .
92118 /// </param>
93119 /// <param name="random">
94- /// A span containing the random component of the ULID .
95- /// It must be at least 10 bytes long, as the last 10 bytes of the ULID are derived from this span.
120+ /// A span containing the random component of the <see cref="Ulid"/> .
121+ /// It must be at least 10 bytes long, as the last 10 bytes of the <see cref="Ulid"/> are derived from this span.
96122 /// </param>
97123 /// <returns>
98124 /// A new <see cref="Ulid"/> instance composed of the given timestamp and random byte sequence.
0 commit comments