Skip to content

Commit 7d5b922

Browse files
committed
ValueBag -> DateTimeComponents
1 parent 62e2010 commit 7d5b922

File tree

11 files changed

+143
-143
lines changed

11 files changed

+143
-143
lines changed

core/common/src/Instant.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -512,21 +512,21 @@ public fun Instant.minus(other: Instant, unit: DateTimeUnit.TimeBased): Long =
512512
/**
513513
* Formats this value using the given [format] using the given [offset].
514514
*
515-
* [ValueBag.Format.ISO_DATE_TIME_OFFSET] is the format used by [toString] and [Instant.Companion.parse].
515+
* [DateTimeComponents.Format.ISO_DATE_TIME_OFFSET] is the format used by [toString] and [Instant.Companion.parse].
516516
*/
517-
public fun Instant.format(format: DateTimeFormat<ValueBag>, offset: UtcOffset = UtcOffset.ZERO): String {
517+
public fun Instant.format(format: DateTimeFormat<DateTimeComponents>, offset: UtcOffset = UtcOffset.ZERO): String {
518518
val instant = this
519519
return format.format { populateFrom(instant, offset) }
520520
}
521521

522522
/**
523523
* Parses an [Instant] value using the given [format].
524524
* Equivalent to calling [DateTimeFormat.parse] on [format] with [input] and obtaining the resulting [Instant] using
525-
* [ValueBag.toInstantUsingUtcOffset].
525+
* [DateTimeComponents.toInstantUsingUtcOffset].
526526
*
527527
* @throws IllegalArgumentException if the text cannot be parsed or the boundaries of [Instant] are exceeded.
528528
*/
529-
public fun Instant.Companion.parse(input: CharSequence, format: DateTimeFormat<ValueBag>): Instant =
529+
public fun Instant.Companion.parse(input: CharSequence, format: DateTimeFormat<DateTimeComponents>): Instant =
530530
format.parse(input).toInstantUsingUtcOffset()
531531

532532
internal const val DISTANT_PAST_SECONDS = -3217862419201
@@ -538,4 +538,4 @@ internal const val DISTANT_FUTURE_SECONDS = 3093527980800
538538
* Be careful: this function may throw for some values of the [Instant].
539539
*/
540540
internal fun Instant.toStringWithOffset(offset: UtcOffset): String =
541-
format(ValueBag.Formats.ISO_DATE_TIME_OFFSET, offset)
541+
format(DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET, offset)

core/common/src/LocalDate.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public expect class LocalDate : Comparable<LocalDate> {
5858
* ```
5959
*
6060
* Only parsing and formatting of well-formed values is supported. If the input does not fit the boundaries
61-
* (for example, [dayOfMonth] is 31 for February), consider using [ValueBag.Format] instead.
61+
* (for example, [dayOfMonth] is 31 for February), consider using [DateTimeComponents.Format] instead.
6262
*
6363
* There is a collection of predefined formats in [LocalDate.Formats].
6464
*/

core/common/src/LocalDateTime.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public expect class LocalDateTime : Comparable<LocalDateTime> {
6060
* ```
6161
*
6262
* Only parsing and formatting of well-formed values is supported. If the input does not fit the boundaries
63-
* (for example, [dayOfMonth] is 31 for February), consider using [ValueBag.Format] instead.
63+
* (for example, [dayOfMonth] is 31 for February), consider using [DateTimeComponents.Format] instead.
6464
*
6565
* There is a collection of predefined formats in [LocalDateTime.Formats].
6666
*/

core/common/src/LocalTime.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public expect class LocalTime : Comparable<LocalTime> {
100100
* ```
101101
*
102102
* Only parsing and formatting of well-formed values is supported. If the input does not fit the boundaries
103-
* (for example, [second] is 60), consider using [ValueBag.Format] instead.
103+
* (for example, [second] is 60), consider using [DateTimeComponents.Format] instead.
104104
*
105105
* There is a collection of predefined formats in [LocalTime.Formats].
106106
*/

core/common/src/format/ValueBagFormat.kt renamed to core/common/src/format/DateTimeComponents.kt

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ import kotlin.reflect.*
1818
*
1919
* Its main purpose is to provide support for complex date-time formats that don't correspond to any of the standard
2020
* entities in the library. For example, a format that includes only the month and the day of the month, but not the
21-
* year, can not be represented and parsed as a [LocalDate], but it is valid for a [ValueBag].
21+
* year, can not be represented and parsed as a [LocalDate], but it is valid for a [DateTimeComponents].
2222
*
2323
* Example:
2424
* ```
2525
* val input = "2020-03-16T23:59:59.999999999+03:00"
26-
* val bag = ValueBag.Format.ISO_INSTANT.parse(input)
26+
* val bag = DateTimeComponents.Format.ISO_INSTANT.parse(input)
2727
* val localDateTime = bag.toLocalDateTime() // LocalDateTime(2020, 3, 16, 23, 59, 59, 999_999_999)
2828
* val instant = bag.toInstantUsingUtcOffset() // Instant.parse("2020-03-16T20:59:59.999999999Z")
2929
* val offset = bag.toUtcOffset() // UtcOffset(hours = 3)
3030
* ```
3131
*
3232
* Another purpose is to support parsing and formatting data with out-of-bounds values. For example, parsing
33-
* `23:59:60` as a [LocalTime] is not possible, but it is possible to parse it as a [ValueBag], adjust the value by
33+
* `23:59:60` as a [LocalTime] is not possible, but it is possible to parse it as a [DateTimeComponents], adjust the value by
3434
* setting [second] to `59`, and then convert it to a [LocalTime] via [toLocalTime].
3535
*
3636
* Example:
3737
* ```
3838
* val input = "23:59:60"
3939
* val extraDay: Boolean
40-
* val time = ValueBag.Format {
40+
* val time = DateTimeComponents.Format {
4141
* appendTime(LocalTime.Format.ISO)
4242
* }.parse(input).apply {
4343
* if (hour == 23 && minute == 59 && second == 60) {
@@ -49,12 +49,12 @@ import kotlin.reflect.*
4949
* ```
5050
*
5151
* Because this class has limited applications, constructing it directly is not possible.
52-
* For formatting, use the [format] overload that accepts a lambda with a [ValueBag] receiver.
52+
* For formatting, use the [format] overload that accepts a lambda with a [DateTimeComponents] receiver.
5353
*
5454
* Example:
5555
* ```
5656
* // Mon, 16 Mar 2020 23:59:59 +0300
57-
* ValueBag.Format.RFC_1123.format {
57+
* DateTimeComponents.Format.RFC_1123.format {
5858
* populateFrom(LocalDateTime(2020, 3, 16, 23, 59, 59, 999_999_999))
5959
* populateFrom(UtcOffset(hours = 3))
6060
* }
@@ -63,26 +63,26 @@ import kotlin.reflect.*
6363
* Accessing the fields of this class is not thread-safe.
6464
* Make sure to apply proper synchronization if you are using a single instance from multiple threads.
6565
*/
66-
public class ValueBag internal constructor(internal val contents: ValueBagContents = ValueBagContents()) {
66+
public class DateTimeComponents internal constructor(internal val contents: DateTimeComponentsContents = DateTimeComponentsContents()) {
6767
public companion object {
6868
/**
69-
* Creates a [DateTimeFormat] for [ValueBag] values using [DateTimeFormatBuilder.WithDateTimeComponents].
69+
* Creates a [DateTimeFormat] for [DateTimeComponents] values using [DateTimeFormatBuilder.WithDateTimeComponents].
7070
*
71-
* There is a collection of predefined formats in [ValueBag.Formats].
71+
* There is a collection of predefined formats in [DateTimeComponents.Formats].
7272
*/
7373
@Suppress("FunctionName")
74-
public fun Format(block: DateTimeFormatBuilder.WithDateTimeComponents.() -> Unit): DateTimeFormat<ValueBag> {
75-
val builder = ValueBagFormat.Builder(AppendableFormatStructure())
74+
public fun Format(block: DateTimeFormatBuilder.WithDateTimeComponents.() -> Unit): DateTimeFormat<DateTimeComponents> {
75+
val builder = DateTimeComponentsFormat.Builder(AppendableFormatStructure())
7676
block(builder)
77-
return ValueBagFormat(builder.build())
77+
return DateTimeComponentsFormat(builder.build())
7878
}
7979
}
8080

8181
/**
82-
* The entry point for parsing and formatting [ValueBag] values.
82+
* The entry point for parsing and formatting [DateTimeComponents] values.
8383
*
84-
* If predefined formats are not sufficient, use [ValueBag.Format] to create a custom
85-
* [kotlinx.datetime.format.DateTimeFormat] for [ValueBag] values.
84+
* If predefined formats are not sufficient, use [DateTimeComponents.Format] to create a custom
85+
* [kotlinx.datetime.format.DateTimeFormat] for [DateTimeComponents] values.
8686
*/
8787
public object Formats {
8888

@@ -94,11 +94,11 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
9494
* * `2020-01-01T23:59:59+01`
9595
* * `2020-01-01T23:59:59Z`
9696
*
97-
* This format uses the local date, local time, and UTC offset fields of [ValueBag].
97+
* This format uses the local date, local time, and UTC offset fields of [DateTimeComponents].
9898
*
9999
* See ISO-8601-1:2019, 5.4.2.1b), excluding the format without the offset.
100100
*/
101-
public val ISO_DATE_TIME_OFFSET: DateTimeFormat<ValueBag> = Format {
101+
public val ISO_DATE_TIME_OFFSET: DateTimeFormat<DateTimeComponents> = Format {
102102
appendDate(ISO_DATE)
103103
alternativeParsing({
104104
char('t')
@@ -132,7 +132,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
132132
*
133133
* North American and military time zone abbreviations are not supported.
134134
*/
135-
public val RFC_1123: DateTimeFormat<ValueBag> = Format {
135+
public val RFC_1123: DateTimeFormat<DateTimeComponents> = Format {
136136
alternativeParsing({
137137
// the day of week may be missing
138138
}) {
@@ -166,23 +166,23 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
166166
}
167167

168168
/**
169-
* Writes the contents of the specified [localTime] to this [ValueBag].
169+
* Writes the contents of the specified [localTime] to this [DateTimeComponents].
170170
* The [localTime] is written to the [hour], [minute], [second] and [nanosecond] fields.
171171
*
172172
* If any of the fields are already set, they will be overwritten.
173173
*/
174174
public fun populateFrom(localTime: LocalTime) { contents.time.populateFrom(localTime) }
175175

176176
/**
177-
* Writes the contents of the specified [localDate] to this [ValueBag].
177+
* Writes the contents of the specified [localDate] to this [DateTimeComponents].
178178
* The [localDate] is written to the [year], [monthNumber] and [dayOfMonth] fields.
179179
*
180180
* If any of the fields are already set, they will be overwritten.
181181
*/
182182
public fun populateFrom(localDate: LocalDate) { contents.date.populateFrom(localDate) }
183183

184184
/**
185-
* Writes the contents of the specified [localDateTime] to this [ValueBag].
185+
* Writes the contents of the specified [localDateTime] to this [DateTimeComponents].
186186
* The [localDateTime] is written to the
187187
* [year], [monthNumber], [dayOfMonth], [hour], [minute], [second] and [nanosecond] fields.
188188
*
@@ -194,15 +194,15 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
194194
}
195195

196196
/**
197-
* Writes the contents of the specified [utcOffset] to this [ValueBag].
197+
* Writes the contents of the specified [utcOffset] to this [DateTimeComponents].
198198
* The [utcOffset] is written to the [offsetTotalHours], [offsetMinutesOfHour] and [offsetSecondsOfMinute] fields.
199199
*
200200
* If any of the fields are already set, they will be overwritten.
201201
*/
202202
public fun populateFrom(utcOffset: UtcOffset) { contents.offset.populateFrom(utcOffset) }
203203

204204
/**
205-
* Writes the contents of the specified [instant] to this [ValueBag].
205+
* Writes the contents of the specified [instant] to this [DateTimeComponents].
206206
*
207207
* This method is almost always equivalent to the following code:
208208
* ```
@@ -288,7 +288,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
288288
public var timeZoneId: String? by contents::timeZoneId
289289

290290
/**
291-
* Builds a [UtcOffset] from the fields in this [ValueBag].
291+
* Builds a [UtcOffset] from the fields in this [DateTimeComponents].
292292
*
293293
* This method uses the following fields:
294294
* * [offsetTotalHours] (default value is 0)
@@ -300,7 +300,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
300300
public fun toUtcOffset(): UtcOffset = contents.offset.toUtcOffset()
301301

302302
/**
303-
* Builds a [LocalDate] from the fields in this [ValueBag].
303+
* Builds a [LocalDate] from the fields in this [DateTimeComponents].
304304
*
305305
* This method uses the following fields:
306306
* * [year]
@@ -314,7 +314,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
314314
public fun toLocalDate(): LocalDate = contents.date.toLocalDate()
315315

316316
/**
317-
* Builds a [LocalTime] from the fields in this [ValueBag].
317+
* Builds a [LocalTime] from the fields in this [DateTimeComponents].
318318
*
319319
* This method uses the following fields:
320320
* * [hour]
@@ -327,7 +327,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
327327
public fun toLocalTime(): LocalTime = contents.time.toLocalTime()
328328

329329
/**
330-
* Builds a [LocalDateTime] from the fields in this [ValueBag].
330+
* Builds a [LocalDateTime] from the fields in this [DateTimeComponents].
331331
*
332332
* This method uses the following fields:
333333
* * [year]
@@ -349,7 +349,7 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
349349
public fun toLocalDateTime(): LocalDateTime = toLocalDate().atTime(toLocalTime())
350350

351351
/**
352-
* Builds an [Instant] from the fields in this [ValueBag].
352+
* Builds an [Instant] from the fields in this [DateTimeComponents].
353353
*
354354
* Uses the fields required for [toLocalDateTime] and [toUtcOffset].
355355
*
@@ -383,69 +383,69 @@ public class ValueBag internal constructor(internal val contents: ValueBagConten
383383
}
384384

385385
/**
386-
* Uses this format to format an unstructured [ValueBag].
386+
* Uses this format to format an unstructured [DateTimeComponents].
387387
*
388-
* [block] is called on an empty [ValueBag] before formatting.
388+
* [block] is called on an empty [DateTimeComponents] before formatting.
389389
*
390390
* Example:
391391
* ```
392392
* // Mon, 16 Mar 2020 23:59:59 +0300
393-
* ValueBag.Format.RFC_1123.format {
393+
* DateTimeComponents.Format.RFC_1123.format {
394394
* populateFrom(LocalDateTime(2020, 3, 16, 23, 59, 59, 999_999_999))
395395
* populateFrom(UtcOffset(hours = 3))
396396
* }
397397
* ```
398398
*/
399-
public fun DateTimeFormat<ValueBag>.format(block: ValueBag.() -> Unit): String = format(ValueBag().apply { block() })
399+
public fun DateTimeFormat<DateTimeComponents>.format(block: DateTimeComponents.() -> Unit): String = format(DateTimeComponents().apply { block() })
400400

401401
/**
402-
* Parses a [ValueBag] from [input] using the given format.
402+
* Parses a [DateTimeComponents] from [input] using the given format.
403403
* Equivalent to calling [DateTimeFormat.parse] on [format] with [input].
404404
*
405-
* [ValueBag] does not perform any validation, so even invalid values may be parsed successfully if the string pattern
405+
* [DateTimeComponents] does not perform any validation, so even invalid values may be parsed successfully if the string pattern
406406
* matches.
407407
*
408408
* @throws IllegalArgumentException if the text does not match the format.
409409
*/
410-
public fun ValueBag.Companion.parse(input: CharSequence, format: DateTimeFormat<ValueBag>): ValueBag =
410+
public fun DateTimeComponents.Companion.parse(input: CharSequence, format: DateTimeFormat<DateTimeComponents>): DateTimeComponents =
411411
format.parse(input)
412412

413-
internal class ValueBagContents internal constructor(
413+
internal class DateTimeComponentsContents internal constructor(
414414
val date: IncompleteLocalDate = IncompleteLocalDate(),
415415
val time: IncompleteLocalTime = IncompleteLocalTime(),
416416
val offset: IncompleteUtcOffset = IncompleteUtcOffset(),
417417
var timeZoneId: String? = null,
418418
) : DateFieldContainer by date, TimeFieldContainer by time, UtcOffsetFieldContainer by offset,
419-
DateTimeFieldContainer, Copyable<ValueBagContents> {
420-
override fun copy(): ValueBagContents = ValueBagContents(date.copy(), time.copy(), offset.copy(), timeZoneId)
419+
DateTimeFieldContainer, Copyable<DateTimeComponentsContents> {
420+
override fun copy(): DateTimeComponentsContents = DateTimeComponentsContents(date.copy(), time.copy(), offset.copy(), timeZoneId)
421421

422422
override fun equals(other: Any?): Boolean =
423-
other is ValueBagContents && other.date == date && other.time == time &&
423+
other is DateTimeComponentsContents && other.date == date && other.time == time &&
424424
other.offset == offset && other.timeZoneId == timeZoneId
425425

426426
override fun hashCode(): Int =
427427
date.hashCode() xor time.hashCode() xor offset.hashCode() xor (timeZoneId?.hashCode() ?: 0)
428428
}
429429

430430
@SharedImmutable
431-
internal val timeZoneField = GenericFieldSpec(ValueBagContents::timeZoneId)
431+
internal val timeZoneField = GenericFieldSpec(DateTimeComponentsContents::timeZoneId)
432432

433433
internal class TimeZoneIdDirective(knownZones: Set<String>) :
434-
StringFieldFormatDirective<ValueBagContents>(timeZoneField, knownZones) {
434+
StringFieldFormatDirective<DateTimeComponentsContents>(timeZoneField, knownZones) {
435435

436436
override val builderRepresentation: String = "${DateTimeFormatBuilder.WithDateTimeComponents::appendTimeZoneId.name}()"
437437
}
438438

439-
internal class ValueBagFormat(val actualFormat: StringFormat<ValueBagContents>) :
440-
AbstractDateTimeFormat<ValueBag, ValueBagContents>(actualFormat) {
441-
override fun intermediateFromValue(value: ValueBag): ValueBagContents = value.contents
439+
internal class DateTimeComponentsFormat(val actualFormat: StringFormat<DateTimeComponentsContents>) :
440+
AbstractDateTimeFormat<DateTimeComponents, DateTimeComponentsContents>(actualFormat) {
441+
override fun intermediateFromValue(value: DateTimeComponents): DateTimeComponentsContents = value.contents
442442

443-
override fun valueFromIntermediate(intermediate: ValueBagContents): ValueBag = ValueBag(intermediate)
443+
override fun valueFromIntermediate(intermediate: DateTimeComponentsContents): DateTimeComponents = DateTimeComponents(intermediate)
444444

445-
override fun newIntermediate(): ValueBagContents = ValueBagContents()
445+
override fun newIntermediate(): DateTimeComponentsContents = DateTimeComponentsContents()
446446

447-
class Builder(override val actualBuilder: AppendableFormatStructure<ValueBagContents>) :
448-
AbstractDateTimeFormatBuilder<ValueBagContents, Builder>, DateTimeFormatBuilder.WithDateTimeComponents {
447+
class Builder(override val actualBuilder: AppendableFormatStructure<DateTimeComponentsContents>) :
448+
AbstractDateTimeFormatBuilder<DateTimeComponentsContents, Builder>, DateTimeFormatBuilder.WithDateTimeComponents {
449449
override fun appendYear(padding: Padding) =
450450
actualBuilder.add(BasicFormatStructure(YearDirective(padding)))
451451

@@ -512,8 +512,8 @@ internal class ValueBagFormat(val actualFormat: StringFormat<ValueBagContents>)
512512
}
513513

514514
@Suppress("NO_ELSE_IN_WHEN")
515-
override fun appendValueBag(format: DateTimeFormat<ValueBag>) = when (format) {
516-
is ValueBagFormat -> actualBuilder.add(format.actualFormat.directives)
515+
override fun appendDateTimeComponents(format: DateTimeFormat<DateTimeComponents>) = when (format) {
516+
is DateTimeComponentsFormat -> actualBuilder.add(format.actualFormat.directives)
517517
}
518518

519519
override fun createEmpty(): Builder = Builder(AppendableFormatStructure())

core/common/src/format/DateTimeFormatBuilder.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ public sealed interface DateTimeFormatBuilder {
234234
*
235235
* Example:
236236
* ```
237-
* appendValueBag(ValueBag.Format.RFC_1123)
237+
* appendDateTimeComponents(DateTimeComponents.Format.RFC_1123)
238238
* ```
239239
*/
240-
public fun appendValueBag(format: DateTimeFormat<ValueBag>)
240+
public fun appendDateTimeComponents(format: DateTimeFormat<DateTimeComponents>)
241241
}
242242
}
243243

core/common/test/InstantTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class InstantTest {
130130

131131
for (instant in instants) {
132132
for (offset in offsets) {
133-
val str = instant.format(ValueBag.Formats.ISO_DATE_TIME_OFFSET, offset)
133+
val str = instant.format(DateTimeComponents.Formats.ISO_DATE_TIME_OFFSET, offset)
134134
assertEquals(instant, Instant.parse(str))
135135
}
136136
}

0 commit comments

Comments
 (0)