This is an automated email from the ASF dual-hosted git repository. tv pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/turbine-fulcrum-pool.git
commit 072425ee647a851672750ab6250a15a069c87e98 Author: Thomas Vandahl <[email protected]> AuthorDate: Mon Jan 12 20:24:23 2026 +0100 Use standard JDK queue implementation --- .../org/apache/fulcrum/pool/BoundedBuffer.java | 152 --------------------- src/java/org/apache/fulcrum/pool/PoolBuffer.java | 78 ++++++----- 2 files changed, 46 insertions(+), 184 deletions(-) diff --git a/src/java/org/apache/fulcrum/pool/BoundedBuffer.java b/src/java/org/apache/fulcrum/pool/BoundedBuffer.java deleted file mode 100644 index 47dcbf1..0000000 --- a/src/java/org/apache/fulcrum/pool/BoundedBuffer.java +++ /dev/null @@ -1,152 +0,0 @@ -package org.apache.fulcrum.pool; - - -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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. - */ - - -/** - * Efficient array-based bounded buffer class. - * Adapted from CPJ, chapter 8, which describes design. - * Originally written by Doug Lea and released into the public domain. - * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>] <p> - * - * @author <a href="mailto:[email protected]">Ilkka Priha</a> - * @version $Id$ - */ -public class BoundedBuffer -{ - /** - * The default capacity. - */ - public static final int DEFAULT_CAPACITY = 1024; - - protected final Object[] array_; // the elements - - protected int takePtr_ = 0; // circular indices - protected int putPtr_ = 0; - - protected int usedSlots_ = 0; // length - protected int emptySlots_; // capacity - length - - /** - * Creates a buffer with the given capacity. - * - * @param capacity the capacity. - * @throws IllegalArgumentException if capacity less or equal to zero. - */ - public BoundedBuffer(int capacity) - throws IllegalArgumentException - { - if (capacity <= 0) - throw new IllegalArgumentException(); - - array_ = new Object[capacity]; - emptySlots_ = capacity; - } - - /** - * Creates a buffer with the default capacity - */ - public BoundedBuffer() - { - this(DEFAULT_CAPACITY); - } - - /** - * Returns the number of elements in the buffer. - * This is only a snapshot value, that may change - * immediately after returning. - * - * @return the size. - */ - public synchronized int size() - { - return usedSlots_; - } - - /** - * Returns the capacity of the buffer. - * - * @return the capacity. - */ - public int capacity() - { - return array_.length; - } - - /** - * Peeks, but does not remove the top item from the buffer. - * - * @return the object or null. - */ - public synchronized Object peek() - { - if (usedSlots_ > 0) - return array_[takePtr_]; - else - return null; - } - - /** - * Puts an item in the buffer only if there is capacity available. - * - * @param x the item to be inserted. - * @return true if accepted, else false. - */ - public synchronized boolean offer(Object x) - { - if (x == null) - throw new IllegalArgumentException(); - - if (emptySlots_ > 0) - { - --emptySlots_; - array_[putPtr_] = x; - if (++putPtr_ >= array_.length) - putPtr_ = 0; - usedSlots_++; - return true; - } - else - return false; - } - - /** - * Polls and removes the top item from the buffer if one is available. - * - * @return the oldest item from the buffer, or null if the buffer is empty. - */ - public synchronized <T> T poll() - { - if (usedSlots_ > 0) - { - --usedSlots_; - @SuppressWarnings("unchecked") - T old = (T) array_[takePtr_]; - array_[takePtr_] = null; - if (++takePtr_ >= array_.length) - takePtr_ = 0; - emptySlots_++; - return old; - } - else - return null; - } -} diff --git a/src/java/org/apache/fulcrum/pool/PoolBuffer.java b/src/java/org/apache/fulcrum/pool/PoolBuffer.java index 4cb732c..08f1380 100644 --- a/src/java/org/apache/fulcrum/pool/PoolBuffer.java +++ b/src/java/org/apache/fulcrum/pool/PoolBuffer.java @@ -21,19 +21,20 @@ package org.apache.fulcrum.pool; import java.lang.reflect.Method; import java.util.ArrayList; +import java.util.concurrent.LinkedBlockingQueue; import org.apache.fulcrum.factory.FactoryService; /** * An inner class for class specific pools. */ -public class PoolBuffer +public class PoolBuffer { /** * A buffer for class instances. */ - private BoundedBuffer pool; + private LinkedBlockingQueue<Object> pool; /** * A flag to determine if a more efficient recycler is implemented. @@ -45,14 +46,20 @@ public class PoolBuffer */ private ArrayList<Recycler> recyclers; + /** + * The capacity initially set + */ + private int capacity; + /** * Constructs a new pool buffer with a specific capacity. * * @param capacity a capacity. */ - public PoolBuffer(int capacity) + public PoolBuffer(int capacity) { - pool = new BoundedBuffer(capacity); + this.pool = new LinkedBlockingQueue<>(capacity); + this.capacity = capacity; } /** @@ -61,42 +68,45 @@ public class PoolBuffer * * @param isArrayCtor a <code>boolean</code> value */ - public void setArrayCtorRecyclable(boolean isArrayCtor) + public void setArrayCtorRecyclable(boolean isArrayCtor) { arrayCtorRecyclable = isArrayCtor; } /** * Polls for an instance from the pool. - * - * + * + * * @param params object parameters * @param signature signature of the class * @param factoryService service to add * @throws PoolException if service failed to be found * @return an instance or null. */ - public <T> T poll(Object[] params, String[] signature, FactoryService factoryService) throws PoolException + public <T> T poll(Object[] params, String[] signature, FactoryService factoryService) throws PoolException { - T instance = pool.poll(); - if (instance != null) + @SuppressWarnings("unchecked") + T instance = (T) pool.poll(); + if (instance != null) { - if (arrayCtorRecyclable) + if (arrayCtorRecyclable) { ((ArrayCtorRecyclable) instance).recycle(params); - } else if (instance instanceof Recyclable) { - try + } + else if (instance instanceof Recyclable) + { + try { - if (signature != null && signature.length > 0) + if (signature != null && signature.length > 0) { /* Get the recycle method from the cache. */ Method recycle = getRecycle(signature); - if (recycle == null) + if (recycle == null) { synchronized (this) { /* Make a synchronized recheck. */ recycle = getRecycle(signature); - if (recycle == null) + if (recycle == null) { Class<? extends Object> clazz = instance.getClass(); recycle = clazz.getMethod("recycle", @@ -112,11 +122,13 @@ public class PoolBuffer } } recycle.invoke(instance, params); - } else { + } + else + { ((Recyclable) instance).recycle(); } - } - catch (Exception x) + } + catch (Exception x) { throw new PoolException("Recycling failed for " + instance.getClass().getName(), x); } @@ -127,19 +139,19 @@ public class PoolBuffer /** * Offers an instance to the pool. - * + * * @param instance an instance. * @return false if failed to dispose */ - public boolean offer(Object instance) + public boolean offer(Object instance) { - if (instance instanceof Recyclable) + if (instance instanceof Recyclable) { - try + try { ((Recyclable) instance).dispose(); - } - catch (Exception x) + } + catch (Exception x) { return false; } @@ -152,9 +164,9 @@ public class PoolBuffer * * @return the capacity. */ - public int capacity() + public int capacity() { - return pool.capacity(); + return capacity; } /** @@ -162,7 +174,7 @@ public class PoolBuffer * * @return the size. */ - public int size() + public int size() { return pool.size(); } @@ -173,17 +185,19 @@ public class PoolBuffer * @param signature the signature. * @return the recycle method or null. */ - private Method getRecycle(String[] signature) + private Method getRecycle(String[] signature) { ArrayList<Recycler> cache = recyclers; - if (cache != null) + if (cache != null) { Method recycle; - for (Recycler recycler : cache) + for (Recycler recycler : cache) { recycle = recycler.match(signature); if (recycle != null) - return recycle; + { + return recycle; + } } } return null;
