Skip to content

Commit

Permalink
Enable explicit API mode (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
dellisd authored May 7, 2024
1 parent f56172b commit 812654b
Show file tree
Hide file tree
Showing 34 changed files with 208 additions and 209 deletions.
2 changes: 2 additions & 0 deletions geojson/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ plugins {
}

kotlin {
explicitApi()

jvm {
compilations.create("bench")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ import kotlinx.serialization.Serializable
*/
@Serializable(with = BoundingBoxSerializer::class)
@Suppress("MagicNumber")
class BoundingBox constructor(val coordinates: DoubleArray) {
public class BoundingBox constructor(public val coordinates: DoubleArray) {
init {
require(coordinates.size == 4 || coordinates.size == 6) {
"Bounding Box coordinates must either have 4 or 6 values"
}
}

constructor(west: Double, south: Double, east: Double, north: Double) : this(
public constructor(west: Double, south: Double, east: Double, north: Double) : this(
doubleArrayOf(west, south, east, north)
)

constructor(coordinates: List<Double>) : this(coordinates.toDoubleArray())
public constructor(coordinates: List<Double>) : this(coordinates.toDoubleArray())

constructor(
public constructor(
west: Double,
south: Double,
minAltitude: Double,
Expand All @@ -46,27 +46,27 @@ class BoundingBox constructor(val coordinates: DoubleArray) {
maxAltitude: Double
) : this(doubleArrayOf(west, south, minAltitude, east, north, maxAltitude))

constructor(southwest: Position, northeast: Position) : this(
public constructor(southwest: Position, northeast: Position) : this(
when (southwest.hasAltitude && northeast.hasAltitude) {
true -> southwest.coordinates + northeast.coordinates
false -> doubleArrayOf(southwest.longitude, southwest.latitude, northeast.longitude, northeast.latitude)
}
)

val southwest: Position
public val southwest: Position
get() = when (hasAltitude) {
true -> Position(coordinates[0], coordinates[1], coordinates[2])
false -> Position(coordinates[0], coordinates[1])
}

val northeast: Position
public val northeast: Position
get() = when (hasAltitude) {
true -> Position(coordinates[3], coordinates[4], coordinates[5])
false -> Position(coordinates[2], coordinates[3])
}

operator fun component1(): Position = southwest
operator fun component2(): Position = northeast
public operator fun component1(): Position = southwest
public operator fun component2(): Position = northeast

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -87,9 +87,9 @@ class BoundingBox constructor(val coordinates: DoubleArray) {
return "BoundingBox(southwest=$southwest, northeast=$northeast)"
}

fun json(): String = coordinates.jsonJoin()
public fun json(): String = coordinates.jsonJoin()
}

@Suppress("MagicNumber")
val BoundingBox.hasAltitude: Boolean
public val BoundingBox.hasAltitude: Boolean
get() = coordinates.size == 6
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package io.github.dellisd.spatialk.geojson
@RequiresOptIn(message = "This API is experimental. It may be changed in the future without notice.")
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
annotation class ExperimentalGeoJsonApi
public annotation class ExperimentalGeoJsonApi
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,40 @@ import kotlin.jvm.JvmStatic
*/
@Suppress("TooManyFunctions")
@Serializable(with = FeatureSerializer::class)
class Feature(
val geometry: Geometry?,
public class Feature(
public val geometry: Geometry?,
properties: Map<String, JsonElement> = emptyMap(),
val id: String? = null,
public val id: String? = null,
override val bbox: BoundingBox? = null
) : GeoJson {
private val _properties: MutableMap<String, JsonElement> = properties.toMutableMap()
val properties: Map<String, JsonElement> get() = _properties
public val properties: Map<String, JsonElement> get() = _properties

fun setStringProperty(key: String, value: String?) {
public fun setStringProperty(key: String, value: String?) {
_properties[key] = JsonPrimitive(value)
}

fun setNumberProperty(key: String, value: Number?) {
public fun setNumberProperty(key: String, value: Number?) {
_properties[key] = JsonPrimitive(value)
}

fun setBooleanProperty(key: String, value: Boolean?) {
public fun setBooleanProperty(key: String, value: Boolean?) {
_properties[key] = JsonPrimitive(value)
}

fun setJsonProperty(key: String, value: JsonElement) {
public fun setJsonProperty(key: String, value: JsonElement) {
_properties[key] = value
}

fun getStringProperty(key: String): String? = properties[key]?.jsonPrimitive?.content
public fun getStringProperty(key: String): String? = properties[key]?.jsonPrimitive?.content

fun getNumberProperty(key: String): Number? = properties[key]?.jsonPrimitive?.double
public fun getNumberProperty(key: String): Number? = properties[key]?.jsonPrimitive?.double

fun getBooleanProperty(key: String): Boolean? = properties[key]?.jsonPrimitive?.boolean
public fun getBooleanProperty(key: String): Boolean? = properties[key]?.jsonPrimitive?.boolean

fun getJsonProperty(key: String): JsonElement? = properties[key]
public fun getJsonProperty(key: String): JsonElement? = properties[key]

fun removeProperty(key: String): Any? = _properties.remove(key)
public fun removeProperty(key: String): Any? = _properties.remove(key)

/**
* Gets the value of the property with the given [key].
Expand All @@ -74,7 +74,7 @@ class Feature(
* @return The value of the property cast to [T]?, or null if the key had no value.
*/
@JvmName("getPropertyCast")
inline fun <reified T : Any?> getProperty(key: String) = properties[key] as T?
public inline fun <reified T : Any?> getProperty(key: String): T? = properties[key] as T?

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -98,10 +98,10 @@ class Feature(
return result
}

operator fun component1() = geometry
operator fun component2() = properties
operator fun component3() = id
operator fun component4() = bbox
public operator fun component1(): Geometry? = geometry
public operator fun component2(): Map<String, JsonElement> = properties
public operator fun component3(): String? = id
public operator fun component4(): BoundingBox? = bbox

override fun toString(): String = json()

Expand All @@ -115,16 +115,16 @@ class Feature(
)
}}"""

fun copy(
public fun copy(
geometry: Geometry? = this.geometry,
properties: Map<String, JsonElement> = this.properties,
id: String? = this.id,
bbox: BoundingBox? = this.bbox
): Feature = Feature(geometry, properties, id, bbox)

companion object {
public companion object {
@JvmStatic
fun fromJson(json: String): Feature = fromJson(Json.decodeFromString(JsonObject.serializer(), json))
public fun fromJson(json: String): Feature = fromJson(Json.decodeFromString(JsonObject.serializer(), json))

@JvmStatic
public fun fromJsonOrNull(json: String): Feature? = try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import kotlin.jvm.JvmStatic
* @property features The collection of [Feature] objects stored in this collection
*/
@Serializable(with = FeatureCollectionSerializer::class)
class FeatureCollection(
val features: List<Feature> = emptyList(),
public class FeatureCollection(
public val features: List<Feature> = emptyList(),
override val bbox: BoundingBox? = null
) : Collection<Feature> by features, GeoJson {

constructor(vararg features: Feature, bbox: BoundingBox? = null) : this(features.toMutableList(), bbox)
public constructor(vararg features: Feature, bbox: BoundingBox? = null) : this(features.toMutableList(), bbox)

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -52,10 +52,10 @@ class FeatureCollection(
override fun json(): String =
"""{"type":"FeatureCollection",${bbox.jsonProp()}"features":${features.jsonJoin { it.json() }}}"""

operator fun component1(): List<Feature> = features
operator fun component2(): BoundingBox? = bbox
public operator fun component1(): List<Feature> = features
public operator fun component2(): BoundingBox? = bbox

companion object {
public companion object {
@JvmStatic
public fun fromJson(json: String): FeatureCollection =
fromJson(Json.decodeFromString(JsonObject.serializer(), json))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package io.github.dellisd.spatialk.geojson
*
* @property bbox An optional bounding box used to represent the limits of the object's geometry.
*/
interface GeoJson {
val bbox: BoundingBox?
public interface GeoJson {
public val bbox: BoundingBox?

/**
* Gets a JSON representation of this object.
* @return JSON representation
*/
fun json(): String
public fun json(): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import kotlinx.serialization.json.jsonPrimitive
import kotlin.jvm.JvmStatic

@Serializable(with = GeometrySerializer::class)
sealed class Geometry protected constructor() : GeoJson {
public sealed class Geometry protected constructor() : GeoJson {
abstract override val bbox: BoundingBox?

override fun toString(): String = json()

companion object {
public companion object {
@JvmStatic
public fun fromJson(json: String): Geometry = fromJson(Json.decodeFromString(JsonObject.serializer(), json))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import kotlin.jvm.JvmStatic

@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(with = GeometrySerializer::class)
class GeometryCollection @JvmOverloads constructor(
val geometries: List<Geometry>,
public class GeometryCollection @JvmOverloads constructor(
public val geometries: List<Geometry>,
override val bbox: BoundingBox? = null
) : Geometry(), Collection<Geometry> by geometries {
@JvmOverloads
constructor(vararg geometries: Geometry, bbox: BoundingBox? = null) : this(geometries.toList(), bbox)
public constructor(vararg geometries: Geometry, bbox: BoundingBox? = null) : this(geometries.toList(), bbox)

override fun equals(other: Any?): Boolean {
if (this === other) return true
Expand All @@ -43,7 +43,7 @@ class GeometryCollection @JvmOverloads constructor(
override fun json(): String =
"""{"type":"GeometryCollection",${bbox.jsonProp()}"geometries":${geometries.jsonJoin { it.json() }}}"""

companion object {
public companion object {
@JvmStatic
public fun fromJson(json: String): GeometryCollection =
fromJson(Json.decodeFromString(JsonObject.serializer(), json))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import kotlin.jvm.JvmStatic

@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(with = GeometrySerializer::class)
class LineString @JvmOverloads constructor(
val coordinates: List<Position>,
public class LineString @JvmOverloads constructor(
public val coordinates: List<Position>,
override val bbox: BoundingBox? = null
) : Geometry() {
@JvmOverloads
constructor(vararg coordinates: Position, bbox: BoundingBox? = null) : this(coordinates.toList(), bbox)
public constructor(vararg coordinates: Position, bbox: BoundingBox? = null) : this(coordinates.toList(), bbox)

@JvmOverloads
constructor(
public constructor(
coordinates: Array<DoubleArray>,
bbox: BoundingBox? = null
) : this(coordinates.map(::Position), bbox)
Expand Down Expand Up @@ -53,7 +53,7 @@ class LineString @JvmOverloads constructor(
override fun json(): String =
"""{"type":"LineString",${bbox.jsonProp()}"coordinates":${coordinates.jsonJoin(transform = Position::json)}}"""

companion object {
public companion object {
@JvmStatic
public fun fromJson(json: String): LineString = fromJson(Json.decodeFromString(JsonObject.serializer(), json))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import kotlin.jvm.JvmStatic

@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(with = GeometrySerializer::class)
class MultiLineString @JvmOverloads constructor(
val coordinates: List<List<Position>>,
public class MultiLineString @JvmOverloads constructor(
public val coordinates: List<List<Position>>,
override val bbox: BoundingBox? = null
) : Geometry() {
@JvmOverloads
constructor(vararg coordinates: List<Position>, bbox: BoundingBox? = null) : this(coordinates.toList(), bbox)
public constructor(vararg coordinates: List<Position>, bbox: BoundingBox? = null) : this(coordinates.toList(), bbox)

@JvmOverloads
constructor(
public constructor(
coordinates: Array<Array<DoubleArray>>,
bbox: BoundingBox? = null
) : this(coordinates.map { it.map(::Position) }, bbox)
Expand Down Expand Up @@ -59,7 +59,7 @@ class MultiLineString @JvmOverloads constructor(
}
}}"""

companion object {
public companion object {
@JvmStatic
public fun fromJson(json: String): MultiLineString =
fromJson(Json.decodeFromString(JsonObject.serializer(), json))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import kotlin.jvm.JvmStatic

@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(with = GeometrySerializer::class)
class MultiPoint @JvmOverloads constructor(
val coordinates: List<Position>,
public class MultiPoint @JvmOverloads constructor(
public val coordinates: List<Position>,
override val bbox: BoundingBox? = null
) : Geometry() {
@JvmOverloads
constructor(vararg coordinates: Position, bbox: BoundingBox? = null) : this(coordinates.toList(), bbox)
public constructor(vararg coordinates: Position, bbox: BoundingBox? = null) : this(coordinates.toList(), bbox)

@JvmOverloads
constructor(
public constructor(
coordinates: Array<DoubleArray>,
bbox: BoundingBox? = null
) : this(coordinates.map(::Position), bbox)
Expand All @@ -49,7 +49,7 @@ class MultiPoint @JvmOverloads constructor(
override fun json(): String =
"""{"type":"MultiPoint",${bbox.jsonProp()}"coordinates":${coordinates.jsonJoin(transform = Position::json)}}"""

companion object {
public companion object {
@JvmStatic
public fun fromJson(json: String): MultiPoint = fromJson(Json.decodeFromString(JsonObject.serializer(), json))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import kotlin.jvm.JvmStatic

@Suppress("SERIALIZER_TYPE_INCOMPATIBLE")
@Serializable(with = GeometrySerializer::class)
class MultiPolygon @JvmOverloads constructor(
val coordinates: List<List<List<Position>>>,
public class MultiPolygon @JvmOverloads constructor(
public val coordinates: List<List<List<Position>>>,
override val bbox: BoundingBox? = null
) : Geometry() {
@JvmOverloads
constructor(vararg coordinates: List<List<Position>>, bbox: BoundingBox? = null) : this(coordinates.toList(), bbox)
public constructor(vararg coordinates: List<List<Position>>, bbox: BoundingBox? = null) : this(coordinates.toList(), bbox)

@JvmOverloads
constructor(
public constructor(
coordinates: Array<Array<Array<DoubleArray>>>,
bbox: BoundingBox? = null
) : this(coordinates.map { ring -> ring.map { it.map(::Position) } }, bbox)
Expand Down Expand Up @@ -55,7 +55,7 @@ class MultiPolygon @JvmOverloads constructor(
}
}}"""

companion object {
public companion object {
@JvmStatic
public fun fromJson(json: String): MultiPolygon =
fromJson(Json.decodeFromString(JsonObject.serializer(), json))
Expand Down
Loading

0 comments on commit 812654b

Please sign in to comment.