/* * Copyright 2020 dorkbox, 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 dorkbox.objectPool import com.conversantmedia.util.concurrent.DisruptorBlockingQueue import dorkbox.objectPool.blocking.BlockingPool import dorkbox.objectPool.nonBlocking.BoundedNonBlockingPool import dorkbox.objectPool.nonBlocking.NonBlockingPool import dorkbox.objectPool.nonBlocking.NonBlockingSoftPool import dorkbox.objectPool.suspending.SuspendingPool import java.lang.ref.SoftReference import java.util.* import java.util.concurrent.BlockingQueue import java.util.concurrent.ConcurrentLinkedQueue /** * @author dorkbox, llc */ object ObjectPool { /** * Gets the version number. */ const val version = "3.4" init { // Add this project to the updates system, which verifies this class + UUID + version information dorkbox.updates.Updates.add(ObjectPool::class.java, "1dc60a2801d941cba9c7964255d8b061", version) } /** * Creates a suspending pool of a specific size, where the entire pool is initially filled, and when the pool is empty, a * [Pool.take] will wait for a corresponding [Pool.put]. * * @param poolObject controls the lifecycle of the pooled objects. * @param size the size of the pool to create * @param the type of object used in the pool * * @return a suspending pool using the kotlin Channel implementation of a specific size */ fun suspending(poolObject: SuspendingPoolObject, size: Int): dorkbox.objectPool.SuspendingPool { return SuspendingPool(poolObject, size) } /** * Creates a high-performance blocking pool of a specific size, where the entire pool is initially filled, and when the pool is empty, a * [Pool.take] will wait for a corresponding [Pool.put]. * * @param poolObject controls the lifecycle of the pooled objects. * @param size the size of the pool to create * @param the type of object used in the pool * * @return a blocking pool using the DisruptorBlockingQueue implementation of a specific size */ fun blocking(poolObject: PoolObject, size: Int): Pool { return blocking(poolObject, DisruptorBlockingQueue(size), size) } /** * Creates a blocking pool of a specific size, where the entire pool is initially filled, and when the pool is empty, a * [Pool.take] will wait for a corresponding [Pool.put]. * * @param poolObject controls the lifecycle of the pooled objects. * @param queue the blocking queue implementation to use * @param the type of object used in the pool * * @return a blocking pool using the specified [BlockingQueue] implementation of a specific size */ fun blocking(poolObject: PoolObject, queue: BlockingQueue, size: Int): Pool { return BlockingPool(poolObject, queue, size) } /** * Creates a non-blocking pool which will grow as much as needed. * * If the pool is empty, new objects will be created. The items in the pool will never expire or be automatically garbage collected. * * (see [ObjectPool.nonBlockingSoftReference] for pooled objects that will expire/GC as needed). * * @param poolObject controls the lifecycle of the pooled objects. * @param the type of object used in the pool * * @return a blocking pool using the default [ConcurrentLinkedQueue] implementation */ fun nonBlocking(poolObject: PoolObject): Pool { return NonBlockingPool(poolObject) } /** * Creates a non-blocking pool which will grow as much as needed. * * If the pool is empty, new objects will be created. The items in the pool will never expire or be automatically garbage collected. * * (see [ObjectPool.nonBlockingSoftReference] for pooled objects that will expire/GC as needed). * * @param poolObject controls the lifecycle of the pooled objects. * @param queue the queue implementation to use * @param the type of object used in the pool * * @return a blocking pool using the default ConcurrentLinkedQueue implementation */ fun nonBlocking(poolObject: PoolObject, queue: Queue): Pool { return NonBlockingPool(poolObject, queue) } /** * Creates a non-blocking pool which will grow as much as needed. * * If the pool is empty, new objects will be created. The items in the pool will expire and be automatically Garbage Collected in * response to memory demand. * * (See [ObjectPool.nonBlocking] for pooled objects that will never expire). * * @param poolObject controls the lifecycle of the pooled objects. * @param the type of object used in the pool * * @return a blocking pool using the default ConcurrentLinkedQueue implementation */ fun nonBlockingSoftReference(poolObject: PoolObject): Pool { return NonBlockingSoftPool(poolObject) } /** * Creates a non-blocking pool which will grow as much as needed. * * If the pool is empty, new objects will be created. The items in the pool will expire and be automatically Garbage Collected in * response to memory demand. * * (See [ObjectPool.nonBlocking] for pooled objects that will never expire). * * @param poolObject controls the lifecycle of the pooled objects. * @param queue the queue implementation to use * @param the type of object used in the pool * * @return a blocking pool using the specified Queue implementation */ fun nonBlockingSoftReference(poolObject: PoolObject, queue: Queue>): Pool { return NonBlockingSoftPool(poolObject, queue) } /** * A non-blocking pool which will create as many objects as much as needed but will only store maxSize in the pool. * If the pool is empty, new objects will be created. * The items added to pool will never expire or be automatically garbage collected. * The items not added back to the pool will be garbage collected * * @param poolObject controls the lifecycle of the pooled objects. * @param maxSize controls the maxSize the pool can be * @param the type of object used in the pool * * @return a blocking pool using the default ConcurrentLinkedQueue implementation */ fun nonBlockingBounded(poolObject: BoundedPoolObject, maxSize: Long): Pool { return BoundedNonBlockingPool(poolObject, maxSize) } /** * A non-blocking pool which will create as many objects as much as needed but will only store maxSize in the pool. * If the pool is empty, new objects will be created. * The items added to pool will never expire or be automatically garbage collected. * The items not added back to the pool will be garbage collected * * @param poolObject controls the lifecycle of the pooled objects. * @param maxSize controls the maxSize the pool can be * @param queue the queue implementation to use * @param the type of object used in the pool * * @return a blocking pool using the default ConcurrentLinkedQueue implementation */ fun nonBlockingBounded(poolObject: BoundedPoolObject, maxSize: Long, queue: Queue): Pool { return BoundedNonBlockingPool(poolObject, maxSize, queue) } }