Skip to content

Commit

Permalink
Implemented equals, hashCode, and toString functions for classes
Browse files Browse the repository at this point in the history
  • Loading branch information
chRyNaN committed Sep 21, 2022
1 parent e4ad8fa commit e8ac4da
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package com.chrynan.parcelable.compose

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.compose.runtime.Stable
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.SaverScope
import com.chrynan.parcelable.core.Parcelable
Expand All @@ -12,6 +13,7 @@ import com.chrynan.parcelable.core.encodeToBundle
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer

@Stable
@ExperimentalSerializationApi
internal class AndroidParcelableSaver<T>(
private val parcelable: Parcelable = Parcelable.Default,
Expand All @@ -21,6 +23,25 @@ internal class AndroidParcelableSaver<T>(
override fun SaverScope.save(value: T): Bundle = parcelable.encodeToBundle(value = value, serializer = serializer)

override fun restore(value: Bundle): T = parcelable.decodeFromBundle(bundle = value, deserializer = serializer)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is AndroidParcelableSaver<*>) return false

if (parcelable != other.parcelable) return false
if (serializer != other.serializer) return false

return true
}

override fun hashCode(): Int {
var result = parcelable.hashCode()
result = 31 * result + serializer.hashCode()
return result
}

override fun toString(): String =
"AndroidParcelableSaver(parcelable=$parcelable, serializer=$serializer)"
}

@SuppressLint("ComposableNaming")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.chrynan.parcelable.compose

import androidx.compose.runtime.Stable
import com.chrynan.parcelable.core.Parcel
import com.chrynan.parcelable.core.Parcelable
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer

@Stable
@ExperimentalSerializationApi
internal class JsParcelableSaver<T>(
private val parcelable: Parcelable = Parcelable.Default,
Expand All @@ -24,6 +26,25 @@ internal class JsParcelableSaver<T>(

return parcelable.decodeFromParcel(parcel = parcel, deserializer = serializer)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is JsParcelableSaver<*>) return false

if (parcelable != other.parcelable) return false
if (serializer != other.serializer) return false

return true
}

override fun hashCode(): Int {
var result = parcelable.hashCode()
result = 31 * result + serializer.hashCode()
return result
}

override fun toString(): String =
"JsParcelableSaver(parcelable=$parcelable, serializer=$serializer)"
}

@Suppress("FunctionName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

package com.chrynan.parcelable.compose

import androidx.compose.runtime.Stable
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.SaverScope
import com.chrynan.parcelable.core.Parcel
import com.chrynan.parcelable.core.Parcelable
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.KSerializer

@Stable
@ExperimentalSerializationApi
internal class JvmParcelableSaver<T>(
private val parcelable: Parcelable = Parcelable.Default,
Expand All @@ -28,6 +30,25 @@ internal class JvmParcelableSaver<T>(

return parcelable.decodeFromParcel(parcel = parcel, deserializer = serializer)
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is JvmParcelableSaver<*>) return false

if (parcelable != other.parcelable) return false
if (serializer != other.serializer) return false

return true
}

override fun hashCode(): Int {
var result = parcelable.hashCode()
result = 31 * result + serializer.hashCode()
return result
}

override fun toString(): String =
"JvmParcelableSaver(parcelable=$parcelable, serializer=$serializer)"
}

@ExperimentalSerializationApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,38 @@ class AndroidParcel(private val parcel: android.os.Parcel) : Parcel {
}

override fun toByteArray(): ByteArray = parcel.marshall()

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is AndroidParcel) return false

if (parcel != other.parcel) return false
if (dataBufferCapacity != other.dataBufferCapacity) return false
if (dataSize != other.dataSize) return false
if (dataPosition != other.dataPosition) return false
if (isRecycled != other.isRecycled) return false

return true
}

override fun hashCode(): Int {
var result = parcel.hashCode()

result = 31 * result + dataBufferCapacity
result = 31 * result + dataSize
result = 31 * result + dataPosition
result = 31 * result + isRecycled.hashCode()

return result
}

override fun toString(): String =
"AndroidParcel(" +
"parcel=$parcel, " +
"dataBufferCapacity=$dataBufferCapacity, " +
"dataSize=$dataSize, " +
"dataPosition=$dataPosition, " +
"isRecycled=$isRecycled)"
}

actual fun Parcel(): Parcel = AndroidParcel(android.os.Parcel.obtain())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,38 @@ class ByteArrayParcel internal constructor(initial: ByteArray = byteArrayOf()) :

private fun ByteArray.toStringValue(): String = decodeToString()

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is ByteArrayParcel) return false

if (dataBufferCapacity != other.dataBufferCapacity) return false
if (dataSize != other.dataSize) return false
if (dataPosition != other.dataPosition) return false
if (isRecycled != other.isRecycled) return false
if (data != other.data) return false

return true
}

override fun hashCode(): Int {
var result = dataBufferCapacity

result = 31 * result + dataSize
result = 31 * result + dataPosition
result = 31 * result + isRecycled.hashCode()
result = 31 * result + data.hashCode()

return result
}

override fun toString(): String =
"ByteArrayParcel(" +
"dataBufferCapacity=$dataBufferCapacity, " +
"dataSize=$dataSize, " +
"dataPosition=$dataPosition, " +
"isRecycled=$isRecycled, " +
"data=$data)"

companion object {

private const val FALSE: Byte = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,27 @@ class ParcelDecoder(
override fun decodeCollectionSize(descriptor: SerialDescriptor): Int = decodeInt()

override fun decodeNotNullMark(): Boolean = input.readBoolean()

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is ParcelDecoder) return false

if (serializersModule != other.serializersModule) return false
if (input != other.input) return false
if (elementIndex != other.elementIndex) return false

return true
}

override fun hashCode(): Int {
var result = serializersModule.hashCode()

result = 31 * result + input.hashCode()
result = 31 * result + elementIndex

return result
}

override fun toString(): String =
"ParcelDecoder(serializersModule=$serializersModule, input=$input, elementIndex=$elementIndex)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,23 @@ class ParcelEncoder(
output.writeInt(collectionSize)
return this
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is ParcelEncoder) return false

if (serializersModule != other.serializersModule) return false
if (output != other.output) return false

return true
}

override fun hashCode(): Int {
var result = serializersModule.hashCode()
result = 31 * result + output.hashCode()
return result
}

override fun toString(): String =
"ParcelEncoder(serializersModule=$serializersModule, output=$output)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,20 @@ sealed class Parcelable(internal val configuration: ParcelableConfiguration) : S
*
* Note that instances of this class are created using the [Parcelable] function.
*/
class Custom internal constructor(configuration: ParcelableConfiguration) : Parcelable(configuration)
class Custom internal constructor(configuration: ParcelableConfiguration) : Parcelable(configuration) {

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Custom) return false

return other.configuration == configuration
}

override fun hashCode(): Int = configuration.hashCode()

override fun toString(): String =
"Parcelable.Custom(configuration=$configuration)"
}

override fun <T> encodeToByteArray(serializer: SerializationStrategy<T>, value: T): ByteArray {
val parcel = Parcel()
Expand Down

0 comments on commit e8ac4da

Please sign in to comment.