-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add TypedOptions * apiDump * Expose the basic TypedOptions constructor * Spotless * Update okio/src/commonMain/kotlin/okio/TypedOptions.kt Co-authored-by: Jake Wharton <[email protected]> --------- Co-authored-by: Jake Wharton <[email protected]>
- Loading branch information
1 parent
13e2f46
commit 0d76d41
Showing
12 changed files
with
234 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package okio | ||
|
||
import kotlin.jvm.JvmStatic | ||
|
||
/** | ||
* A list of values that may be read with [BufferedSource.select]. | ||
* | ||
* Also consider [Options] to select an integer index. | ||
*/ | ||
class TypedOptions<T : Any>( | ||
list: List<T>, | ||
internal val options: Options, | ||
) : AbstractList<T>(), RandomAccess { | ||
internal val list = list.toList() // Defensive copy. | ||
|
||
init { | ||
require(this.list.size == options.size) | ||
} | ||
|
||
override val size: Int | ||
get() = list.size | ||
|
||
override fun get(index: Int) = list[index] | ||
|
||
companion object { | ||
@JvmStatic | ||
inline fun <T : Any> of( | ||
values: Iterable<T>, | ||
encode: (T) -> ByteString, | ||
): TypedOptions<T> { | ||
val list = values.toList() | ||
val options = Options.of(*Array(list.size) { encode(list[it]) }) | ||
return TypedOptions(list, options) | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
okio/src/commonMain/kotlin/okio/internal/BufferedSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
@file:JvmName("-BufferedSource") // A leading '-' hides this class from Java. | ||
|
||
package okio.internal | ||
|
||
import kotlin.jvm.JvmName | ||
import okio.BufferedSource | ||
import okio.TypedOptions | ||
|
||
internal inline fun <T : Any> BufferedSource.commonSelect(options: TypedOptions<T>): T? { | ||
return when (val index = select(options.options)) { | ||
-1 -> null | ||
else -> options[index] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (C) 2024 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package okio | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
import okio.ByteString.Companion.encodeUtf8 | ||
|
||
class TypedOptionsTest { | ||
@Test | ||
fun happyPath() { | ||
val colors = listOf("Red", "Green", "Blue") | ||
val colorOptions = TypedOptions.of(colors) { it.lowercase().encodeUtf8() } | ||
val buffer = Buffer().writeUtf8("bluegreenyellow") | ||
assertEquals("Blue", buffer.select(colorOptions)) | ||
assertEquals("greenyellow", buffer.snapshot().utf8()) | ||
assertEquals("Green", buffer.select(colorOptions)) | ||
assertEquals("yellow", buffer.snapshot().utf8()) | ||
assertEquals(null, buffer.select(colorOptions)) | ||
assertEquals("yellow", buffer.snapshot().utf8()) | ||
} | ||
|
||
@Test | ||
fun typedOptionsConstructor() { | ||
val colors = listOf("Red", "Green", "Blue") | ||
val colorOptions = TypedOptions( | ||
colors, | ||
Options.of("red".encodeUtf8(), "green".encodeUtf8(), "blue".encodeUtf8()), | ||
) | ||
val buffer = Buffer().writeUtf8("bluegreenyellow") | ||
assertEquals("Blue", buffer.select(colorOptions)) | ||
assertEquals("greenyellow", buffer.snapshot().utf8()) | ||
assertEquals("Green", buffer.select(colorOptions)) | ||
assertEquals("yellow", buffer.snapshot().utf8()) | ||
assertEquals(null, buffer.select(colorOptions)) | ||
assertEquals("yellow", buffer.snapshot().utf8()) | ||
} | ||
|
||
@Test | ||
fun typedOptionsConstructorEnforcesSizeMatch() { | ||
val colors = listOf("Red", "Green", "Blue") | ||
assertFailsWith<IllegalArgumentException> { | ||
TypedOptions( | ||
colors, | ||
Options.of("red".encodeUtf8(), "green".encodeUtf8()), | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
fun listFunctionsWork() { | ||
val colors = listOf("Red", "Green", "Blue") | ||
val colorOptions = TypedOptions.of(colors) { it.lowercase().encodeUtf8() } | ||
assertEquals(3, colorOptions.size) | ||
assertEquals("Red", colorOptions[0]) | ||
assertEquals("Green", colorOptions[1]) | ||
assertEquals("Blue", colorOptions[2]) | ||
assertFailsWith<IndexOutOfBoundsException> { | ||
colorOptions[3] | ||
} | ||
} | ||
|
||
/** | ||
* Confirm we can mutate the collection used to create our [TypedOptions] without corrupting its | ||
* behavior. | ||
*/ | ||
@Test | ||
fun safeToMutateSourceCollectionAfterConstruction() { | ||
val colors = mutableListOf("Red", "Green") | ||
val colorOptions = TypedOptions.of(colors) { it.lowercase().encodeUtf8() } | ||
colors[0] = "Black" | ||
|
||
val buffer = Buffer().writeUtf8("red") | ||
assertEquals("Red", buffer.select(colorOptions)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters