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

love.forte.simbot.utils.StoneArray.kt Maven / Gradle / Ivy

There is a newer version: 3.0.0.preview.0.4
Show newest version
/*
 *
 *  * Copyright (c) 2021. ForteScarlet All rights reserved.
 *  * Project  simple-robot
 *  * File     MiraiAvatar.kt
 *  *
 *  * You can contact the author through the following channels:
 *  * github https://github.com/ForteScarlet
 *  * gitee  https://gitee.com/ForteScarlet
 *  * email  [email protected]
 *  * QQ     1149159218
 *
 */

@file:Suppress("unused")

package love.forte.simbot.utils

import love.forte.simbot.utils.StoneArray.Companion.EMPTY


/**
 * 一个数组的视图,但是不允许对数组进行任何修改。
 *
 * @see stoneArrayOf
 * @see asStoneArray
 *
 * @author ForteScarlet
 */
public class StoneArray internal constructor(private val view: Array) : Iterable {

    /**
     * 获取元素。
     */
    operator fun get(index: Int): T = view[index]

    /**
     * 数组长度.
     */
    public val size: Int get() = view.size

    /**
     * 拷贝一个数组的副本。
     */
    public fun copyArray(): Array = view.copyOf()


    override fun iterator(): Iterator = view.iterator()

    internal companion object {
        internal val EMPTY = StoneArray(emptyArray())
    }
}

public fun  StoneArray.isEmpty() = size == 0
public fun  StoneArray.isNotEmpty() = !isEmpty()


@Suppress("UNCHECKED_CAST")
public fun  stoneArrayOf(vararg elements: T): StoneArray = if (elements.isEmpty()) EMPTY as StoneArray else StoneArray(elements)

@Suppress("UNCHECKED_CAST")
public fun  Array.asStoneArray(): StoneArray = if (this.isEmpty()) EMPTY as StoneArray else StoneArray(this)




© 2015 - 2025 Weber Informatics LLC | Privacy Policy