diff --git a/src/main/kotlin/MultiSortedList.kt b/src/main/kotlin/MultiSortedList.kt index 8eaa4f1..194d4eb 100644 --- a/src/main/kotlin/MultiSortedList.kt +++ b/src/main/kotlin/MultiSortedList.kt @@ -1,20 +1,36 @@ import java.util.* import kotlin.Comparator +import kotlin.collections.ArrayList class MultiSortedList constructor( underlying: MutableList, + generator: () -> MutableList, comparator: Comparator, vararg extraComparators: Comparator ): SortedList(underlying, comparator) { companion object { fun > ofComparable( - underlying: MutableList = ArrayList(), + underlying: MutableList, + generator: () -> MutableList = ::ArrayList, comparator: Comparator = Comparator { a, b -> a.compareTo(b) }, vararg extraComparators: Comparator - ) = MultiSortedList(underlying, comparator, *extraComparators) + ) = MultiSortedList(underlying, generator, comparator, *extraComparators) + + fun > ofComparable( + generator: () -> MutableList = ::ArrayList, + comparator: Comparator = Comparator { a, b -> a.compareTo(b) }, + vararg extraComparators: Comparator + ) = MultiSortedList(generator(), generator, comparator, *extraComparators) } - private var extraLists = extraComparators.associateWith { SortedList(underlying.subList(0, underlying.size), it) } + constructor(generator: () -> MutableList, comparator: Comparator, vararg extraComparators: Comparator): + this(generator(), generator, comparator, *extraComparators) + + private var extraLists = extraComparators.associateWith { + val list = generator() + Collections.copy(list, underlying) + SortedList(list, it) + } override fun add(element: E): Boolean { extraLists.values.forEach { diff --git a/src/main/kotlin/PortalManager.kt b/src/main/kotlin/PortalManager.kt index a723046..68d9536 100644 --- a/src/main/kotlin/PortalManager.kt +++ b/src/main/kotlin/PortalManager.kt @@ -24,8 +24,8 @@ private const val DEFAULT_COOLDOWN_MIN = 5L class PortalManager(private val data: ConfigurationSection, private val config: () -> ConfigurationSection): Listener { private val players = PlayerMapper(data, PATH_DATA_PLAYERS) private val worlds = WorldMapper(data, PATH_DATA_WORLDS) - private var portals = MultiSortedList(ArrayList(), COMPARATOR_PORTAL_LOCATION_OWNER, COMPARATOR_PORTAL_UID, COMPARATOR_PORTAL_OWNER_NAME, COMPARATOR_PORTAL_LINKS) - private var invitations = MultiSortedList(ArrayList(), COMPARATOR_INVITE_RECIPIENT, COMPARATOR_INVITE_PORTAL) + private var portals = MultiSortedList(::ArrayList, COMPARATOR_PORTAL_LOCATION_OWNER, COMPARATOR_PORTAL_UID, COMPARATOR_PORTAL_OWNER_NAME, COMPARATOR_PORTAL_LINKS) + private var invitations = MultiSortedList(::ArrayList, COMPARATOR_INVITE_RECIPIENT, COMPARATOR_INVITE_PORTAL) private val cooldowns = LinkedList>() private val cooldownsLookup = SortedList(ArrayList(), COMPARATOR_PLAYER) @@ -92,7 +92,7 @@ class PortalManager(private val data: ConfigurationSection, private val config: if (portal.id >= nextUUID) nextUUID = portal.id + 1UL } - portals = MultiSortedList(portalList, COMPARATOR_PORTAL_LOCATION_OWNER, COMPARATOR_PORTAL_UID) + portals = MultiSortedList(portalList, ::ArrayList, COMPARATOR_PORTAL_LOCATION_OWNER, COMPARATOR_PORTAL_UID) if(portals.isEmpty()) nextUUID = UUID(0, 0) else { @@ -105,6 +105,7 @@ class PortalManager(private val data: ConfigurationSection, private val config: invitations = MultiSortedList( data.getStringList(PATH_DATA_INVITES).mapTo(ArrayList(), ::Invite), + ::ArrayList, COMPARATOR_INVITE_RECIPIENT, COMPARATOR_INVITE_PORTAL )