Repository: square/okhttp
File: okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/-UtilJvm.kt (Kotlin)

Code before:
  val oldName = currentThread.name
  currentThread.name = name
  try {
    block()
  } finally {
    currentThread.name = oldName
  }
}

/** Returns the Content-Length as reported by the response headers. */
internal fun Response.headersContentLength(): Long = headers["Content-Length"]?.toLongOrDefault(-1L) ?: -1L

/** Returns an immutable wrap of this. */
@Suppress("NOTHING_TO_INLINE")
internal inline fun <T> List<T>.unmodifiable(): List<T> = Collections.unmodifiableList(this)

/** Returns an immutable wrap of this. */
@Suppress("NOTHING_TO_INLINE")
internal inline fun <T> Set<T>.unmodifiable(): Set<T> = Collections.unmodifiableSet(this)

/** Returns an immutable wrap of this. */
@Suppress("NOTHING_TO_INLINE")
internal inline fun <K, V> Map<K, V>.unmodifiable(): Map<K, V> = Collections.unmodifiableMap(this)

/** Returns an immutable copy of this. */
internal inline fun <reified T> List<T>.toImmutableList(): List<T> =
  when {
    this.isEmpty() -> emptyList()
    this.size == 1 -> Collections.singletonList(this[0])
    else -> this.toTypedArray().asList().unmodifiable()
  }

/** Returns an immutable list containing [elements]. */
@SafeVarargs
internal fun <T> immutableListOf(vararg elements: T): List<T> = elements.toImmutableList()

/** Returns an immutable list from copy of this. */
internal fun <T> Array<out T>?.toImmutableList(): List<T> =
  when {
    this.isNullOrEmpty() -> emptyList()
    this.size == 1 -> Collections.singletonList(this[0])
    else -> this.clone().asList().unmodifiable()
  }

/** Closes this, ignoring any checked exceptions. */
internal fun Socket.closeQuietly() {
  try {
    close()
  } catch (e: AssertionError) {
    throw e
  } catch (rethrown: RuntimeException) {

Review comment (suggestion):
This is a lot of code to inline. We can eliminate the need for `inline`+`reified` by doing something like `ArrayList(this).unmodifiable()` instead.

Code after:
  val oldName = currentThread.name
  currentThread.name = name
  try {
    block()
  } finally {
    currentThread.name = oldName
  }
}

/** Returns the Content-Length as reported by the response headers. */
internal fun Response.headersContentLength(): Long = headers["Content-Length"]?.toLongOrDefault(-1L) ?: -1L

/** Returns an immutable wrap of this. */
@Suppress("NOTHING_TO_INLINE")
internal inline fun <T> List<T>.unmodifiable(): List<T> = Collections.unmodifiableList(this)

/** Returns an immutable wrap of this. */
@Suppress("NOTHING_TO_INLINE")
internal inline fun <T> Set<T>.unmodifiable(): Set<T> = Collections.unmodifiableSet(this)

/** Returns an immutable wrap of this. */
@Suppress("NOTHING_TO_INLINE")
internal inline fun <K, V> Map<K, V>.unmodifiable(): Map<K, V> = Collections.unmodifiableMap(this)

/** Returns an immutable copy of this. */
@Suppress("UNCHECKED_CAST")
internal fun <T> List<T>.toImmutableList(): List<T> =
  when {
    this.isEmpty() -> emptyList()
    this.size == 1 -> Collections.singletonList(this[0])
    // Collection.toArray returns Object[] (covariant).
    // It is faster than creating real T[] via reflection (Arrays.copyOf).
    else -> (this as java.util.Collection<*>).toArray().asList().unmodifiable() as List<T>
  }

/** Returns an immutable list containing [elements]. */
@SafeVarargs
internal fun <T> immutableListOf(vararg elements: T): List<T> = elements.toImmutableList()

/** Returns an immutable list from copy of this. */
internal fun <T> Array<out T>?.toImmutableList(): List<T> =
  when {
    this.isNullOrEmpty() -> emptyList()
    this.size == 1 -> Collections.singletonList(this[0])
    else -> this.clone().asList().unmodifiable()
  }

/** Closes this, ignoring any checked exceptions. */
internal fun Socket.closeQuietly() {
  try {
    close()