
tech.pronghorn.plugins.JCToolsMpscQueuePlugin.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plugin-collections-jctools Show documentation
Show all versions of plugin-collections-jctools Show documentation
A plugin for the Pronghorn Framework utilizing JCTools for various collections.
The newest version!
/*
* Copyright 2017 Pronghorn Technology LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tech.pronghorn.plugins
import org.jctools.queues.MpscArrayQueue
import org.jctools.queues.MpscUnboundedArrayQueue
import tech.pronghorn.plugins.mpscQueue.DrainableQueue
import tech.pronghorn.plugins.mpscQueue.MpscQueuePlugin
public object JCToolsMpscQueuePlugin : MpscQueuePlugin {
private class DrainableMpscArrayQueue(capacity: Int,
private val jctoolsQueue: MpscArrayQueue = MpscArrayQueue(capacity)) : DrainableQueue(jctoolsQueue) {
override fun drainTo(collection: MutableCollection,
maxElements: Int): Int {
return jctoolsQueue.drain({ collection.add(it) }, maxElements)
}
override fun fillFrom(collection: Collection): Int {
val iterator = collection.iterator()
return jctoolsQueue.fill({ iterator.next() }, collection.size)
}
}
private class DrainableMpscUnboundedArrayQueue(private val jctoolsQueue: MpscUnboundedArrayQueue = MpscUnboundedArrayQueue(1024)) : DrainableQueue(jctoolsQueue) {
override fun drainTo(collection: MutableCollection,
maxElements: Int): Int {
return jctoolsQueue.drain({ collection.add(it) }, maxElements)
}
override fun fillFrom(collection: Collection): Int {
val iterator = collection.iterator()
return jctoolsQueue.fill({ iterator.next() }, collection.size)
}
}
override fun getBounded(capacity: Int): DrainableQueue = DrainableMpscArrayQueue(capacity)
override fun getUnbounded(): DrainableQueue = DrainableMpscUnboundedArrayQueue()
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy