8000 Modify the implementation of readonly list/set by MukjepScarlet · Pull Request #8754 · square/okhttp · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Modify the implementation of readonly list/set #8754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
8000
Diff view
4 changes: 2 additions & 2 deletions okhttp/src/commonJvmAndroid/kotlin/okhttp3/Challenge.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ package okhttp3

import java.nio.charset.Charset
import java.util.Collections.singletonMap
import java.util.Collections.unmodifiableMap
import java.util.Locale.US
import kotlin.text.Charsets.ISO_8859_1
import okhttp3.internal.commonEquals
import okhttp3.internal.commonHashCode
import okhttp3.internal.commonToString
import okhttp3.internal.unmodifiable

/**
* An [RFC 7235][rfc_7235] challenge.
Expand Down Expand Up @@ -68,7 +68,7 @@ class Challenge(
val newKey = key?.lowercase(US)
newAuthParams[newKey] = value
}
this.authParams = unmodifiableMap<String?, String>(newAuthParams)
this.authParams = newAuthParams.unmodifiable()
}

/** Returns a copy of this charset that expects a credential encoded with [charset]. */
Expand Down
8 changes: 2 additions & 6 deletions okhttp/src/commonJvmAndroid/kotlin/okhttp3/Cookie.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package okhttp3

import java.util.Calendar
import java.util.Collections
import java.util.Date
import java.util.GregorianCalendar
import java.util.Locale
Expand All @@ -30,6 +29,7 @@ import okhttp3.internal.indexOfControlOrNonAscii
import okhttp3.internal.publicsuffix.PublicSuffixDatabase
import okhttp3.internal.toCanonicalHost
import okhttp3.internal.trimSubstring
import okhttp3.internal.unmodifiable
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

/**
Expand Down Expand Up @@ -720,11 +720,7 @@ class Cookie private constructor(
cookies.add(cookie)
}

return if (cookies != null) {
Collections.unmodifiableList(cookies)
} else {
emptyList()
}
return cookies?.unmodifiable().orEmpty()
}
}
}
6 changes: 3 additions & 3 deletions okhttp/src/commonJvmAndroid/kotlin/okhttp3/Dispatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package okhttp3

import java.util.ArrayDeque
import java.util.Collections
import java.util.Deque
import java.util.concurrent.ExecutorService
import java.util.concurrent.SynchronousQueue
Expand All @@ -29,6 +28,7 @@ import okhttp3.internal.connection.RealCall
import okhttp3.internal.connection.RealCall.AsyncCall
import okhttp3.internal.okHttpName
import okhttp3.internal.threadFactory
import okhttp3.internal.unmodifiable

/**
* Policy on when async requests are executed.
Expand Down Expand Up @@ -259,13 +259,13 @@ class Dispatcher() {
/** Returns a snapshot of the calls currently awaiting execution. */
fun queuedCalls(): List<Call> =
this.withLock {
return Collections.unmodifiableList(readyAsyncCalls.map { it.call })
return readyAsyncCalls.map { it.call }.unmodifiable()
}

/** Returns a snapshot of the calls currently being executed. */
fun runningCalls(): List<Call> =
this.withLock {
return Collections.unmodifiableList(runningSyncCalls + runningAsyncCalls.map { it.call })
return (runningSyncCalls + runningAsyncCalls.map { it.call }).unmodifiable()
}

fun queuedCallsCount(): Int = this.withLock { readyAsyncCalls.size }
Expand Down
4 changes: 2 additions & 2 deletions okhttp/src/commonJvmAndroid/kotlin/okhttp3/Headers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package okhttp3

import java.time.Instant
import java.util.Collections
import java.util.Date
import java.util.Locale
import java.util.TreeMap
Expand All @@ -44,6 +43,7 @@ import okhttp3.internal.commonValues
import okhttp3.internal.headersCheckName
import okhttp3.internal.http.toHttpDateOrNull
import okhttp3.internal.http.toHttpDateString
import okhttp3.internal.unmodifiable
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

