001: package org.apache.turbine.util.pool;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: /**
023: * Efficient array-based bounded buffer class.
024: * Adapted from CPJ, chapter 8, which describes design.
025: * Originally written by Doug Lea and released into the public domain.
026: * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>] <p>
027: *
028: * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a>
029: * @version $Id: BoundedBuffer.java 534527 2007-05-02 16:10:59Z tv $
030: */
031: public class BoundedBuffer {
032: /** The default capacity. */
033: public static final int DEFAULT_CAPACITY = 1024;
034:
035: protected final Object[] buffer; // the elements
036:
037: protected int takePtr = 0; // circular indices
038: protected int putPtr = 0;
039:
040: protected int usedSlots = 0; // length
041: protected int emptySlots; // capacity - length
042:
043: /**
044: * Creates a buffer with the given capacity.
045: *
046: * @param capacity the capacity.
047: * @throws IllegalArgumentException if capacity less or equal to zero.
048: */
049: public BoundedBuffer(int capacity) throws IllegalArgumentException {
050: if (capacity <= 0) {
051: throw new IllegalArgumentException(
052: "Bounded Buffer must have capacity > 0!");
053: }
054:
055: buffer = new Object[capacity];
056: emptySlots = capacity;
057: }
058:
059: /**
060: * Creates a buffer with the default capacity
061: */
062: public BoundedBuffer() {
063: this (DEFAULT_CAPACITY);
064: }
065:
066: /**
067: * Returns the number of elements in the buffer.
068: * This is only a snapshot value, that may change
069: * immediately after returning.
070: *
071: * @return the size.
072: */
073: public synchronized int size() {
074: return usedSlots;
075: }
076:
077: /**
078: * Returns the capacity of the buffer.
079: *
080: * @return the capacity.
081: */
082: public int capacity() {
083: return buffer.length;
084: }
085:
086: /**
087: * Peeks, but does not remove the top item from the buffer.
088: *
089: * @return the object or null.
090: */
091: public synchronized Object peek() {
092: return (usedSlots > 0) ? buffer[takePtr] : null;
093: }
094:
095: /**
096: * Puts an item in the buffer only if there is capacity available.
097: *
098: * @param item the item to be inserted.
099: * @return true if accepted, else false.
100: */
101: public synchronized boolean offer(Object x) {
102: if (x == null) {
103: throw new IllegalArgumentException(
104: "Bounded Buffer cannot store a null object");
105: }
106:
107: if (emptySlots > 0) {
108: --emptySlots;
109: buffer[putPtr] = x;
110: if (++putPtr >= buffer.length) {
111: putPtr = 0;
112: }
113: usedSlots++;
114: return true;
115: } else {
116: return false;
117: }
118: }
119:
120: /**
121: * Polls and removes the top item from the buffer if one is available.
122: *
123: * @return the oldest item from the buffer, or null if the buffer is empty.
124: */
125: public synchronized Object poll() {
126: if (usedSlots > 0) {
127: --usedSlots;
128: Object old = buffer[takePtr];
129: buffer[takePtr] = null;
130: if (++takePtr >= buffer.length) {
131: takePtr = 0;
132: }
133: emptySlots++;
134: return old;
135: } else {
136: return null;
137: }
138: }
139: }
|