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

build(kotlin): Add spotless ktfmt and enable explicit api mode for library code. #1912

Merged
merged 9 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
run spotless apply
  • Loading branch information
wywen committed Oct 26, 2024
commit 498226ad6d6bb4419d6c54d57b9fc243cc235ce3
Original file line number Diff line number Diff line change
Expand Up @@ -20,127 +20,128 @@
package org.apache.fury.serializer.kotlin;

import kotlin.*;
import kotlin.UByteArray;
import kotlin.UIntArray;
import kotlin.ULongArray;
import kotlin.UShortArray;
import kotlin.text.*;
import kotlin.time.Duration;
import kotlin.time.DurationUnit;
import kotlin.time.TestTimeSource;
import kotlin.time.TimedValue;
import kotlin.uuid.Uuid;
import kotlin.UByteArray;
import kotlin.UIntArray;
import kotlin.ULongArray;
import kotlin.UShortArray;
import org.apache.fury.AbstractThreadSafeFury;
import org.apache.fury.Fury;
import org.apache.fury.ThreadSafeFury;
import org.apache.fury.resolver.ClassResolver;
import org.apache.fury.serializer.collection.CollectionSerializers;
import org.apache.fury.serializer.collection.MapSerializers;

/**
* KotlinSerializers provide default serializers for kotlin.
*/
/** KotlinSerializers provide default serializers for kotlin. */
@SuppressWarnings({"rawtypes", "unchecked"})
public class KotlinSerializers {

public static void registerSerializers(ThreadSafeFury fury) {
AbstractThreadSafeFury threadSafeFury = (AbstractThreadSafeFury) fury;
threadSafeFury.registerCallback(KotlinSerializers::registerSerializers);
}

public static void registerSerializers(Fury fury) {
ClassResolver resolver = fury.getClassResolver();

// UByte
Class ubyteClass = KotlinToJavaClass.INSTANCE.getUByteClass();
resolver.register(ubyteClass);
resolver.registerSerializer(ubyteClass, new UByteSerializer(fury));

// UShort
Class ushortClass = KotlinToJavaClass.INSTANCE.getUShortClass();
resolver.register(ushortClass);
resolver.registerSerializer(ushortClass, new UShortSerializer(fury));

// UInt
Class uintClass = KotlinToJavaClass.INSTANCE.getUIntClass();
resolver.register(uintClass);
resolver.registerSerializer(uintClass, new UIntSerializer(fury));

// ULong
Class ulongClass = KotlinToJavaClass.INSTANCE.getULongClass();
resolver.register(ulongClass);
resolver.registerSerializer(ulongClass, new ULongSerializer(fury));

// EmptyList
Class emptyListClass = KotlinToJavaClass.INSTANCE.getEmptyListClass();
resolver.register(emptyListClass);
resolver.registerSerializer(emptyListClass, new CollectionSerializers.EmptyListSerializer(fury, emptyListClass));

// EmptySet
Class emptySetClass = KotlinToJavaClass.INSTANCE.getEmptySetClass();
resolver.register(emptySetClass);
resolver.registerSerializer(emptySetClass, new CollectionSerializers.EmptySetSerializer(fury, emptySetClass));

// EmptyMap
Class emptyMapClass = KotlinToJavaClass.INSTANCE.getEmptyMapClass();
resolver.register(emptyMapClass);
resolver.registerSerializer(emptyMapClass, new MapSerializers.EmptyMapSerializer(fury, emptyMapClass));

// Non-Java collection implementation in kotlin stdlib.
Class arrayDequeClass = KotlinToJavaClass.INSTANCE.getArrayDequeClass();
resolver.register(arrayDequeClass);
resolver.registerSerializer(arrayDequeClass, new KotlinArrayDequeSerializer(fury, arrayDequeClass));

// Unsigned array classes: UByteArray, UShortArray, UIntArray, ULongArray.
resolver.register(UByteArray.class);
resolver.registerSerializer(UByteArray.class, new UByteArraySerializer(fury));
resolver.register(UShortArray.class);
resolver.registerSerializer(UShortArray.class, new UShortArraySerializer(fury));
resolver.register(UIntArray.class);
resolver.registerSerializer(UIntArray.class, new UIntArraySerializer(fury));
resolver.register(ULongArray.class);
resolver.registerSerializer(ULongArray.class, new ULongArraySerializer(fury));

// Ranges and Progressions.
resolver.register(kotlin.ranges.CharRange.class);
resolver.register(kotlin.ranges.CharProgression.class);
resolver.register(kotlin.ranges.IntRange.class);
resolver.register(kotlin.ranges.IntProgression.class);
resolver.register(kotlin.ranges.LongRange.class);
resolver.register(kotlin.ranges.LongProgression.class);
resolver.register(kotlin.ranges.UIntRange.class);
resolver.register(kotlin.ranges.UIntProgression.class);
resolver.register(kotlin.ranges.ULongRange.class);
resolver.register(kotlin.ranges.ULongProgression.class);

// Built-in classes.
resolver.register(kotlin.Pair.class);
resolver.register(kotlin.Triple.class);
resolver.register(kotlin.Result.class);
resolver.register(Result.Failure.class);

// kotlin.random
resolver.register(KotlinToJavaClass.INSTANCE.getRandomDefaultClass());
resolver.register(KotlinToJavaClass.INSTANCE.getRandomInternalClass());
resolver.register(KotlinToJavaClass.INSTANCE.getRandomSerializedClass());

// kotlin.text
resolver.register(Regex.class);
resolver.register(KotlinToJavaClass.INSTANCE.getRegexSerializedClass());
resolver.register(RegexOption.class);
resolver.register(CharCategory.class);
resolver.register(CharDirectionality.class);
resolver.register(HexFormat.class);
resolver.register(MatchGroup.class);

// kotlin.time
resolver.register(DurationUnit.class);
resolver.register(Duration.class);
resolver.registerSerializer(Duration.class, new DurationSerializer(fury));
resolver.register(TimedValue.class);

// kotlin.uuid
resolver.register(Uuid.class);
resolver.registerSerializer(Uuid.class, new UuidSerializer(fury));
}
public static void registerSerializers(ThreadSafeFury fury) {
AbstractThreadSafeFury threadSafeFury = (AbstractThreadSafeFury) fury;
threadSafeFury.registerCallback(KotlinSerializers::registerSerializers);
}

public static void registerSerializers(Fury fury) {
ClassResolver resolver = fury.getClassResolver();

// UByte
Class ubyteClass = KotlinToJavaClass.INSTANCE.getUByteClass();
resolver.register(ubyteClass);
resolver.registerSerializer(ubyteClass, new UByteSerializer(fury));

// UShort
Class ushortClass = KotlinToJavaClass.INSTANCE.getUShortClass();
resolver.register(ushortClass);
resolver.registerSerializer(ushortClass, new UShortSerializer(fury));

// UInt
Class uintClass = KotlinToJavaClass.INSTANCE.getUIntClass();
resolver.register(uintClass);
resolver.registerSerializer(uintClass, new UIntSerializer(fury));

// ULong
Class ulongClass = KotlinToJavaClass.INSTANCE.getULongClass();
resolver.register(ulongClass);
resolver.registerSerializer(ulongClass, new ULongSerializer(fury));

// EmptyList
Class emptyListClass = KotlinToJavaClass.INSTANCE.getEmptyListClass();
resolver.register(emptyListClass);
resolver.registerSerializer(
emptyListClass, new CollectionSerializers.EmptyListSerializer(fury, emptyListClass));

// EmptySet
Class emptySetClass = KotlinToJavaClass.INSTANCE.getEmptySetClass();
resolver.register(emptySetClass);
resolver.registerSerializer(
emptySetClass, new CollectionSerializers.EmptySetSerializer(fury, emptySetClass));

// EmptyMap
Class emptyMapClass = KotlinToJavaClass.INSTANCE.getEmptyMapClass();
resolver.register(emptyMapClass);
resolver.registerSerializer(
emptyMapClass, new MapSerializers.EmptyMapSerializer(fury, emptyMapClass));

// Non-Java collection implementation in kotlin stdlib.
Class arrayDequeClass = KotlinToJavaClass.INSTANCE.getArrayDequeClass();
resolver.register(arrayDequeClass);
resolver.registerSerializer(
arrayDequeClass, new KotlinArrayDequeSerializer(fury, arrayDequeClass));

// Unsigned array classes: UByteArray, UShortArray, UIntArray, ULongArray.
resolver.register(UByteArray.class);
resolver.registerSerializer(UByteArray.class, new UByteArraySerializer(fury));
resolver.register(UShortArray.class);
resolver.registerSerializer(UShortArray.class, new UShortArraySerializer(fury));
resolver.register(UIntArray.class);
resolver.registerSerializer(UIntArray.class, new UIntArraySerializer(fury));
resolver.register(ULongArray.class);
resolver.registerSerializer(ULongArray.class, new ULongArraySerializer(fury));

// Ranges and Progressions.
resolver.register(kotlin.ranges.CharRange.class);
resolver.register(kotlin.ranges.CharProgression.class);
resolver.register(kotlin.ranges.IntRange.class);
resolver.register(kotlin.ranges.IntProgression.class);
resolver.register(kotlin.ranges.LongRange.class);
resolver.register(kotlin.ranges.LongProgression.class);
resolver.register(kotlin.ranges.UIntRange.class);
resolver.register(kotlin.ranges.UIntProgression.class);
resolver.register(kotlin.ranges.ULongRange.class);
resolver.register(kotlin.ranges.ULongProgression.class);

// Built-in classes.
resolver.register(kotlin.Pair.class);
resolver.register(kotlin.Triple.class);
resolver.register(kotlin.Result.class);
resolver.register(Result.Failure.class);

// kotlin.random
resolver.register(KotlinToJavaClass.INSTANCE.getRandomDefaultClass());
resolver.register(KotlinToJavaClass.INSTANCE.getRandomInternalClass());
resolver.register(KotlinToJavaClass.INSTANCE.getRandomSerializedClass());

// kotlin.text
resolver.register(Regex.class);
resolver.register(KotlinToJavaClass.INSTANCE.getRegexSerializedClass());
resolver.register(RegexOption.class);
resolver.register(CharCategory.class);
resolver.register(CharDirectionality.class);
resolver.register(HexFormat.class);
resolver.register(MatchGroup.class);

// kotlin.time
resolver.register(DurationUnit.class);
resolver.register(Duration.class);
resolver.registerSerializer(Duration.class, new DurationSerializer(fury));
resolver.register(TimedValue.class);

// kotlin.uuid
resolver.register(Uuid.class);
resolver.registerSerializer(Uuid.class, new UuidSerializer(fury));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,26 @@ package org.apache.fury.serializer.kotlin
/**
* Collection builder interface adapts an iterable to allow incremental build by the fury protocol.
*/
interface CollectionBuilder <E, T: Iterable<E>> {
fun add(element: E): Boolean
fun result(): T
interface CollectionBuilder<E, T : Iterable<E>> {
fun add(element: E): Boolean

fun result(): T
}

/**
* Abstract builder for [kotlin.collections.List] interface.
*/
abstract class AbstractListBuilder<E, T:List<E>>
: CollectionBuilder<E, T>, AdaptedCollection<E>() {
protected open val builder: MutableList<E> = mutableListOf()
override val size: Int
get() = builder.size
override fun add(element: E) =
builder.add(element)
override fun iterator(): MutableIterator<E> = builder.iterator()
/** Abstract builder for [kotlin.collections.List] interface. */
abstract class AbstractListBuilder<E, T : List<E>> :
CollectionBuilder<E, T>, AdaptedCollection<E>() {
protected open val builder: MutableList<E> = mutableListOf()
override val size: Int
get() = builder.size

override fun add(element: E) = builder.add(element)

override fun iterator(): MutableIterator<E> = builder.iterator()
}

/**
* Builder for [kotlin.collections.ArrayDeque].
*/
class ArrayDequeBuilder<E>(
override val builder: ArrayDeque<E>
): AbstractListBuilder<E, ArrayDeque<E>>() {
override fun result(): ArrayDeque<E> = builder
/** Builder for [kotlin.collections.ArrayDeque]. */
class ArrayDequeBuilder<E>(override val builder: ArrayDeque<E>) :
AbstractListBuilder<E, ArrayDeque<E>>() {
override fun result(): ArrayDeque<E> = builder
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,61 +23,51 @@ import org.apache.fury.Fury
import org.apache.fury.memory.MemoryBuffer
import org.apache.fury.serializer.collection.AbstractCollectionSerializer

/**
* Serializer for kotlin collections.
*/
/** Serializer for kotlin collections. */
@Suppress("UNCHECKED_CAST")
abstract class AbstractKotlinCollectionSerializer<E, T: Iterable<E>>(
fury: Fury,
cls: Class<T>
) : AbstractCollectionSerializer<T>(fury, cls) {
abstract override fun onCollectionWrite(buffer: MemoryBuffer, value: T): Collection<E>
abstract class AbstractKotlinCollectionSerializer<E, T : Iterable<E>>(fury: Fury, cls: Class<T>) :
AbstractCollectionSerializer<T>(fury, cls) {
abstract override fun onCollectionWrite(buffer: MemoryBuffer, value: T): Collection<E>

override fun read(buffer: MemoryBuffer): T {
val collection = newCollection(buffer)
val numElements = getAndClearNumElements()
if (numElements != 0) readElements(fury, buffer, collection, numElements)
return onCollectionRead(collection)
}
override fun read(buffer: MemoryBuffer): T {
val collection = newCollection(buffer)
val numElements = getAndClearNumElements()
if (numElements != 0) readElements(fury, buffer, collection, numElements)
return onCollectionRead(collection)
}

override fun onCollectionRead(collection: Collection<*>): T {
val builder = collection as CollectionBuilder<E, T>
return builder.result()
}
override fun onCollectionRead(collection: Collection<*>): T {
val builder = collection as CollectionBuilder<E, T>
return builder.result()
}
}

/**
* Serializer for [[kotlin.collections.ArrayDeque]].
*/
class KotlinArrayDequeSerializer<E> (
fury: Fury,
cls: Class<ArrayDeque<E>>,
/** Serializer for [[kotlin.collections.ArrayDeque]]. */
class KotlinArrayDequeSerializer<E>(
fury: Fury,
cls: Class<ArrayDeque<E>>,
) : AbstractKotlinCollectionSerializer<E, ArrayDeque<E>>(fury, cls) {
override fun onCollectionWrite(buffer: MemoryBuffer, value: ArrayDeque<E>): Collection<E> {
val adapter = IterableAdapter<E>(value)
buffer.writeVarUint32Small7(adapter.size)
return adapter
}
override fun newCollection(buffer: MemoryBuffer): Collection<E> {
val numElements = buffer.readVarUint32()
setNumElements(numElements)
return ArrayDequeBuilder<E>(ArrayDeque<E>(numElements))
}
override fun onCollectionWrite(buffer: MemoryBuffer, value: ArrayDeque<E>): Collection<E> {
val adapter = IterableAdapter<E>(value)
buffer.writeVarUint32Small7(adapter.size)
return adapter
}

override fun newCollection(buffer: MemoryBuffer): Collection<E> {
val numElements = buffer.readVarUint32()
setNumElements(numElements)
return ArrayDequeBuilder<E>(ArrayDeque<E>(numElements))
}
}

typealias AdaptedCollection<E> = java.util.AbstractCollection<E>

/**
* An adapter which wraps a kotlin iterable into a [[java.util.Collection]].
*/
private class IterableAdapter<E>(
coll: Iterable<E>
) : AdaptedCollection<E>() {
private val mutableList = coll.toMutableList()
/** An adapter which wraps a kotlin iterable into a [[java.util.Collection]]. */
private class IterableAdapter<E>(coll: Iterable<E>) : AdaptedCollection<E>() {
private val mutableList = coll.toMutableList()

override val size: Int
get() = mutableList.count()
override val size: Int
get() = mutableList.count()

override fun iterator(): MutableIterator<E> =
mutableList.iterator()
override fun iterator(): MutableIterator<E> = mutableList.iterator()
}
Loading