/**
Expand Down Expand Up @@ -107,7 +107,7 @@ class Headers internal constructor(
for (i in 0 until size) {
result.add(name(i))
}
return Collections.unmodifiableSet(result)
return result.unmodifiable()
}

/** Returns an immutable list of the header values for `name`. */
Expand Down
10 changes: 5 additions & 5 deletions okhttp/src/commonJvmAndroid/kotlin/okhttp3/HttpUrl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import okhttp3.internal.delimiterOffset
import okhttp3.internal.indexOfFirstNonAsciiWhitespace
import okhttp3.internal.indexOfLastNonAsciiWhitespace
import okhttp3.internal.publicsuffix.PublicSuffixDatabase
import okhttp3.internal.readOnly
import okhttp3.internal.toCanonicalHost
import okhttp3.internal.unmodifiable
import okhttp3.internal.url.FRAGMENT_ENCODE_SET
import okhttp3.internal.url.FRAGMENT_ENCODE_SET_URI
import okhttp3.internal.url.PASSWORD_ENCODE_SET
Expand Down Expand Up @@ -617,11 +617,11 @@ class HttpUrl private constructor(
val queryParameterNames: Set<String>
get() {
if (queryNamesAndValues == null) return emptySet()
val result = LinkedHashSet<String>()
val result = LinkedHashSet<String>(queryNamesAndValues.size / 2, 1.0F)
for (i in 0 until queryNamesAndValues.size step 2) {
result.add(queryNamesAndValues[i]!!)
}
return result.readOnly()
return result.unmodifiable()
}

/**
Expand All @@ -639,13 +639,13 @@ class HttpUrl private constructor(
*/
fun queryParameterValues(name: String): List<String?> {
if (queryNamesAndValues == null) return emptyList()
val result = mutableListOf<String?>()
val result = ArrayList<String?>(4)
for (i in 0 until queryNamesAndValues.size step 2) {
if (name == queryNamesAndValues[i]) {
result.add(queryNamesAndValues[i + 1])
}
}
return result.readOnly()
return result.unmodifiable()
}

/**
Expand Down
4 changes: 2 additions & 2 deletions okhttp/src/commonJvmAndroid/kotlin/okhttp3/OkHttpClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import java.net.Proxy
import java.net.ProxySelector
import java.net.Socket
import java.time.Duration
import java.util.Collections
import java.util.Random
import java.util.concurrent.ExecutorService
import java.util.concurrent.TimeUnit
Expand All @@ -42,6 +41,7 @@ import okhttp3.internal.proxy.NullProxySelector
import okhttp3.internal.tls.CertificateChainCleaner
import okhttp3.internal.tls.OkHostnameVerifier
import okhttp3.internal.toImmutableList
import okhttp3.internal.unmodifiable
import okhttp3.internal.ws.RealWebSocket
import okio.Sink
import okio.Source
Expand Down Expand Up @@ -1054,7 +1054,7 @@ open class OkHttpClient internal constructor(
}

// Assign as an unmodifiable list. This is effectively immutable.
this.protocols = Collections.unmodifiableList(protocolsCopy)
this.protocols = protocolsCopy.unmodifiable()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ internal fun Headers.commonValues(name: String): List<String> {
result.add(value(i))
}
}
return result?.toList().orEmpty()
return result?.unmodifiable().orEmpty()
}

internal fun Headers.commonIterator(): Iterator<Pair<String, String>> = Array(size) { name(it) to value(it) }.iterator()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,3 @@ internal fun <T> interleave(
}
}
}

// TODO check read only options for creating lists
public fun <T> List<T>.readOnly() = this.toList()

// TODO check read only options for creating lists
public fun <T> Set<T>.readOnly() = this.toSet()
18 changes: 15 additions & 3 deletions okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/-UtilJvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,27 @@ internal inline fun threadName(
/** Returns the Content-Length as reported by the response headers. */
internal fun Response.headersContentLength(): Long = headers["Content-Length"]?.toLongOrNull() ?: -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> = Collections.unmodifiableList(toTypedArray().asList())
internal inline fun <reified T> List<T>.toImmutableList(): List<T> = this.toTypedArray().toImmutableList()

/** Returns an immutable list containing [elements]. */
@SafeVarargs
internal fun <T> immutableListOf(vararg elements: T): List<T> = Collections.unmodifiableList(elements.asList())
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> = this?.let { Collections.unmodifiableList(it.copyOf().asList()) } ?: emptyList()
internal fun <T> Array<out T>?.toImmutableList(): List<T> = if (this.isNullOrEmpty()) emptyList() else this.asList().unmodifiable()

/** Closes this, ignoring any checked exceptions. */
internal fun Socket.closeQuietly() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private fun Buffer.readChallengeHeader(result: MutableList<Challenge>) {
result.add(
Challenge(
schemeName,
Collections.singletonMap<String, String>(null, peek + "=".repeat(eqCount)),
Collections.singletonMap<String?, String>(null, peek + "=".repeat(eqCount)),
),
)
peek = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ package okhttp3.internal.http2

import java.io.IOException
import java.util.Arrays
import java.util.Collections
import okhttp3.internal.and
import okhttp3.internal.http2.Header.Companion.RESPONSE_STATUS
import okhttp3.internal.http2.Header.Companion.TARGET_AUTHORITY
import okhttp3.internal.http2.Header.Companion.TARGET_METHOD
import okhttp3.internal.http2.Header.Companion.TARGET_PATH
import okhttp3.internal.http2.Header.Companion.TARGET_SCHEME
import okhttp3.internal.unmodifiable
import okio.Buffer
import okio.BufferedSource
import okio.ByteString
Expand Down Expand Up @@ -389,13 +389,13 @@ object Hpack {
}

private fun nameToFirstIndex(): Map<ByteString, Int> {
val result = LinkedHashMap<ByteString, Int>(STATIC_HEADER_TABLE.size)
val result = LinkedHashMap<ByteString, Int>(STATIC_HEADER_TABLE.size, 1.0F)
for (i in STATIC_HEADER_TABLE.indices) {
if (!result.containsKey(STATIC_HEADER_TABLE[i].name)) {
result[STATIC_HEADER_TABLE[i].name] = i
}
}
return Collections.unmodifiableMap(result)
return result.unmodifiable()
}

class Writer
Expand Down
3 changes: 1 addition & 2 deletions okhttp/src/jvmTest/kotlin/okhttp3/SocksProxy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import java.net.Proxy
import java.net.ServerSocket
import java.net.Socket
import java.net.SocketException
import java.util.Collections
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
Expand All @@ -50,7 +49,7 @@ class SocksProxy {
private val executor = Executors.newCachedThreadPool(threadFactory("SocksProxy"))
private var serverSocket: ServerSocket? = null
private val connectionCount = AtomicInteger()
private val openSockets = Collections.newSetFromMap(ConcurrentHashMap<Socket, Boolean>())
private val openSockets: MutableSet<Socket> = ConcurrentHashMap.newKeySet()

fun play() {
serverSocket = ServerSocket(0)
Expand Down
Loading
0