Skip to content

Instantly share code, notes, and snippets.

@tanmatra
Last active October 2, 2022 08:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanmatra/04b049a77918b8b321ba4c37f4c582c1 to your computer and use it in GitHub Desktop.
Save tanmatra/04b049a77918b8b321ba4c37f4c582c1 to your computer and use it in GitHub Desktop.
Kotlin hashCode builder with inline functions
class Example(val time: Long,
val short: Short = -111,
val eventId: Int?,
val data: ByteArray?,
val extra: Any = Unit,
val extra2: LongArray? = null)
{
override fun hashCode() = initHash()
.addHash(time)
.addHash(short)
.addHash(eventId)
.addHash(data)
.addHash(extra)
.addHash(extra2)
}
@file:Suppress("NOTHING_TO_INLINE", "unused")
package hash
import java.util.Arrays
const val MULTIPLIER = 31
inline fun initHash(): Int = 0
//------------------------------------------------------------------------- Byte
inline infix fun Int.addHash(value: Byte): Int {
return this * MULTIPLIER + value
}
inline infix fun Int.addHash(value: Byte?): Int {
return this * MULTIPLIER + if (value == null) 0 else value
}
//------------------------------------------------------------------------ Short
inline infix fun Int.addHash(value: Short): Int {
return this * MULTIPLIER + value
}
inline infix fun Int.addHash(value: Short?): Int {
return this * MULTIPLIER + if (value == null) 0 else value
}
//-------------------------------------------------------------------------- Int
inline infix fun Int.addHash(value: Int): Int {
return this * MULTIPLIER + value
}
inline infix fun Int.addHash(value: Int?): Int {
return this * MULTIPLIER + if (value == null) 0 else value
}
//------------------------------------------------------------------------- Long
inline infix fun Int.addHash(value: Long): Int {
return this * MULTIPLIER + (value xor (value ushr 32)).toInt()
}
@JvmName("addHash0")
inline infix fun Int.addHash(value: Long?): Int {
return this * MULTIPLIER + if (value == null) 0 else (value xor (value ushr 32)).toInt()
}
//------------------------------------------------------------------------ Float
inline infix fun Int.addHash(value: Float): Int {
return this * MULTIPLIER + java.lang.Float.hashCode(value)
}
@JvmName("addHash0")
inline infix fun Int.addHash(value: Float?): Int {
return this * MULTIPLIER + if (value == null) 0 else java.lang.Float.hashCode(value)
}
//----------------------------------------------------------------------- Double
inline infix fun Int.addHash(value: Double): Int {
return this * MULTIPLIER + java.lang.Double.hashCode(value)
}
@JvmName("addHash0")
inline infix fun Int.addHash(value: Double?): Int {
return this * MULTIPLIER + if (value == null) 0 else java.lang.Double.hashCode(value)
}
//----------------------------------------------------------------------- Byte[]
inline infix fun Int.addHash(value: ByteArray): Int {
return this * MULTIPLIER + Arrays.hashCode(value)
}
@JvmName("addHash0")
inline infix fun Int.addHash(value: ByteArray?): Int {
return this * MULTIPLIER + if (value == null) 0 else Arrays.hashCode(value)
}
//---------------------------------------------------------------------- Short[]
inline infix fun Int.addHash(value: ShortArray): Int {
return this * MULTIPLIER + Arrays.hashCode(value)
}
@JvmName("addHash0")
inline infix fun Int.addHash(value: ShortArray?): Int {
return this * MULTIPLIER + if (value == null) 0 else Arrays.hashCode(value)
}
//------------------------------------------------------------------------ Int[]
inline infix fun Int.addHash(value: IntArray): Int {
return this * MULTIPLIER + Arrays.hashCode(value)
}
@JvmName("addHash0")
inline infix fun Int.addHash(value: IntArray?): Int {
return this * MULTIPLIER + if (value == null) 0 else Arrays.hashCode(value)
}
//----------------------------------------------------------------------- Long[]
inline infix fun Int.addHash(value: LongArray): Int {
return this * MULTIPLIER + Arrays.hashCode(value)
}
@JvmName("addHash0")
inline infix fun Int.addHash(value: LongArray?): Int {
return this * MULTIPLIER + if (value == null) 0 else Arrays.hashCode(value)
}
//---------------------------------------------------------------------- Float[]
inline infix fun Int.addHash(value: FloatArray): Int {
return this * MULTIPLIER + Arrays.hashCode(value)
}
@JvmName("addHash0")
inline infix fun Int.addHash(value: FloatArray?): Int {
return this * MULTIPLIER + if (value == null) 0 else Arrays.hashCode(value)
}
//--------------------------------------------------------------------- Double[]
inline infix fun Int.addHash(value: DoubleArray): Int {
return this * MULTIPLIER + Arrays.hashCode(value)
}
@JvmName("addHash0")
inline infix fun Int.addHash(value: DoubleArray?): Int {
return this * MULTIPLIER + if (value == null) 0 else Arrays.hashCode(value)
}
//------------------------------------------------------------------------ Any[]
inline infix fun Int.addHash(value: Array<*>): Int {
return this * MULTIPLIER + Arrays.hashCode(value)
}
@JvmName("addHash0")
inline infix fun Int.addHash(value: Array<*>?): Int {
return this * MULTIPLIER + if (value == null) 0 else Arrays.hashCode(value)
}
//-------------------------------------------------------------------------- Any
inline infix fun Int.addHash(value: Any): Int {
return this * MULTIPLIER + value.hashCode()
}
@JvmName("addHash0")
inline infix fun Int.addHash(value: Any?): Int {
return this * MULTIPLIER + (value?.hashCode() ?: 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment