Skip to content

Commit 8ddfac5

Browse files
committed
Update temporal type annotations.
1 parent 275d568 commit 8ddfac5

File tree

5 files changed

+46
-49
lines changed

5 files changed

+46
-49
lines changed

plot-api/src/commonMain/kotlin/org/jetbrains/letsPlot/intern/ToSpecConverters.kt

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -429,8 +429,8 @@ private fun inferSeriesType(data: Any?): String {
429429
// types.size == 1 means all elements are of the same type, so we can take any (let's take the first one)
430430
val value = l.first()
431431

432-
return if (JvmStandardizing.isDateTimeJvm(value)) {
433-
SeriesAnnotation.Types.DATE_TIME
432+
return if (JvmStandardizing.isJvm(value)) {
433+
JvmStandardizing.typeAnnotation(value)
434434
} else {
435435
when (value) {
436436
is Byte -> SeriesAnnotation.Types.INTEGER
@@ -442,8 +442,8 @@ private fun inferSeriesType(data: Any?): String {
442442
is String -> SeriesAnnotation.Types.STRING
443443
is Boolean -> SeriesAnnotation.Types.BOOLEAN
444444
is kotlinx.datetime.Instant -> SeriesAnnotation.Types.DATE_TIME
445-
is kotlinx.datetime.LocalDate -> SeriesAnnotation.Types.DATE_TIME
446-
is kotlinx.datetime.LocalTime -> SeriesAnnotation.Types.DATE_TIME
445+
is kotlinx.datetime.LocalDate -> SeriesAnnotation.Types.DATE
446+
is kotlinx.datetime.LocalTime -> SeriesAnnotation.Types.TIME
447447
is kotlinx.datetime.LocalDateTime -> SeriesAnnotation.Types.DATE_TIME
448448
else -> SeriesAnnotation.Types.UNKNOWN
449449
}
@@ -458,15 +458,3 @@ private fun createGeoDataframeAnnotation(data: SpatialDataset): Map<String, Any>
458458
)
459459
)
460460
}
461-
462-
463-
//private fun mergeThemeOptions(m0: Map<String, Any>, m1: Map<String, Any>): Map<String, Any> {
464-
// val overlappingKeys = m0.keys.intersect(m1.keys)
465-
// val keysToMerge = overlappingKeys.filter {
466-
// m0[it] is Map<*, *> && m1[it] is Map<*, *>
467-
// }
468-
// val m2 = keysToMerge.map {
469-
// it to (m0[it] as Map<*, *> + m1[it] as Map<*, *>)
470-
// }.toMap()
471-
// return m0 + m1 + m2
472-
//}

plot-api/src/commonMain/kotlin/org/jetbrains/letsPlot/intern/standardizing/JvmStandardizing.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ package org.jetbrains.letsPlot.intern.standardizing
77

88
expect object JvmStandardizing {
99
fun isJvm(o: Any): Boolean
10-
fun isDateTimeJvm(o: Any): Boolean
10+
fun typeAnnotation(o: Any): String
1111
fun standardize(o: Any): Any
1212
}

plot-api/src/jsMain/kotlin/org/jetbrains/letsPlot/intern/standardizing/JvmStandardizing.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
package org.jetbrains.letsPlot.intern.standardizing
77

88
actual object JvmStandardizing {
9-
actual fun isDateTimeJvm(o: Any): Boolean {
10-
return false
11-
}
129
actual fun isJvm(o: Any): Boolean {
1310
return false
1411
}
1512

13+
actual fun typeAnnotation(o: Any): String {
14+
throw IllegalStateException("Not supported in Kotlin/JS")
15+
}
16+
1617
actual fun standardize(o: Any): Any {
1718
throw IllegalStateException("Not supported in Kotlin/JS")
1819
}

plot-api/src/jvmMain/kotlin/org/jetbrains/letsPlot/intern/standardizing/JvmStandardizing.kt

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.jetbrains.letsPlot.intern.standardizing
77

8+
import org.jetbrains.letsPlot.core.spec.Option.Meta.SeriesAnnotation
89
import java.time.*
910
import java.util.*
1011

@@ -16,7 +17,22 @@ private val AWT_PRESENT: Boolean = try {
1617
}
1718

1819
actual object JvmStandardizing {
19-
actual fun isDateTimeJvm(o: Any): Boolean {
20+
actual fun typeAnnotation(o: Any): String {
21+
return when (o) {
22+
is Date -> SeriesAnnotation.Types.DATE_TIME
23+
is Instant -> SeriesAnnotation.Types.DATE_TIME
24+
is ZonedDateTime -> SeriesAnnotation.Types.DATE_TIME
25+
is OffsetDateTime -> SeriesAnnotation.Types.DATE_TIME
26+
is LocalDate -> SeriesAnnotation.Types.DATE
27+
is LocalTime -> SeriesAnnotation.Types.TIME
28+
is LocalDateTime -> SeriesAnnotation.Types.DATE_TIME
29+
else -> SeriesAnnotation.Types.UNKNOWN
30+
}
31+
}
32+
33+
actual fun isJvm(o: Any): Boolean {
34+
if (AWT_PRESENT && o is java.awt.Color) return true
35+
2036
return when (o) {
2137
is Date -> true
2238
is Instant -> true
@@ -29,34 +45,24 @@ actual object JvmStandardizing {
2945
}
3046
}
3147

32-
actual fun isJvm(o: Any): Boolean {
33-
return when {
34-
isDateTimeJvm(o) -> true
35-
AWT_PRESENT && o is java.awt.Color -> true
36-
else -> false
37-
}
38-
}
39-
4048
actual fun standardize(o: Any): Any {
41-
if (AWT_PRESENT &&
42-
o is java.awt.Color
43-
) {
49+
if (AWT_PRESENT && o is java.awt.Color) {
4450
return "#%02x%02x%02x".format(o.red, o.green, o.blue)
4551
}
4652

4753
return when (o) {
48-
is Date -> o.time.toDouble()
49-
is Instant -> o.toEpochMilli().toDouble()
50-
is ZonedDateTime -> o.toInstant().toEpochMilli().toDouble()
51-
is OffsetDateTime -> o.toInstant().toEpochMilli().toDouble()
52-
is LocalDate -> o.atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli().toDouble()
53-
is LocalTime -> LocalDateTime.of(LocalDate.EPOCH, o).toInstant(ZoneOffset.UTC).toEpochMilli().toDouble()
54-
is LocalDateTime -> o.toInstant(ZoneOffset.UTC).toEpochMilli().toDouble()
55-
else -> unsupportedTypeError(o)
56-
}
57-
}
58-
59-
private fun unsupportedTypeError(o: Any) {
60-
throw IllegalArgumentException("Can't standardize value \"$o\" of type ${o::class.qualifiedName} as string or number.")
54+
is Date -> o.time
55+
is Instant -> o.toEpochMilli()
56+
is ZonedDateTime -> o.toInstant().toEpochMilli()
57+
is OffsetDateTime -> o.toInstant().toEpochMilli()
58+
is LocalDate -> o.atStartOfDay(ZoneOffset.UTC).toInstant().toEpochMilli()
59+
is LocalTime -> LocalDateTime.of(LocalDate.EPOCH, o).toInstant(ZoneOffset.UTC).toEpochMilli()
60+
is LocalDateTime -> o.toInstant(ZoneOffset.UTC).toEpochMilli()
61+
else -> {
62+
throw IllegalArgumentException(
63+
"Can't standardize value \"$o\" of type ${o::class.qualifiedName} as string or number."
64+
)
65+
}
66+
}.toDouble()
6167
}
6268
}

plot-api/src/jvmTest/kotlin/org/jetbrains/letsPlot/SeriesAnnotationTest.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class SeriesAnnotationTest {
5959
"java-instant-column" to listOf(Instant.parse("2021-01-01T00:00:00Z")),
6060
"java-date-column" to listOf(java.util.Date.from(java.time.Instant.parse("2021-01-01T00:00:00Z"))),
6161
"java-zoned-datetime-column" to listOf(java.time.ZonedDateTime.parse("2021-01-01T00:00:00Z")),
62+
"java-offset-datetime-column" to listOf(java.time.OffsetDateTime.parse("2021-01-01T00:00:00Z")),
6263
"java-local-date-column" to listOf(java.time.LocalDate.parse("2021-01-01")),
6364
"java-local-time-column" to listOf(java.time.LocalTime.parse("12:34:56")),
6465
"java-local-datetime-column" to listOf(java.time.LocalDateTime.parse("2021-01-01T12:34:56")),
@@ -86,13 +87,14 @@ class SeriesAnnotationTest {
8687
seriesAnnotation(column = "java-instant-column", type = Types.DATE_TIME),
8788
seriesAnnotation(column = "java-date-column", type = Types.DATE_TIME),
8889
seriesAnnotation(column = "java-zoned-datetime-column", type = Types.DATE_TIME),
89-
seriesAnnotation(column = "java-local-date-column", type = Types.DATE_TIME),
90-
seriesAnnotation(column = "java-local-time-column", type = Types.DATE_TIME),
90+
seriesAnnotation(column = "java-offset-datetime-column", type = Types.DATE_TIME),
91+
seriesAnnotation(column = "java-local-date-column", type = Types.DATE),
92+
seriesAnnotation(column = "java-local-time-column", type = Types.TIME),
9193
seriesAnnotation(column = "java-local-datetime-column", type = Types.DATE_TIME),
9294

9395
seriesAnnotation(column = "kotlin-instant-column", type = Types.DATE_TIME),
94-
seriesAnnotation(column = "kotlin-local-date-column", type = Types.DATE_TIME),
95-
seriesAnnotation(column = "kotlin-local-time-column", type = Types.DATE_TIME),
96+
seriesAnnotation(column = "kotlin-local-date-column", type = Types.DATE),
97+
seriesAnnotation(column = "kotlin-local-time-column", type = Types.TIME),
9698
seriesAnnotation(column = "kotlin-local-datetime-column", type = Types.DATE_TIME),
9799
)
98100
)

0 commit comments

Comments
 (0)