All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.increase.api.core.Utils.kt Maven / Gradle / Ivy

There is a newer version: 0.106.0
Show newest version
@file:JvmName("Utils")

package com.increase.api.core

import com.google.common.collect.ImmutableListMultimap
import com.google.common.collect.ListMultimap
import com.google.common.collect.Multimaps
import com.increase.api.errors.IncreaseInvalidDataException
import java.util.Collections

@JvmSynthetic
internal fun  T?.getOrThrow(name: String): T {
    if (this == null) {
        throw IncreaseInvalidDataException("'${name}' is not present")
    }

    return this
}

@JvmSynthetic
internal fun  List.toUnmodifiable(): List {
    if (isEmpty()) {
        return Collections.emptyList()
    }

    return Collections.unmodifiableList(this)
}

@JvmSynthetic
internal fun  Map.toUnmodifiable(): Map {
    if (isEmpty()) {
        return Collections.emptyMap()
    }

    return Collections.unmodifiableMap(this)
}

@JvmSynthetic
internal fun  ListMultimap.toUnmodifiable(): ListMultimap {
    if (isEmpty()) {
        return ImmutableListMultimap.of()
    }

    return Multimaps.unmodifiableListMultimap(this)
}

@JvmSynthetic
internal fun ListMultimap.getRequiredHeader(header: String): String {
    val value =
        entries()
            .stream()
            .filter { entry -> entry.key.equals(header, ignoreCase = true) }
            .map { entry -> entry.value }
            .findFirst()
    if (!value.isPresent) {
        throw IncreaseInvalidDataException("Could not find $header header")
    }
    return value.get()
}

internal interface Enum




© 2015 - 2024 Weber Informatics LLC | Privacy Policy