View Javadoc
1 package org.apache.turbine.util.pool; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 /*** 58 * Efficient array-based bounded buffer class. 59 * Adapted from CPJ, chapter 8, which describes design. 60 * Originally written by Doug Lea and released into the public domain. 61 * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>] <p> 62 * 63 * @author <a href="mailto:ilkka.priha@simsoft.fi">Ilkka Priha</a> 64 * @version $Id: BoundedBuffer.java,v 1.1.1.1 2001/08/16 05:09:59 jvanzyl Exp $ 65 */ 66 public class BoundedBuffer 67 { 68 /*** 69 * The default capacity. 70 */ 71 public static final int DEFAULT_CAPACITY = 1024; 72 73 protected final Object[] array_; // the elements 74 75 protected int takePtr_ = 0; // circular indices 76 protected int putPtr_ = 0; 77 78 protected int usedSlots_ = 0; // length 79 protected int emptySlots_; // capacity - length 80 81 /*** 82 * Creates a buffer with the given capacity. 83 * 84 * @param capacity the capacity. 85 * @throws IllegalArgumentException if capacity less or equal to zero. 86 */ 87 public BoundedBuffer(int capacity) 88 throws IllegalArgumentException 89 { 90 if (capacity <= 0) 91 throw new IllegalArgumentException(); 92 93 array_ = new Object[capacity]; 94 emptySlots_ = capacity; 95 } 96 97 /*** 98 * Creates a buffer with the default capacity 99 */ 100 public BoundedBuffer() 101 { 102 this(DEFAULT_CAPACITY); 103 } 104 105 /*** 106 * Returns the number of elements in the buffer. 107 * This is only a snapshot value, that may change 108 * immediately after returning. 109 * 110 * @return the size. 111 */ 112 public synchronized int size() 113 { 114 return usedSlots_; 115 } 116 117 /*** 118 * Returns the capacity of the buffer. 119 * 120 * @return the capacity. 121 */ 122 public int capacity() 123 { 124 return array_.length; 125 } 126 127 /*** 128 * Peeks, but does not remove the top item from the buffer. 129 * 130 * @return the object or null. 131 */ 132 public synchronized Object peek() 133 { 134 if (usedSlots_ > 0) 135 return array_[takePtr_]; 136 else 137 return null; 138 } 139 140 /*** 141 * Puts an item in the buffer only if there is capacity available. 142 * 143 * @param item the item to be inserted. 144 * @return true if accepted, else false. 145 */ 146 public synchronized boolean offer(Object x) 147 { 148 if (x == null) 149 throw new IllegalArgumentException(); 150 151 if (emptySlots_ > 0) 152 { 153 --emptySlots_; 154 array_[putPtr_] = x; 155 if (++putPtr_ >= array_.length) 156 putPtr_ = 0; 157 usedSlots_++; 158 return true; 159 } 160 else 161 return false; 162 } 163 164 /*** 165 * Polls and removes the top item from the buffer if one is available. 166 * 167 * @return the oldest item from the buffer, or null if the buffer is empty. 168 */ 169 public synchronized Object poll() 170 { 171 if (usedSlots_ > 0) 172 { 173 --usedSlots_; 174 Object old = array_[takePtr_]; 175 array_[takePtr_] = null; 176 if (++takePtr_ >= array_.length) 177 takePtr_ = 0; 178 emptySlots_++; 179 return old; 180 } 181 else 182 return null; 183 } 184 }

This page was automatically generated by Maven