-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
15f22cd
commit 49c0367
Showing
5 changed files
with
674 additions
and
11 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
92 changes: 92 additions & 0 deletions
92
orb-java-core/src/main/kotlin/com/withorb/api/core/http/Headers.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,92 @@ | ||
package com.withorb.api.core.http | ||
|
||
import com.withorb.api.core.toImmutable | ||
import java.util.TreeMap | ||
|
||
class Headers | ||
private constructor( | ||
private val map: Map<String, List<String>>, | ||
@get:JvmName("size") val size: Int | ||
) { | ||
|
||
fun isEmpty(): Boolean = map.isEmpty() | ||
|
||
fun names(): Set<String> = map.keys | ||
|
||
fun values(name: String): List<String> = map[name].orEmpty() | ||
|
||
fun toBuilder(): Builder = Builder().putAll(map) | ||
|
||
companion object { | ||
|
||
@JvmStatic fun builder() = Builder() | ||
} | ||
|
||
class Builder { | ||
|
||
private val map: MutableMap<String, MutableList<String>> = | ||
TreeMap(String.CASE_INSENSITIVE_ORDER) | ||
private var size: Int = 0 | ||
|
||
fun put(name: String, value: String) = apply { | ||
map.getOrPut(name) { mutableListOf() }.add(value) | ||
size++ | ||
} | ||
|
||
fun put(name: String, values: Iterable<String>) = apply { values.forEach { put(name, it) } } | ||
|
||
fun putAll(headers: Map<String, Iterable<String>>) = apply { headers.forEach(::put) } | ||
|
||
fun putAll(headers: Headers) = apply { | ||
headers.names().forEach { put(it, headers.values(it)) } | ||
} | ||
|
||
fun remove(name: String) = apply { size -= map.remove(name).orEmpty().size } | ||
|
||
fun removeAll(names: Set<String>) = apply { names.forEach(::remove) } | ||
|
||
fun clear() = apply { | ||
map.clear() | ||
size = 0 | ||
} | ||
|
||
fun replace(name: String, value: String) = apply { | ||
remove(name) | ||
put(name, value) | ||
} | ||
|
||
fun replace(name: String, values: Iterable<String>) = apply { | ||
remove(name) | ||
put(name, values) | ||
} | ||
|
||
fun replaceAll(headers: Map<String, Iterable<String>>) = apply { | ||
headers.forEach(::replace) | ||
} | ||
|
||
fun replaceAll(headers: Headers) = apply { | ||
headers.names().forEach { replace(it, headers.values(it)) } | ||
} | ||
|
||
fun build() = | ||
Headers( | ||
map.mapValuesTo(TreeMap(String.CASE_INSENSITIVE_ORDER)) { (_, values) -> | ||
values.toImmutable() | ||
} | ||
.toImmutable(), | ||
size | ||
) | ||
} | ||
|
||
override fun hashCode(): Int = map.hashCode() | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) { | ||
return true | ||
} | ||
|
||
return other is Headers && map == other.map | ||
} | ||
|
||
override fun toString(): String = "Headers{map=$map}" | ||
} |
86 changes: 86 additions & 0 deletions
86
orb-java-core/src/main/kotlin/com/withorb/api/core/http/QueryParams.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,86 @@ | ||
package com.withorb.api.core.http | ||
|
||
import com.withorb.api.core.toImmutable | ||
|
||
class QueryParams | ||
private constructor( | ||
private val map: Map<String, List<String>>, | ||
@get:JvmName("size") val size: Int | ||
) { | ||
|
||
fun isEmpty(): Boolean = map.isEmpty() | ||
|
||
fun keys(): Set<String> = map.keys | ||
|
||
fun values(key: String): List<String> = map[key].orEmpty() | ||
|
||
fun toBuilder(): Builder = Builder().putAll(map) | ||
|
||
companion object { | ||
|
||
@JvmStatic fun builder() = Builder() | ||
} | ||
|
||
class Builder { | ||
|
||
private val map: MutableMap<String, MutableList<String>> = mutableMapOf() | ||
private var size: Int = 0 | ||
|
||
fun put(key: String, value: String) = apply { | ||
map.getOrPut(key) { mutableListOf() }.add(value) | ||
size++ | ||
} | ||
|
||
fun put(key: String, values: Iterable<String>) = apply { values.forEach { put(key, it) } } | ||
|
||
fun putAll(queryParams: Map<String, Iterable<String>>) = apply { | ||
queryParams.forEach(::put) | ||
} | ||
|
||
fun putAll(queryParams: QueryParams) = apply { | ||
queryParams.keys().forEach { put(it, queryParams.values(it)) } | ||
} | ||
|
||
fun replace(key: String, value: String) = apply { | ||
remove(key) | ||
put(key, value) | ||
} | ||
|
||
fun replace(key: String, values: Iterable<String>) = apply { | ||
remove(key) | ||
put(key, values) | ||
} | ||
|
||
fun replaceAll(queryParams: Map<String, Iterable<String>>) = apply { | ||
queryParams.forEach(::replace) | ||
} | ||
|
||
fun replaceAll(queryParams: QueryParams) = apply { | ||
queryParams.keys().forEach { replace(it, queryParams.values(it)) } | ||
} | ||
|
||
fun remove(key: String) = apply { size -= map.remove(key).orEmpty().size } | ||
|
||
fun removeAll(keys: Set<String>) = apply { keys.forEach(::remove) } | ||
|
||
fun clear() = apply { | ||
map.clear() | ||
size = 0 | ||
} | ||
|
||
fun build() = | ||
QueryParams(map.mapValues { (_, values) -> values.toImmutable() }.toImmutable(), size) | ||
} | ||
|
||
override fun hashCode(): Int = map.hashCode() | ||
|
||
override fun equals(other: Any?): Boolean { | ||
if (this === other) { | ||
return true | ||
} | ||
|
||
return other is QueryParams && map == other.map | ||
} | ||
|
||
override fun toString(): String = "QueryParams{map=$map}" | ||
} |
Oops, something went wrong.