diff --git a/core/src/commonMain/generated/io/islandtime/_Conversions.kt b/core/src/commonMain/generated/io/islandtime/_Conversions.kt new file mode 100644 index 000000000..f81b7db72 --- /dev/null +++ b/core/src/commonMain/generated/io/islandtime/_Conversions.kt @@ -0,0 +1,55 @@ +// +// This file is auto-generated by 'tools:code-generator' +// +@file:JvmMultifileClass +@file:JvmName("DateTimesKt") + +package io.islandtime + +import kotlin.jvm.JvmMultifileClass +import kotlin.jvm.JvmName + +/** + * Returns this year-month with the precision reduced to the year. + */ +fun YearMonth.toYear(): Year = Year(year) + +/** + * Returns this date with the precision reduced to the year. + */ +fun Date.toYear(): Year = Year(year) + +/** + * Returns this date with the precision reduced to the month. + */ +fun Date.toYearMonth(): YearMonth = YearMonth(year, month) + +/** + * Returns this date-time with the precision reduced to the year. + */ +fun DateTime.toYear(): Year = date.toYear() + +/** + * Returns this date-time with the precision reduced to the month. + */ +fun DateTime.toYearMonth(): YearMonth = date.toYearMonth() + +/** + * Returns this date-time with the precision reduced to the year. + */ +fun OffsetDateTime.toYear(): Year = dateTime.toYear() + +/** + * Returns this date-time with the precision reduced to the month. + */ +fun OffsetDateTime.toYearMonth(): YearMonth = dateTime.toYearMonth() + +/** + * Returns this date-time with the precision reduced to the year. + */ +fun ZonedDateTime.toYear(): Year = dateTime.toYear() + +/** + * Returns this date-time with the precision reduced to the month. + */ +fun ZonedDateTime.toYearMonth(): YearMonth = dateTime.toYearMonth() diff --git a/core/src/commonMain/kotlin/io/islandtime/Conversions.kt b/core/src/commonMain/kotlin/io/islandtime/Conversions.kt index 1aca37c55..0c76f98f9 100644 --- a/core/src/commonMain/kotlin/io/islandtime/Conversions.kt +++ b/core/src/commonMain/kotlin/io/islandtime/Conversions.kt @@ -7,51 +7,6 @@ import io.islandtime.base.TimePoint import kotlin.jvm.JvmMultifileClass import kotlin.jvm.JvmName -/** - * Returns this date with the precision reduced to the year. - */ -fun YearMonth.toYear(): Year = Year(year) - -/** - * Returns this date with the precision reduced to the year. - */ -fun Date.toYear(): Year = Year(year) - -/** - * Returns this date-time with the precision reduced to the year. - */ -fun DateTime.toYear(): Year = date.toYear() - -/** - * Returns this date-time with the precision reduced to the year. - */ -fun OffsetDateTime.toYear(): Year = date.toYear() - -/** - * Returns this date-time with the precision reduced to the year. - */ -fun ZonedDateTime.toYear(): Year = date.toYear() - -/** - * Returns this date with the precision reduced to the year-month. - */ -fun Date.toYearMonth(): YearMonth = YearMonth(year, month) - -/** - * Returns this date-time with the precision reduced to the year-month. - */ -fun DateTime.toYearMonth(): YearMonth = date.toYearMonth() - -/** - * Returns this date-time with the precision reduced to the year-month. - */ -fun OffsetDateTime.toYearMonth(): YearMonth = dateTime.toYearMonth() - -/** - * Returns this date-time with the precision reduced to the year-month. - */ -fun ZonedDateTime.toYearMonth(): YearMonth = dateTime.toYearMonth() - /** * Returns the combined time and UTC offset. */ diff --git a/tools/code-generator/src/main/kotlin/io/islandtime/codegen/Main.kt b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/Main.kt index b8efa0dc6..e5d0a882f 100644 --- a/tools/code-generator/src/main/kotlin/io/islandtime/codegen/Main.kt +++ b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/Main.kt @@ -2,6 +2,7 @@ package io.islandtime.codegen import com.squareup.kotlinpoet.ClassName import io.islandtime.codegen.generators.ConstantsGenerator +import io.islandtime.codegen.generators.DateConversionsGenerator import io.islandtime.codegen.generators.DatePropertiesGenerator import io.islandtime.codegen.generators.TemporalUnitGenerator import java.io.File @@ -21,7 +22,8 @@ fun calendar(name: String) = ClassName(CALENDAR_PACKAGE_NAME, name) private val generators = arrayOf( TemporalUnitGenerator, ConstantsGenerator, - DatePropertiesGenerator + DatePropertiesGenerator, + DateConversionsGenerator ) fun main() { diff --git a/tools/code-generator/src/main/kotlin/io/islandtime/codegen/descriptions/DateTimeDescription.kt b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/descriptions/DateTimeDescription.kt index e5c524b80..fc5c085dc 100644 --- a/tools/code-generator/src/main/kotlin/io/islandtime/codegen/descriptions/DateTimeDescription.kt +++ b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/descriptions/DateTimeDescription.kt @@ -4,35 +4,63 @@ import com.squareup.kotlinpoet.TypeName import io.islandtime.codegen.base enum class DateTimeDescription { + Year { + override val typeName: TypeName get() = base("Year") + override val smallestUnit: TemporalUnitDescription get() = TemporalUnitDescription.YEARS + override val simpleName: String get() = "year" + override val isDateBased: Boolean = true + }, + YearMonth { + override val typeName: TypeName get() = base("YearMonth") + override val smallestUnit: TemporalUnitDescription get() = TemporalUnitDescription.MONTHS + override val simpleName: String get() = "year-month" + override val isDateBased: Boolean = true + }, Date { override val typeName: TypeName get() = base("Date") - override val datePropertyName: String get() = "this" + override val smallestUnit: TemporalUnitDescription get() = TemporalUnitDescription.DAYS override val simpleName: String get() = "date" + override val isDateBased: Boolean = true }, DateTime { override val typeName: TypeName get() = base("DateTime") + override val smallestUnit: TemporalUnitDescription get() = TemporalUnitDescription.NANOSECONDS override val datePropertyName: String get() = "date" override val simpleName: String get() = "date-time" + override val isDateBased: Boolean = true }, OffsetDateTime { override val typeName: TypeName get() = base("OffsetDateTime") + override val smallestUnit: TemporalUnitDescription get() = TemporalUnitDescription.NANOSECONDS override val datePropertyName: String get() = "dateTime" override val simpleName: String get() = "date-time" + override val isDateBased: Boolean = true }, ZonedDateTime { override val typeName: TypeName get() = base("ZonedDateTime") + override val smallestUnit: TemporalUnitDescription get() = TemporalUnitDescription.NANOSECONDS override val datePropertyName: String get() = "dateTime" override val simpleName: String get() = "date-time" + override val isDateBased: Boolean = true }, Instant { override val typeName: TypeName get() = base("Instant") - override val datePropertyName: String get() = "" + override val smallestUnit: TemporalUnitDescription get() = TemporalUnitDescription.NANOSECONDS override val simpleName: String get() = "instant" }; abstract val typeName: TypeName - abstract val datePropertyName: String + abstract val smallestUnit: TemporalUnitDescription abstract val simpleName: String - val isDateBased: Boolean get() = datePropertyName.isNotEmpty() + open val isDateBased: Boolean = false + + open val datePropertyName: String get() = throw NotImplementedError() + + open fun convertsDirectlyTo(other: DateTimeDescription): Boolean { + return isDateBased && + other.isDateBased && + other.smallestUnit > smallestUnit && + other.smallestUnit > TemporalUnitDescription.DAYS + } } \ No newline at end of file diff --git a/tools/code-generator/src/main/kotlin/io/islandtime/codegen/descriptions/TemporalUnitDescription.kt b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/descriptions/TemporalUnitDescription.kt index 7e5659377..e4de722c3 100644 --- a/tools/code-generator/src/main/kotlin/io/islandtime/codegen/descriptions/TemporalUnitDescription.kt +++ b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/descriptions/TemporalUnitDescription.kt @@ -45,7 +45,8 @@ enum class TemporalUnitDescription( val intName: String get() = "Int$pluralName" val longName: String get() = "Long$pluralName" open val singularName: String get() = pluralName.dropLast(1) - val lowerCaseName: String get() = pluralName.toLowerCase(Locale.US) + val lowerSingularName: String get() = singularName.toLowerCase(Locale.US) + val lowerPluralName: String get() = pluralName.toLowerCase(Locale.US) val valueName: String get() = "value" val inUnitPropertyName: String get() = "in$pluralName" val inUnitUncheckedPropertyName: String get() = "${inUnitPropertyName}Unchecked" @@ -86,7 +87,7 @@ data class TemporalUnitConversion( val constantName: String get() { - val (smallerUnit, largerUnit) = smallerUnitToLargerUnit() + val (smallerUnit, largerUnit) = orderedFromSmallerToLargerUnit() return "${smallerUnit.pluralName}_PER_${largerUnit.singularName}".toUpperCase(Locale.US) } @@ -98,7 +99,7 @@ data class TemporalUnitConversion( } val constantValue: Long by lazy { - val (smallerUnit, largerUnit) = smallerUnitToLargerUnit() + val (smallerUnit, largerUnit) = orderedFromSmallerToLargerUnit() (smallerUnit.ordinal until largerUnit.ordinal) .map { TemporalUnitDescription.values()[it].conversionFactor.toLong() } @@ -117,7 +118,7 @@ data class TemporalUnitConversion( } } - private fun smallerUnitToLargerUnit() = if (fromUnit <= toUnit) { + private fun orderedFromSmallerToLargerUnit() = if (fromUnit <= toUnit) { fromUnit to toUnit } else { toUnit to fromUnit diff --git a/tools/code-generator/src/main/kotlin/io/islandtime/codegen/generators/DateConversionsGenerator.kt b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/generators/DateConversionsGenerator.kt new file mode 100644 index 000000000..c074ead90 --- /dev/null +++ b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/generators/DateConversionsGenerator.kt @@ -0,0 +1,59 @@ +package io.islandtime.codegen.generators + +import com.squareup.kotlinpoet.FileSpec +import io.islandtime.codegen.SingleFileGenerator +import io.islandtime.codegen.descriptions.DateTimeDescription +import io.islandtime.codegen.descriptions.DateTimeDescription.* +import io.islandtime.codegen.descriptions.TemporalUnitDescription +import io.islandtime.codegen.dsl.FileBuilder +import io.islandtime.codegen.dsl.FunctionBuilder +import io.islandtime.codegen.dsl.file + +object DateConversionsGenerator : SingleFileGenerator() { + override fun generateSingle(): FileSpec = buildDateConversionsFile() +} + +private fun buildDateConversionsFile() = file( + packageName = "io.islandtime", + fileName = "_Conversions", + jvmName = "DateTimesKt" +) { + DateTimeDescription.values().forEach { receiverClass -> + DateTimeDescription.values() + .filter { otherClass -> receiverClass.convertsDirectlyTo(otherClass) } + .forEach { otherClass -> buildConversionFunction(from = receiverClass, to = otherClass) } + } +} + +private fun FileBuilder.buildConversionFunction(from: DateTimeDescription, to: DateTimeDescription) { + function(name = "to${to.name}") { + kdoc { + "Returns this ${from.simpleName} with the precision reduced to the ${to.smallestUnit.lowerSingularName}." + } + + receiver(from.typeName) + returns(to.typeName) + + if (from.smallestUnit < TemporalUnitDescription.DAYS) { + delegatesTo(from.datePropertyName) + } else { + construct(from, to) + } + } +} + +private fun FunctionBuilder.construct(from: DateTimeDescription, to: DateTimeDescription) { + code { + when (to) { + Year -> { + require(from in arrayOf(Date, YearMonth)) { "Cannot construct '$to' from '$from'" } + "return Year(year)" + } + YearMonth -> { + require(from == Date) { "Cannot construct '$to' from '$from'" } + "return YearMonth(year, month)" + } + else -> throw IllegalArgumentException("Cannot construct '$to'") + } + } +} \ No newline at end of file diff --git a/tools/code-generator/src/main/kotlin/io/islandtime/codegen/generators/DatePropertiesGenerator.kt b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/generators/DatePropertiesGenerator.kt index bba571faa..88fe6ea79 100644 --- a/tools/code-generator/src/main/kotlin/io/islandtime/codegen/generators/DatePropertiesGenerator.kt +++ b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/generators/DatePropertiesGenerator.kt @@ -6,6 +6,8 @@ import io.islandtime.codegen.SingleFileGenerator import io.islandtime.codegen.calendar import io.islandtime.codegen.descriptions.DateTimeDescription import io.islandtime.codegen.descriptions.DateTimeDescription.Date +import io.islandtime.codegen.descriptions.TemporalUnitDescription.DAYS +import io.islandtime.codegen.dsl.FileBuilder import io.islandtime.codegen.dsl.file import io.islandtime.codegen.internal import io.islandtime.codegen.measures @@ -19,201 +21,204 @@ private fun buildDatePropertiesFile() = file( fileName = "_DateProperties", jvmName = "DateTimesKt" ) { - DateTimeDescription.values().filter { it.isDateBased }.forEach { receiverClass -> - property(name = "weekOfMonth", returnType = Int::class) { - kdoc { "The week of the month (0-5) according to the ISO definition." } - receiver(receiverClass.typeName) - - if (receiverClass == Date) { - getter { - using("impl", internal("weekOfMonthImpl")) - "return %impl:T" - } - } else { - modifiers(KModifier.INLINE) - delegatesTo(receiverClass.datePropertyName) + DateTimeDescription.values() + .filter { it.isDateBased && it.smallestUnit <= DAYS } + .forEach { buildDatePropertiesForClass(it) } +} + +private fun FileBuilder.buildDatePropertiesForClass(receiverClass: DateTimeDescription) { + property(name = "weekOfMonth", returnType = Int::class) { + kdoc { "The week of the month (0-5) according to the ISO definition." } + receiver(receiverClass.typeName) + + if (receiverClass == Date) { + getter { + using("impl", internal("weekOfMonthImpl")) + "return %impl:T" } + } else { + modifiers(KModifier.INLINE) + delegatesTo(receiverClass.datePropertyName) } + } - function(name = "weekOfMonth") { - kdoc { "The week of the month (0-5) according to the week definition in [settings]." } - receiver(receiverClass.typeName) - argument("settings", calendar("WeekSettings")) - returns(Int::class) + function(name = "weekOfMonth") { + kdoc { "The week of the month (0-5) according to the week definition in [settings]." } + receiver(receiverClass.typeName) + argument("settings", calendar("WeekSettings")) + returns(Int::class) - if (receiverClass == Date) { - code { - using("impl", internal("weekOfMonthImpl")) - "return %impl:T(%settings:N)" - } - } else { - delegatesTo(receiverClass.datePropertyName) + if (receiverClass == Date) { + code { + using("impl", internal("weekOfMonthImpl")) + "return %impl:T(%settings:N)" } + } else { + delegatesTo(receiverClass.datePropertyName) } + } - property(name = "weekOfYear", returnType = Int::class) { - kdoc { - """ - The week of the year according to the ISO week definition. If the week number is associated with the - preceding year, `0` will be returned. + property(name = "weekOfYear", returnType = Int::class) { + kdoc { + """ + The week of the year according to the ISO week definition. If the week number is associated with the + preceding year, `0` will be returned. - To obtain the week number used in the ISO week date system, use [weekOfWeekBasedYear] instead. - - @see weekOfWeekBasedYear - """.trimIndent() - } + To obtain the week number used in the ISO week date system, use [weekOfWeekBasedYear] instead. + + @see weekOfWeekBasedYear + """.trimIndent() + } - receiver(receiverClass.typeName) + receiver(receiverClass.typeName) - if (receiverClass == Date) { - getter { - using("impl", internal("weekOfYearImpl")) - "return %impl:T" - } - } else { - modifiers(KModifier.INLINE) - delegatesTo(receiverClass.datePropertyName) + if (receiverClass == Date) { + getter { + using("impl", internal("weekOfYearImpl")) + "return %impl:T" } + } else { + modifiers(KModifier.INLINE) + delegatesTo(receiverClass.datePropertyName) } + } - function("weekOfYear") { - kdoc { - """ - The week of the year according to the week definition in [settings]. If the week number is - associated with the preceding year, `0` will be returned. + function("weekOfYear") { + kdoc { + """ + The week of the year according to the week definition in [settings]. If the week number is associated + with the preceding year, `0` will be returned. - To obtain the week number of the week-based year, use [weekOfWeekBasedYear] instead. - - @see weekOfWeekBasedYear - """.trimIndent() - } + To obtain the week number of the week-based year, use [weekOfWeekBasedYear] instead. + + @see weekOfWeekBasedYear + """.trimIndent() + } + + receiver(receiverClass.typeName) + argument("settings", calendar("WeekSettings")) + returns(Int::class) - receiver(receiverClass.typeName) - argument("settings", calendar("WeekSettings")) - returns(Int::class) - - if (receiverClass == Date) { - code { - using("impl", internal("weekOfYearImpl")) - "return %impl:T(%settings:N)" - } - } else { - delegatesTo(receiverClass.datePropertyName) + if (receiverClass == Date) { + code { + using("impl", internal("weekOfYearImpl")) + "return %impl:T(%settings:N)" } + } else { + delegatesTo(receiverClass.datePropertyName) } + } - property(name = "weekBasedYear", returnType = Int::class) { - kdoc { - """ - The week-based year used in the ISO [week·date·system](https://en.wikipedia.org/wiki/ISO_week_date). - This differs from the regular ISO year when the week number falls in the preceding or following - year. - - @see weekOfWeekBasedYear - """.trimIndent() - } + property(name = "weekBasedYear", returnType = Int::class) { + kdoc { + """ + The week-based year used in the ISO [week·date·system](https://en.wikipedia.org/wiki/ISO_week_date). + This differs from the regular ISO year when the week number falls in the preceding or following year. + + @see weekOfWeekBasedYear + """.trimIndent() + } - receiver(receiverClass.typeName) + receiver(receiverClass.typeName) - if (receiverClass == Date) { - getter { - using("impl", internal("weekBasedYearImpl")) - "return %impl:T" - } - } else { - modifiers(KModifier.INLINE) - delegatesTo(receiverClass.datePropertyName) + if (receiverClass == Date) { + getter { + using("impl", internal("weekBasedYearImpl")) + "return %impl:T" } + } else { + modifiers(KModifier.INLINE) + delegatesTo(receiverClass.datePropertyName) } + } - function(name = "weekBasedYear") { - kdoc { - """ - The week-based year according to the week definition in [settings]. This differs from the regular - ISO year when the week number falls in the preceding or following year. - - @see weekOfWeekBasedYear - """.trimIndent() - } + function(name = "weekBasedYear") { + kdoc { + """ + The week-based year according to the week definition in [settings]. This differs from the regular ISO + year when the week number falls in the preceding or following year. + + @see weekOfWeekBasedYear + """.trimIndent() + } + + receiver(receiverClass.typeName) + argument("settings", calendar("WeekSettings")) + returns(Int::class) - receiver(receiverClass.typeName) - argument("settings", calendar("WeekSettings")) - returns(Int::class) - - if (receiverClass == Date) { - code { - using("impl", internal("weekBasedYearImpl")) - "return %impl:T(%settings:N)" - } - } else { - delegatesTo(receiverClass.datePropertyName) + if (receiverClass == Date) { + code { + using("impl", internal("weekBasedYearImpl")) + "return %impl:T(%settings:N)" } + } else { + delegatesTo(receiverClass.datePropertyName) } + } - property(name = "weekOfWeekBasedYear", returnType = Int::class) { - kdoc { - """ - The week number used in the ISO [week·date·system](https://en.wikipedia.org/wiki/ISO_week_date). - - @see weekBasedYear - """.trimIndent() - } + property(name = "weekOfWeekBasedYear", returnType = Int::class) { + kdoc { + """ + The week number used in the ISO [week·date·system](https://en.wikipedia.org/wiki/ISO_week_date). + + @see weekBasedYear + """.trimIndent() + } - receiver(receiverClass.typeName) + receiver(receiverClass.typeName) - if (receiverClass == Date) { - getter { - using("impl", internal("weekOfWeekBasedYearImpl")) - "return %impl:T" - } - } else { - modifiers(KModifier.INLINE) - delegatesTo(receiverClass.datePropertyName) + if (receiverClass == Date) { + getter { + using("impl", internal("weekOfWeekBasedYearImpl")) + "return %impl:T" } + } else { + modifiers(KModifier.INLINE) + delegatesTo(receiverClass.datePropertyName) } + } - function(name = "weekOfWeekBasedYear") { - kdoc { - """ - The week number of the week-based year according to the week definition in [settings]. - - @see weekBasedYear - """.trimIndent() - } + function(name = "weekOfWeekBasedYear") { + kdoc { + """ + The week number of the week-based year according to the week definition in [settings]. + + @see weekBasedYear + """.trimIndent() + } + + receiver(receiverClass.typeName) + argument("settings", calendar("WeekSettings")) + returns(Int::class) - receiver(receiverClass.typeName) - argument("settings", calendar("WeekSettings")) - returns(Int::class) - - if (receiverClass == Date) { - code { - using("impl", internal("weekOfWeekBasedYearImpl")) - "return %impl:T(%settings:N)" - } - } else { - delegatesTo(receiverClass.datePropertyName) + if (receiverClass == Date) { + code { + using("impl", internal("weekOfWeekBasedYearImpl")) + "return %impl:T(%settings:N)" } + } else { + delegatesTo(receiverClass.datePropertyName) } + } - property(name = "lengthOfWeekBasedYear", returnType = measures("IntWeeks")) { - kdoc { - """ - The length of the ISO week-based year that this ${receiverClass.simpleName} falls in, either 52 or - 53 weeks. - """.trimIndent() - } + property(name = "lengthOfWeekBasedYear", returnType = measures("IntWeeks")) { + kdoc { + """ + The length of the ISO week-based year that this ${receiverClass.simpleName} falls in, either 52 or 53 + weeks. + """.trimIndent() + } - receiver(receiverClass.typeName) + receiver(receiverClass.typeName) - if (receiverClass == Date) { - getter { - using("impl", internal("lengthOfWeekBasedYear")) - "return %impl:T(weekBasedYear)" - } - } else { - modifiers(KModifier.INLINE) - delegatesTo(receiverClass.datePropertyName) + if (receiverClass == Date) { + getter { + using("impl", internal("lengthOfWeekBasedYear")) + "return %impl:T(weekBasedYear)" } + } else { + modifiers(KModifier.INLINE) + delegatesTo(receiverClass.datePropertyName) } } } \ No newline at end of file diff --git a/tools/code-generator/src/main/kotlin/io/islandtime/codegen/generators/TemporalUnitGenerator.kt b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/generators/TemporalUnitGenerator.kt index 7544fcd3a..654bebc00 100644 --- a/tools/code-generator/src/main/kotlin/io/islandtime/codegen/generators/TemporalUnitGenerator.kt +++ b/tools/code-generator/src/main/kotlin/io/islandtime/codegen/generators/TemporalUnitGenerator.kt @@ -61,7 +61,7 @@ fun TemporalUnitDescription.toFileSpec(): FileSpec { if (isTimeBased) { addAliasedImport(KOTLIN_DURATION_CLASS_NAME, "KotlinDuration") - addAliasedImport(ClassName("kotlin.time", lowerCaseName), "kotlin$pluralName") + addAliasedImport(ClassName("kotlin.time", lowerPluralName), "kotlin$pluralName") } listOf( @@ -160,7 +160,7 @@ abstract class TemporalUnitClassGenerator( fun build() = listOf(buildClass()) + allExtensionSpecs() fun buildClass() = io.islandtime.codegen.dsl.buildClassTypeSpec(className) { - addKdoc("A number of ${description.lowerCaseName}.") + addKdoc("A number of ${description.lowerPluralName}.") addModifiers(KModifier.INLINE) primaryConstructor(constructorFunSpec) addSuperinterface(ClassName("kotlin", "Comparable").parameterizedBy(className)) @@ -297,7 +297,7 @@ fun TemporalUnitClassGenerator.buildToKotlinDurationFunSpec() = addAnnotation(EXPERIMENTAL_TIME_CLASS_NAME) addKdoc("Convert to a [${KOTLIN_DURATION_CLASS_NAME.canonicalName}].") returns(KOTLIN_DURATION_CLASS_NAME) - addStatement("return %N.%T", valuePropertySpec, ClassName("kotlin.time", description.lowerCaseName)) + addStatement("return %N.%T", valuePropertySpec, ClassName("kotlin.time", description.lowerPluralName)) } fun TemporalUnitClassGenerator.buildFractionalToStringCodeBlock() = io.islandtime.codegen.dsl.buildCodeBlock { @@ -501,7 +501,7 @@ fun TemporalUnitClassGenerator.buildIntPlusMinusOperatorFunSpec( ) = io.islandtime.codegen.dsl.buildFunSpec(plusOrMinusOperator.functionName) { addModifiers(KModifier.OPERATOR) - val amount = ParameterSpec(conversion.toUnit.lowerCaseName, conversion.toUnit.intClassName) + val amount = ParameterSpec(conversion.toUnit.lowerPluralName, conversion.toUnit.intClassName) addParameter(amount) when (conversion.operator) { @@ -569,7 +569,7 @@ fun TemporalUnitClassGenerator.buildLongPlusMinusOperatorFunSpec( ) = io.islandtime.codegen.dsl.buildFunSpec(plusOrMinusOperator.functionName) { addModifiers(KModifier.OPERATOR) - val amount = ParameterSpec(conversion.toUnit.lowerCaseName, conversion.toUnit.longClassName) + val amount = ParameterSpec(conversion.toUnit.lowerPluralName, conversion.toUnit.longClassName) addParameter(amount) when (conversion.operator) { @@ -754,7 +754,7 @@ fun TemporalUnitClassGenerator.buildInBiggerUnitConversionSpecs( conversion.toUnit.inWholeUnitPropertyName, conversion.toUnit.classNameFor(primitive) ) { - addKdoc("Convert to whole ${conversion.toUnit.lowerCaseName}.") + addKdoc("Convert to whole ${conversion.toUnit.lowerPluralName}.") getter( io.islandtime.codegen.dsl.buildGetterFunSpec { val statement = buildString { @@ -764,7 +764,7 @@ fun TemporalUnitClassGenerator.buildInBiggerUnitConversionSpecs( append(".toInt()") } - append(".${conversion.toUnit.lowerCaseName}") + append(".${conversion.toUnit.lowerPluralName}") } addStatement(statement, valuePropertySpec, conversion.propertyClassName) @@ -789,7 +789,7 @@ fun TemporalUnitClassGenerator.buildInSmallerUnitConversionSpecs( ) { addKdoc( """ - Convert to ${conversion.toUnit.lowerCaseName}. + Convert to ${conversion.toUnit.lowerPluralName}. @throws ArithmeticException if overflow occurs """.trimIndent() ) @@ -797,9 +797,9 @@ fun TemporalUnitClassGenerator.buildInSmallerUnitConversionSpecs( io.islandtime.codegen.dsl.buildGetterFunSpec { addStatement( if (primitive == Int::class && conversion.toUnit.forceLongInOperators) { - "return (%N.toLong() %T %T).${conversion.toUnit.lowerCaseName}" + "return (%N.toLong() %T %T).${conversion.toUnit.lowerPluralName}" } else { - "return (%N %T %T).${conversion.toUnit.lowerCaseName}" + "return (%N %T %T).${conversion.toUnit.lowerPluralName}" }, valuePropertySpec, internal("timesExact"), @@ -820,22 +820,22 @@ fun TemporalUnitClassGenerator.buildInSmallerUnitConversionSpecs( ) { if (overflowSafeMethodRequired) { addModifiers(KModifier.INTERNAL) - addKdoc("Convert to ${conversion.toUnit.lowerCaseName} without checking for overflow.") + addKdoc("Convert to ${conversion.toUnit.lowerPluralName} without checking for overflow.") } else { - addKdoc("Convert to ${conversion.toUnit.lowerCaseName}.") + addKdoc("Convert to ${conversion.toUnit.lowerPluralName}.") } getter( io.islandtime.codegen.dsl.buildGetterFunSpec { if (primitive == Int::class && conversion.toUnit.forceLongInOperators) { addStatement( - "return (%N.toLong() * %T).${conversion.toUnit.lowerCaseName}", + "return (%N.toLong() * %T).${conversion.toUnit.lowerPluralName}", valuePropertySpec, conversion.propertyClassName ) } else { addStatement( - "return (%N * %T).${conversion.toUnit.lowerCaseName}", + "return (%N * %T).${conversion.toUnit.lowerPluralName}", valuePropertySpec, conversion.propertyClassName ) @@ -848,7 +848,7 @@ fun TemporalUnitClassGenerator.buildInSmallerUnitConversionSpecs( } fun TemporalUnitClassGenerator.buildPrimitiveExtensionPropertySpec() = io.islandtime.codegen.dsl.buildPropertySpec( - description.lowerCaseName, + description.lowerPluralName, className ) { addKdoc("Convert to [%T].", className) @@ -865,12 +865,12 @@ fun TemporalUnitClassGenerator.buildTimesExtensionFunSpec( addModifiers(KModifier.OPERATOR) addKdoc( """ - Multiply by a number of ${description.lowerCaseName}. + Multiply by a number of ${description.lowerPluralName}. @throws ArithmeticException if overflow occurs """.trimIndent() ) - val unit = ParameterSpec(description.lowerCaseName, className) + val unit = ParameterSpec(description.lowerPluralName, className) addParameter(unit) addStatement("return %N * this", unit) } @@ -903,7 +903,7 @@ fun TemporalUnitClassGenerator.buildToComponentsFunctions(): List { val lambdaParameters = allComponentUnits.map { currentUnit -> io.islandtime.codegen.dsl.buildParameterSpec( - currentUnit.lowerCaseName, + currentUnit.lowerPluralName, if (currentUnit == biggestUnit && primitive == Long::class) { currentUnit.longClassName } else { @@ -959,12 +959,12 @@ fun TemporalUnitClassGenerator.buildToComponentsFunctions(): List { } addNamedCode( - "val ${currentUnit.lowerCaseName} = ${conversionString}.${currentUnit.lowerCaseName}\n", + "val ${currentUnit.lowerPluralName} = ${conversionString}.${currentUnit.lowerPluralName}\n", arguments ) } - val allVariableNames = allComponentUnits.joinToString(", ") { it.lowerCaseName } + val allVariableNames = allComponentUnits.joinToString(", ") { it.lowerPluralName } addStatement("return action($allVariableNames)") } }