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

commonMain.aws.smithy.kotlin.runtime.util.Stack.kt Maven / Gradle / Ivy

/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

package aws.smithy.kotlin.runtime.util

/**
 * Convenience type for treating a list like stack
 */
typealias ListStack = MutableList

/**
 * Push an item to top of stack
 */
fun  ListStack.push(item: T) = add(item)

/**
 * Pop the top of the stack or throw [NoSuchElementException]
 */
fun  ListStack.pop(): T = removeLast()

/**
 * Pop the top of the stack or return null if stack is empty
 */
fun  ListStack.popOrNull(): T? = removeLastOrNull()

/**
 * Return top of stack or throws exception if stack is empty
 */
fun  ListStack.top(): T = this[count() - 1]

/**
 * Return top of stack or null if stack is empty
 */
fun  ListStack.topOrNull(): T? = if (isNotEmpty()) top() else null

/**
 * Pop the top of the stack and push a [item]
 */
fun  ListStack.replaceTop(item: T): T? {
    val lastTop = popOrNull()
    push(item)
    return lastTop
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy