Skip to content

Commit 3f48caf

Browse files
authored
More cosmetic cleanup after Kotlin to Java conversion (#2041)
No behavior changes. Co-authored-by: Jesse Wilson <[email protected]>
1 parent fa603c8 commit 3f48caf

File tree

3 files changed

+51
-41
lines changed

3 files changed

+51
-41
lines changed

moshi/src/main/java/com/squareup/moshi/internal/LinkedHashTreeMap.kt

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@ internal class LinkedHashTreeMap<K, V>(
5454

5555
// Clear all links to help GC
5656
val header = header
57-
var e = header.next
57+
var e = header.next!!
5858
while (e !== header) {
59-
val next = e!!.next
59+
val next = e.next
6060
e.prev = null
6161
e.next = null
62-
e = next
62+
e = next!!
6363
}
6464
header.prev = header
65-
header.next = header.prev
65+
header.next = header
6666
}
6767

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

8686
private var realKey: K? = null
8787

88-
override val key: K get() = knownNotNull(realKey)
88+
override val key: K
89+
get() = knownNotNull(realKey)
8990

9091
@JvmField
9192
val hash: Int
@@ -137,7 +138,7 @@ internal class LinkedHashTreeMap<K, V>(
137138
}
138139

139140
override fun hashCode(): Int {
140-
return (realKey?.hashCode() ?: 0) xor if (value == null) 0 else value.hashCode()
141+
return (realKey?.hashCode() ?: 0) xor (value?.hashCode() ?: 0)
141142
}
142143

143144
override fun toString() = "$key=$value"
@@ -188,7 +189,7 @@ internal class LinkedHashTreeMap<K, V>(
188189
val comparator: Comparator<in K?> = comparator
189190
val table = table
190191
val hash = secondaryHash(key.hashCode())
191-
val index = hash and table.size - 1
192+
val index = hash and (table.size - 1)
192193
var nearest = table[index]
193194
var comparison = 0
194195
if (nearest != null) {
@@ -246,7 +247,7 @@ internal class LinkedHashTreeMap<K, V>(
246247
return try {
247248
@Suppress("UNCHECKED_CAST")
248249
if (key != null) find(key as K, false) else null
249-
} catch (e: ClassCastException) {
250+
} catch (_: ClassCastException) {
250251
null
251252
}
252253
}
@@ -363,7 +364,7 @@ internal class LinkedHashTreeMap<K, V>(
363364
parent.right = replacement
364365
}
365366
} else {
366-
val index = node.hash and table.size - 1
367+
val index = node.hash and (table.size - 1)
367368
table[index] = replacement
368369
}
369370
}
@@ -437,17 +438,19 @@ internal class LinkedHashTreeMap<K, V>(
437438
/** Rotates the subtree so that its root's right child is the new root. */
438439
private fun rotateLeft(root: Node<K, V>) {
439440
val left = root.left
440-
val pivot = root.right
441-
val pivotLeft = pivot!!.left
441+
val pivot = root.right!!
442+
val pivotLeft = pivot.left
442443
val pivotRight = pivot.right
443444

444445
// move the pivot's left child to the root's right
445446
root.right = pivotLeft
446447
if (pivotLeft != null) {
447448
pivotLeft.parent = root
448449
}
450+
449451
replaceInParent(root, pivot)
450452

453+
// move the root to the pivot's left
451454
pivot.left = root
452455
root.parent = pivot
453456

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

459462
/** Rotates the subtree so that its root's left child is the new root. */
460463
private fun rotateRight(root: Node<K, V>) {
461-
val pivot = root.left
464+
val pivot = root.left!!
462465
val right = root.right
463-
val pivotLeft = pivot!!.left
466+
val pivotLeft = pivot.left
464467
val pivotRight = pivot.right
465468

466469
// move the pivot's right child to the root's left
467470
root.left = pivotRight
468471
if (pivotRight != null) {
469472
pivotRight.parent = root
470473
}
474+
471475
replaceInParent(root, pivot)
472476

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

482486
abstract inner class LinkedTreeMapIterator<T> : MutableIterator<T> {
483-
var next = header.next
487+
var next: Node<K, V> = header.next!!
484488
private var lastReturned: Node<K, V>? = null
485489
private var expectedModCount: Int = modCount
486490
override fun hasNext(): Boolean = next !== header
@@ -493,7 +497,7 @@ internal class LinkedHashTreeMap<K, V>(
493497
if (modCount != expectedModCount) {
494498
throw ConcurrentModificationException()
495499
}
496-
next = e!!.next
500+
next = e.next!!
497501
return e.also { lastReturned = it }
498502
}
499503

@@ -639,6 +643,7 @@ internal fun <K, V> doubleCapacity(oldTable: Array<Node<K, V>?>): Array<Node<K,
639643
internal class AvlIterator<K, V> {
640644
/** This stack is a singly linked list, linked by the 'parent' field. */
641645
private var stackTop: Node<K, V>? = null
646+
642647
fun reset(root: Node<K, V>?) {
643648
var stackTop: Node<K, V>? = null
644649
var n = root
@@ -652,8 +657,8 @@ internal class AvlIterator<K, V> {
652657

653658
operator fun next(): Node<K, V>? {
654659
var stackTop: Node<K, V>? = stackTop ?: return null
655-
val result = stackTop
656-
stackTop = result!!.parent
660+
val result = stackTop!!
661+
stackTop = result.parent
657662
result.parent = null
658663
var n = result.right
659664
while (n != null) {
@@ -682,9 +687,11 @@ internal class AvlIterator<K, V> {
682687
internal class AvlBuilder<K, V> {
683688
/** This stack is a singly linked list, linked by the 'parent' field. */
684689
private var stack: Node<K, V>? = null
690+
685691
private var leavesToSkip = 0
686692
private var leavesSkipped = 0
687693
private var size = 0
694+
688695
fun reset(targetSize: Int) {
689696
// compute the target tree size. This is a power of 2 minus one, like 15 or 31.
690697
val treeCapacity = Integer.highestOneBit(targetSize) * 2 - 1
@@ -701,7 +708,7 @@ internal class AvlBuilder<K, V> {
701708
node.height = 1
702709

703710
// Skip a leaf if necessary.
704-
if (leavesToSkip > 0 && size and 1 == 0) {
711+
if (leavesToSkip > 0 && (size and 1) == 0) {
705712
size++
706713
leavesToSkip--
707714
leavesSkipped++
@@ -711,7 +718,7 @@ internal class AvlBuilder<K, V> {
711718
size++
712719

713720
// Skip a leaf if necessary.
714-
if (leavesToSkip > 0 && size and 1 == 0) {
721+
if (leavesToSkip > 0 && (size and 1) == 0) {
715722
size++
716723
leavesToSkip--
717724
leavesSkipped++
@@ -731,14 +738,14 @@ internal class AvlBuilder<K, V> {
731738
* size (N-1) whenever the total size is 2N-1 whenever N is a power of 2.
732739
*/
733740
var scale = 4
734-
while (size and scale - 1 == scale - 1) {
741+
while (size and (scale - 1) == scale - 1) {
735742
when (leavesSkipped) {
736743
0 -> {
737744
// Pop right, center and left, then make center the top of the stack.
738-
val right = stack
739-
val center = right!!.parent
740-
val left = center!!.parent
741-
center.parent = left!!.parent
745+
val right = stack!!
746+
val center = right.parent!!
747+
val left = center.parent!!
748+
center.parent = left.parent
742749
stack = center
743750
// Construct a tree.
744751
center.left = left
@@ -750,9 +757,9 @@ internal class AvlBuilder<K, V> {
750757

751758
1 -> {
752759
// Pop right and center, then make center the top of the stack.
753-
val right = stack
754-
val center = right!!.parent
755-
stack = center!!
760+
val right = stack!!
761+
val center = right.parent!!
762+
stack = center
756763
// Construct a tree with no left child.
757764
center.right = right
758765
center.height = right.height + 1

moshi/src/main/java/com/squareup/moshi/internal/StandardJsonAdapters.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ internal object StandardJsonAdapters : JsonAdapter.Factory {
148148
}
149149

150150
override fun toJson(writer: JsonWriter, value: Int?) {
151-
writer.value(knownNotNull(value).toInt().toLong())
151+
writer.value(knownNotNull(value).toLong())
152152
}
153153

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

162162
override fun toJson(writer: JsonWriter, value: Long?) {
163-
writer.value(knownNotNull(value).toLong())
163+
writer.value(knownNotNull(value))
164164
}
165165

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

181181
private val STRING_JSON_ADAPTER: JsonAdapter<String> = object : JsonAdapter<String>() {
182-
override fun fromJson(reader: JsonReader): String? {
182+
override fun fromJson(reader: JsonReader): String {
183183
return reader.nextString()
184184
}
185185

moshi/src/main/java/com/squareup/moshi/internal/Util.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import kotlin.contracts.contract
4949
@Suppress("UNCHECKED_CAST")
5050
private val METADATA: Class<out Annotation>? = try {
5151
Class.forName(kotlinMetadataClassName) as Class<out Annotation>
52-
} catch (ignored: ClassNotFoundException) {
52+
} catch (_: ClassNotFoundException) {
5353
null
5454
}
5555

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

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

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

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

359362
/**
@@ -394,8 +397,8 @@ internal fun mapKeyAndValueTypes(context: Type, contextRawType: Class<*>): Array
394397
* @param supertype a superclass of, or interface implemented by, this.
395398
*/
396399
internal fun getSupertype(context: Type, contextRawType: Class<*>, supertype: Class<*>): Type {
397-
if (!supertype.isAssignableFrom(contextRawType)) throw IllegalArgumentException()
398-
return getGenericSupertype(context, contextRawType, supertype).resolve((context), (contextRawType))
400+
require(supertype.isAssignableFrom(contextRawType))
401+
return getGenericSupertype(context, contextRawType, supertype).resolve(context, contextRawType)
399402
}
400403

401404
internal fun <T : Annotation?> createJsonQualifierImplementation(annotationType: Class<T>): T {
@@ -456,7 +459,7 @@ public fun Moshi.generatedAdapter(
456459
// Common case first
457460
constructor = adapterClass.getDeclaredConstructor(Moshi::class.java, Array<Type>::class.java)
458461
args = arrayOf(this, typeArgs)
459-
} catch (e: NoSuchMethodException) {
462+
} catch (_: NoSuchMethodException) {
460463
constructor = adapterClass.getDeclaredConstructor(Array<Type>::class.java)
461464
args = arrayOf(typeArgs)
462465
}
@@ -465,7 +468,7 @@ public fun Moshi.generatedAdapter(
465468
// Common case first
466469
constructor = adapterClass.getDeclaredConstructor(Moshi::class.java)
467470
args = arrayOf(this)
468-
} catch (e: NoSuchMethodException) {
471+
} catch (_: NoSuchMethodException) {
469472
constructor = adapterClass.getDeclaredConstructor()
470473
args = emptyArray()
471474
}
@@ -629,7 +632,7 @@ internal class ParameterizedTypeImpl private constructor(
629632
if (rawType is Class<*>) {
630633
val enclosingClass = rawType.enclosingClass
631634
if (ownerType != null) {
632-
require(!(enclosingClass == null || ownerType.rawType != enclosingClass)) { "unexpected owner type for $rawType: $ownerType" }
635+
require(enclosingClass != null && ownerType.rawType == enclosingClass) { "unexpected owner type for $rawType: $ownerType" }
633636
} else {
634637
require(enclosingClass == null) { "unexpected owner type for $rawType: null" }
635638
}
@@ -706,7 +709,7 @@ internal class WildcardTypeImpl private constructor(
706709
require(upperBounds.size == 1)
707710
return if (lowerBounds.size == 1) {
708711
lowerBounds[0].checkNotPrimitive()
709-
require(!(upperBounds[0] !== Any::class.java))
712+
require(upperBounds[0] === Any::class.java)
710713
WildcardTypeImpl(
711714
lowerBound = lowerBounds[0].canonicalize(),
712715
upperBound = Any::class.java,

0 commit comments

Comments
 (0)