Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kotlin: Improve access of companions object fields/methods in Java #201

Merged
merged 1 commit into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions generate/template/astronomy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,15 @@ class Time private constructor(
internal fun julianMillennia() = tt / DAYS_PER_MILLENNIUM

companion object {
@JvmStatic
private val origin = GregorianCalendar(TimeZoneUtc).also {
Copy link
Contributor Author

@ebraminio ebraminio Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably only public fields and methods should be marked with static but well maybe it instructs the compiler to optimize companion object altogether or not, not sure but sure it should be harmless.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I seem to remember we talked about whether we want to get rid of using GregorianCalendar, because it brings in a dependency on Java. Is that correct? I think you said if someone wants to use the Kotlin version of Astronomy Engine to create WebAssembly, this could be a problem. I think there a few other minor uses of Java symbols. It is possible to do something like the C version, where we make our own date/time functions from scratch.

Copy link
Contributor Author

@ebraminio ebraminio Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sure, you can just take unix timestamp, the core data java.util.Date holds, https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#getTime()

This proposed change is roughly separate matter from what this change is about, https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-inline/ is considered to be common between Kotlin targets mainly for Java support for sure.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I think you meant @JvmStatic, not @JvmInline, but it looks like @JvmStatic is also safe to use outside the JVM (if I understand the documentation correctly). I just keep thinking about this, and yes, I understand it is a separate issue. I just wanted to understand it. Thank you!

it.set(2000, 0, 1, 12, 0, 0)
it.set(Calendar.MILLISECOND, 0)
}.time

private const val MILLIS_PER_DAY = 24 * 3600 * 1000

@JvmStatic
private val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").also {
it.timeZone = TimeZoneUtc
}
Expand All @@ -498,6 +500,7 @@ class Time private constructor(
*
* @param tt The number of days after the J2000 epoch.
*/
@JvmStatic
fun fromTerrestrialTime(tt: Double): Time = Time(universalTime(tt), tt)
}
}
Expand Down Expand Up @@ -547,6 +550,7 @@ internal data class TerseVector(var x: Double, var y: Double, var z: Double) {
}

companion object {
@JvmStatic
fun zero() = TerseVector(0.0, 0.0, 0.0)
}
}
Expand Down Expand Up @@ -1004,7 +1008,8 @@ class RotationMatrix(
* This matrix can be the starting point for other operations,
* such as calling a series of [RotationMatrix.combine] or [RotationMatrix.pivot].
*/
fun identity() = RotationMatrix (
@JvmStatic
fun identity() = RotationMatrix(
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
Expand Down Expand Up @@ -1989,7 +1994,7 @@ internal fun peakMoonShadow(searchCenterTime: Time): ShadowInfo {
val window = 0.03 // days before/after new moon to search for minimum shadow distance
val t1 = searchCenterTime.addDays(-window)
val t2 = searchCenterTime.addDays(+window)
val tx = search(moonShadowSlopeContext, t1, t2, 1.0) ?:
val tx = search(t1, t2, 1.0, moonShadowSlopeContext) ?:
Copy link
Contributor Author

@ebraminio ebraminio Apr 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should've been done in the previous PR, not sure how I've missed it...

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I just pushed this fix already. I figured we were both changing things at the same time?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, and great that git didn't need a rebase! :)

throw InternalError("Failed to find Moon peak shadow event.")
return moonShadow(tx)
}
Expand Down
3 changes: 3 additions & 0 deletions source/kotlin/doc/-rotation-matrix/-companion/identity.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# identity

[jvm]\

@[JvmStatic](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-static/index.html)

fun [identity](identity.md)(): [RotationMatrix](../index.md)

Creates an identity rotation matrix.
Expand Down
2 changes: 1 addition & 1 deletion source/kotlin/doc/-rotation-matrix/-companion/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ object [Companion](index.md)

| Name | Summary |
|---|---|
| [identity](identity.md) | [jvm]<br>fun [identity](identity.md)(): [RotationMatrix](../index.md)<br>Creates an identity rotation matrix. |
| [identity](identity.md) | [jvm]<br>@[JvmStatic](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-static/index.html)<br>fun [identity](identity.md)(): [RotationMatrix](../index.md)<br>Creates an identity rotation matrix. |
3 changes: 3 additions & 0 deletions source/kotlin/doc/-time/-companion/from-terrestrial-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
# fromTerrestrialTime

[jvm]\

@[JvmStatic](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-static/index.html)

fun [fromTerrestrialTime](from-terrestrial-time.md)(tt: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html)): [Time](../index.md)

Creates a Time object from a Terrestrial Time day value.
Expand Down
2 changes: 1 addition & 1 deletion source/kotlin/doc/-time/-companion/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ object [Companion](index.md)

| Name | Summary |
|---|---|
| [fromTerrestrialTime](from-terrestrial-time.md) | [jvm]<br>fun [fromTerrestrialTime](from-terrestrial-time.md)(tt: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html)): [Time](../index.md)<br>Creates a Time object from a Terrestrial Time day value. |
| [fromTerrestrialTime](from-terrestrial-time.md) | [jvm]<br>@[JvmStatic](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-static/index.html)<br>fun [fromTerrestrialTime](from-terrestrial-time.md)(tt: [Double](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-double/index.html)): [Time](../index.md)<br>Creates a Time object from a Terrestrial Time day value. |
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,15 @@ class Time private constructor(
internal fun julianMillennia() = tt / DAYS_PER_MILLENNIUM

companion object {
@JvmStatic
private val origin = GregorianCalendar(TimeZoneUtc).also {
it.set(2000, 0, 1, 12, 0, 0)
it.set(Calendar.MILLISECOND, 0)
}.time

private const val MILLIS_PER_DAY = 24 * 3600 * 1000

@JvmStatic
private val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").also {
it.timeZone = TimeZoneUtc
}
Expand All @@ -498,6 +500,7 @@ class Time private constructor(
*
* @param tt The number of days after the J2000 epoch.
*/
@JvmStatic
fun fromTerrestrialTime(tt: Double): Time = Time(universalTime(tt), tt)
}
}
Expand Down Expand Up @@ -547,6 +550,7 @@ internal data class TerseVector(var x: Double, var y: Double, var z: Double) {
}

companion object {
@JvmStatic
fun zero() = TerseVector(0.0, 0.0, 0.0)
}
}
Expand Down Expand Up @@ -1004,7 +1008,8 @@ class RotationMatrix(
* This matrix can be the starting point for other operations,
* such as calling a series of [RotationMatrix.combine] or [RotationMatrix.pivot].
*/
fun identity() = RotationMatrix (
@JvmStatic
fun identity() = RotationMatrix(
1.0, 0.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0
Expand Down Expand Up @@ -1989,7 +1994,7 @@ internal fun peakMoonShadow(searchCenterTime: Time): ShadowInfo {
val window = 0.03 // days before/after new moon to search for minimum shadow distance
val t1 = searchCenterTime.addDays(-window)
val t2 = searchCenterTime.addDays(+window)
val tx = search(moonShadowSlopeContext, t1, t2, 1.0) ?:
val tx = search(t1, t2, 1.0, moonShadowSlopeContext) ?:
throw InternalError("Failed to find Moon peak shadow event.")
return moonShadow(tx)
}
Expand Down