View Javadoc
1 /* 2 * Copyright (C) 2002 Carsten Krebs (Team-Konzept GmbH & Co.KG) 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 package com.teamkonzept.dom4jb.saxon; 19 20 import java.lang.ref.WeakReference; 21 22 public class AxisEnumerationCache { 23 24 protected static final int initialCapacity = 4; 25 /*** 26 * The array buffer into which the elements of the NodeList are stored. 27 * The capacity of the ArrayList is the length of this array buffer. 28 */ 29 private WeakReference[] cache; 30 31 32 /*** 33 * The size of the Cache (the number of elements it contains). 34 * 35 * @serial 36 */ 37 private int size; 38 39 40 public AxisEnumerationCache() { 41 this( initialCapacity ); 42 } 43 44 public AxisEnumerationCache( final int initialCapacity ) { 45 this.cache = new WeakReference[initialCapacity]; 46 this.size = 0; 47 } 48 49 /*** 50 * Trims the capacity of this <tt>NodeList</tt> instance to be the 51 * list's current size. An application can use this operation to minimize 52 * the storage of an <tt>NodeList</tt> instance. 53 */ 54 public void trimToSize() { 55 final int oldCapacity = cache.length; 56 if (size < oldCapacity) { 57 final WeakReference[] oldData = cache; 58 cache = new WeakReference[size]; 59 System.arraycopy(oldData, 0, cache, 0, size); 60 } 61 } 62 63 /*** 64 * Increases the capacity of this <tt>NodeList</tt> instance, if 65 * necessary, to ensure that it can hold at least the number of elements 66 * specified by the minimum capacity argument. 67 * 68 * @param minCapacity the desired minimum capacity. 69 */ 70 public void ensureCapacity(final int minCapacity) { 71 final int oldCapacity = cache.length; 72 if (minCapacity > oldCapacity) { 73 final WeakReference oldData[] = cache; 74 int newCapacity = (oldCapacity * 3)/2 + 1; 75 if (newCapacity < minCapacity) 76 newCapacity = minCapacity; 77 cache = new WeakReference[newCapacity]; 78 System.arraycopy(oldData, 0, cache, 0, size); 79 } 80 } 81 82 83 public synchronized RecyclableAxisEnumeration get() { 84 while ( size > 0 ) { 85 final WeakReference ref = cache[--size]; 86 cache[size] = null; 87 final Object obj = ref.get(); 88 if ( obj != null ) { 89 return (RecyclableAxisEnumeration)obj; 90 } 91 } 92 93 return null; 94 } 95 96 /*** 97 * Appends the specified element to the end of this list. 98 * 99 * @param o element to be appended to this list. 100 */ 101 public synchronized void add( final RecyclableAxisEnumeration iter ) { 102 ensureCapacity(size + 1); 103 cache[size++] = new WeakReference( iter ); 104 } 105 106 } 107

This page was automatically generated by Maven