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

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

The 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

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

    return this
}

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

    return Collections.unmodifiableList(this)
}

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

    return Collections.unmodifiableMap(this)
}

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

    return Multimaps.unmodifiableListMultimap(this)
}

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