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

org.jetbrains.exposed.dao.LinkedIdentityHashSet.kt Maven / Gradle / Ivy

There is a newer version: 0.56.0
Show newest version
package org.jetbrains.exposed.dao

import java.util.*

internal class LinkedIdentityHashSet : MutableSet {
    private val set: MutableSet = Collections.newSetFromMap(IdentityHashMap())
    private val list: MutableList = LinkedList()

    override fun add(element: T): Boolean {
        return set.add(element).also { if (it) list.add(element) }
    }

    override fun addAll(elements: Collection): Boolean {
        val toAdd = elements.filter { it !in set } // Maintain order
        if (toAdd.isEmpty()) return false
        set.addAll(toAdd)
        list.addAll(toAdd)
        return true
    }

    override fun clear() {
        set.clear()
        list.clear()
    }

    override fun iterator(): MutableIterator {
        return object : MutableIterator {
            private val delegate = list.iterator()
            private var current: T? = null

            override fun hasNext() = delegate.hasNext()

            override fun next() = delegate.next().also {
                current = it
            }

            override fun remove() {
                val p = checkNotNull(current)
                [email protected](p)
                current = null
            }
        }
    }

    override fun remove(element: T): Boolean {
        return set.remove(element).also { if (it) list.remove(element) }
    }

    override fun removeAll(elements: Collection): Boolean {
        val toRemove = set intersect elements
        if (toRemove.isEmpty()) return false
        set.removeAll(toRemove)
        list.removeAll(toRemove)
        return true
    }

    override fun retainAll(elements: Collection): Boolean {
        return removeAll(set subtract elements)
    }

    override val size: Int
        get() = set.size

    override fun contains(element: T): Boolean {
        return set.contains(element)
    }

    override fun containsAll(elements: Collection): Boolean {
        return set.containsAll(elements)
    }

    override fun isEmpty(): Boolean {
        return set.isEmpty()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy