Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ internal class LinkedHashTreeMap<K, V>(

// Clear all links to help GC
val header = header
var e = header.next
var e = header.next!!
while (e !== header) {
val next = e!!.next
val next = e.next
e.prev = null
e.next = null
e = next
e = next!!
}
header.prev = header
header.next = header.prev
header.next = header
}

override fun remove(key: K) = removeInternalByKey(key)?.value
Expand All @@ -85,7 +85,8 @@ internal class LinkedHashTreeMap<K, V>(

private var realKey: K? = null

override val key: K get() = knownNotNull(realKey)
override val key: K
get() = knownNotNull(realKey)

@JvmField
val hash: Int
Expand Down Expand Up @@ -137,7 +138,7 @@ internal class LinkedHashTreeMap<K, V>(
}

override fun hashCode(): Int {
return (realKey?.hashCode() ?: 0) xor if (value == null) 0 else value.hashCode()
return (realKey?.hashCode() ?: 0) xor (value?.hashCode() ?: 0)
}

override fun toString() = "$key=$value"
Expand Down Expand Up @@ -188,7 +189,7 @@ internal class LinkedHashTreeMap<K, V>(
val comparator: Comparator<in K?> = comparator
val table = table
val hash = secondaryHash(key.hashCode())
val index = hash and table.size - 1
val index = hash and (table.size - 1)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is just for my own benefit, my meat brain can’t handle that subtract binds tighter than bitwise and

var nearest = table[index]
var comparison = 0
if (nearest != null) {
Expand Down Expand Up @@ -246,7 +247,7 @@ internal class LinkedHashTreeMap<K, V>(
return try {
@Suppress("UNCHECKED_CAST")
if (key != null) find(key as K, false) else null
} catch (e: ClassCastException) {
} catch (_: ClassCastException) {
null
}
}
Expand Down Expand Up @@ -363,7 +364,7 @@ internal class LinkedHashTreeMap<K, V>(
parent.right = replacement
}
} else {
val index = node.hash and table.size - 1
val index = node.hash and (table.size - 1)
table[index] = replacement
}
}
Expand Down Expand Up @@ -437,17 +438,19 @@ internal class LinkedHashTreeMap<K, V>(
/** Rotates the subtree so that its root's right child is the new root. */
private fun rotateLeft(root: Node<K, V>) {
val left = root.left
val pivot = root.right
val pivotLeft = pivot!!.left
val pivot = root.right!!
val pivotLeft = pivot.left
val pivotRight = pivot.right

// move the pivot's left child to the root's right
root.right = pivotLeft
if (pivotLeft != null) {
pivotLeft.parent = root
}

replaceInParent(root, pivot)

// move the root to the pivot's left
pivot.left = root
root.parent = pivot

Expand All @@ -458,16 +461,17 @@ internal class LinkedHashTreeMap<K, V>(

/** Rotates the subtree so that its root's left child is the new root. */
private fun rotateRight(root: Node<K, V>) {
val pivot = root.left
val pivot = root.left!!
val right = root.right
val pivotLeft = pivot!!.left
val pivotLeft = pivot.left
val pivotRight = pivot.right

// move the pivot's right child to the root's left
root.left = pivotRight
if (pivotRight != null) {
pivotRight.parent = root
}

replaceInParent(root, pivot)

// move the root to the pivot's right
Expand All @@ -480,7 +484,7 @@ internal class LinkedHashTreeMap<K, V>(
}

abstract inner class LinkedTreeMapIterator<T> : MutableIterator<T> {
var next = header.next
var next: Node<K, V> = header.next!!
private var lastReturned: Node<K, V>? = null
private var expectedModCount: Int = modCount
override fun hasNext(): Boolean = next !== header
Expand All @@ -493,7 +497,7 @@ internal class LinkedHashTreeMap<K, V>(
if (modCount != expectedModCount) {
throw ConcurrentModificationException()
}
next = e!!.next
next = e.next!!
return e.also { lastReturned = it }
}

Expand Down Expand Up @@ -639,6 +643,7 @@ internal fun <K, V> doubleCapacity(oldTable: Array<Node<K, V>?>): Array<Node<K,
internal class AvlIterator<K, V> {
/** This stack is a singly linked list, linked by the 'parent' field. */
private var stackTop: Node<K, V>? = null

fun reset(root: Node<K, V>?) {
var stackTop: Node<K, V>? = null
var n = root
Expand All @@ -652,8 +657,8 @@ internal class AvlIterator<K, V> {

operator fun next(): Node<K, V>? {
var stackTop: Node<K, V>? = stackTop ?: return null
val result = stackTop
stackTop = result!!.parent
val result = stackTop!!
stackTop = result.parent
result.parent = null
var n = result.right
while (n != null) {
Expand Down Expand Up @@ -682,9 +687,11 @@ internal class AvlIterator<K, V> {
internal class AvlBuilder<K, V> {
/** This stack is a singly linked list, linked by the 'parent' field. */
private var stack: Node<K, V>? = null

private var leavesToSkip = 0
private var leavesSkipped = 0
private var size = 0

fun reset(targetSize: Int) {
// compute the target tree size. This is a power of 2 minus one, like 15 or 31.
val treeCapacity = Integer.highestOneBit(targetSize) * 2 - 1
Expand All @@ -701,7 +708,7 @@ internal class AvlBuilder<K, V> {
node.height = 1

// Skip a leaf if necessary.
if (leavesToSkip > 0 && size and 1 == 0) {
if (leavesToSkip > 0 && (size and 1) == 0) {
size++
leavesToSkip--
leavesSkipped++
Expand All @@ -711,7 +718,7 @@ internal class AvlBuilder<K, V> {
size++

// Skip a leaf if necessary.
if (leavesToSkip > 0 && size and 1 == 0) {
if (leavesToSkip > 0 && (size and 1) == 0) {
size++
leavesToSkip--
leavesSkipped++
Expand All @@ -731,14 +738,14 @@ internal class AvlBuilder<K, V> {
* size (N-1) whenever the total size is 2N-1 whenever N is a power of 2.
*/
var scale = 4
while (size and scale - 1 == scale - 1) {
while (size and (scale - 1) == scale - 1) {
when (leavesSkipped) {
0 -> {
// Pop right, center and left, then make center the top of the stack.
val right = stack
val center = right!!.parent
val left = center!!.parent
center.parent = left!!.parent
val right = stack!!
val center = right.parent!!
val left = center.parent!!
center.parent = left.parent
stack = center
// Construct a tree.
center.left = left
Expand All @@ -750,9 +757,9 @@ internal class AvlBuilder<K, V> {

1 -> {
// Pop right and center, then make center the top of the stack.
val right = stack
val center = right!!.parent
stack = center!!
val right = stack!!
val center = right.parent!!
stack = center
// Construct a tree with no left child.
center.right = right
center.height = right.height + 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ internal object StandardJsonAdapters : JsonAdapter.Factory {
}

override fun toJson(writer: JsonWriter, value: Int?) {
writer.value(knownNotNull(value).toInt().toLong())
writer.value(knownNotNull(value).toLong())
}

override fun toString() = "JsonAdapter(Integer)"
Expand All @@ -160,7 +160,7 @@ internal object StandardJsonAdapters : JsonAdapter.Factory {
}

override fun toJson(writer: JsonWriter, value: Long?) {
writer.value(knownNotNull(value).toLong())
writer.value(knownNotNull(value))
}

override fun toString() = "JsonAdapter(Long)"
Expand All @@ -179,7 +179,7 @@ internal object StandardJsonAdapters : JsonAdapter.Factory {
}

private val STRING_JSON_ADAPTER: JsonAdapter<String> = object : JsonAdapter<String>() {
override fun fromJson(reader: JsonReader): String? {
override fun fromJson(reader: JsonReader): String {
return reader.nextString()
}

Expand Down
25 changes: 14 additions & 11 deletions moshi/src/main/java/com/squareup/moshi/internal/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import kotlin.contracts.contract
@Suppress("UNCHECKED_CAST")
private val METADATA: Class<out Annotation>? = try {
Class.forName(kotlinMetadataClassName) as Class<out Annotation>
} catch (ignored: ClassNotFoundException) {
} catch (_: ClassNotFoundException) {
null
}

Expand All @@ -58,7 +58,7 @@ private val METADATA: Class<out Annotation>? = try {
@JvmField
public val DEFAULT_CONSTRUCTOR_MARKER: Class<*>? = try {
Class.forName("kotlin.jvm.internal.DefaultConstructorMarker")
} catch (ignored: ClassNotFoundException) {
} catch (_: ClassNotFoundException) {
null
}

Expand Down Expand Up @@ -345,15 +345,18 @@ internal fun Type.typeToString(): String {
*/
internal fun declaringClassOf(typeVariable: TypeVariable<*>): Class<*>? {
val genericDeclaration = typeVariable.genericDeclaration
return if (genericDeclaration is Class<*>) genericDeclaration else null
return genericDeclaration as? Class<*>
}

internal fun Type.checkNotPrimitive() {
require(!(this is Class<*> && isPrimitive)) { "Unexpected primitive $this. Use the boxed type." }
require(this !is Class<*> || !isPrimitive) { "Unexpected primitive $this. Use the boxed type." }
}

internal fun Type.toStringWithAnnotations(annotations: Set<Annotation>): String {
return toString() + if (annotations.isEmpty()) " (with no annotations)" else " annotated $annotations"
return when {
annotations.isEmpty() -> "$this (with no annotations)"
else -> "$this annotated $annotations"
}
}

/**
Expand Down Expand Up @@ -394,8 +397,8 @@ internal fun mapKeyAndValueTypes(context: Type, contextRawType: Class<*>): Array
* @param supertype a superclass of, or interface implemented by, this.
*/
internal fun getSupertype(context: Type, contextRawType: Class<*>, supertype: Class<*>): Type {
if (!supertype.isAssignableFrom(contextRawType)) throw IllegalArgumentException()
return getGenericSupertype(context, contextRawType, supertype).resolve((context), (contextRawType))
require(supertype.isAssignableFrom(contextRawType))
return getGenericSupertype(context, contextRawType, supertype).resolve(context, contextRawType)
}

internal fun <T : Annotation?> createJsonQualifierImplementation(annotationType: Class<T>): T {
Expand Down Expand Up @@ -456,7 +459,7 @@ public fun Moshi.generatedAdapter(
// Common case first
constructor = adapterClass.getDeclaredConstructor(Moshi::class.java, Array<Type>::class.java)
args = arrayOf(this, typeArgs)
} catch (e: NoSuchMethodException) {
} catch (_: NoSuchMethodException) {
constructor = adapterClass.getDeclaredConstructor(Array<Type>::class.java)
args = arrayOf(typeArgs)
}
Expand All @@ -465,7 +468,7 @@ public fun Moshi.generatedAdapter(
// Common case first
constructor = adapterClass.getDeclaredConstructor(Moshi::class.java)
args = arrayOf(this)
} catch (e: NoSuchMethodException) {
} catch (_: NoSuchMethodException) {
constructor = adapterClass.getDeclaredConstructor()
args = emptyArray()
}
Expand Down Expand Up @@ -629,7 +632,7 @@ internal class ParameterizedTypeImpl private constructor(
if (rawType is Class<*>) {
val enclosingClass = rawType.enclosingClass
if (ownerType != null) {
require(!(enclosingClass == null || ownerType.rawType != enclosingClass)) { "unexpected owner type for $rawType: $ownerType" }
require(enclosingClass != null && ownerType.rawType == enclosingClass) { "unexpected owner type for $rawType: $ownerType" }
} else {
require(enclosingClass == null) { "unexpected owner type for $rawType: null" }
}
Expand Down Expand Up @@ -706,7 +709,7 @@ internal class WildcardTypeImpl private constructor(
require(upperBounds.size == 1)
return if (lowerBounds.size == 1) {
lowerBounds[0].checkNotPrimitive()
require(!(upperBounds[0] !== Any::class.java))
require(upperBounds[0] === Any::class.java)
WildcardTypeImpl(
lowerBound = lowerBounds[0].canonicalize(),
upperBound = Any::class.java,
Expand Down
Loading