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.schema;
19
20 import java.lang.ref.WeakReference;
21
22 public class ContentIteratorCache {
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 ContentIteratorCache() {
41 this( initialCapacity );
42 }
43
44 public ContentIteratorCache( 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 System.out.println( "new capacity is " + newCapacity );
78 cache = new WeakReference[newCapacity];
79 System.arraycopy(oldData, 0, cache, 0, size);
80 }
81 }
82
83
84 public synchronized ContentIterator get() {
85 while ( size > 0 ) {
86 final WeakReference ref = cache[--size];
87 cache[size] = null;
88 final Object obj = ref.get();
89 if ( obj != null ) {
90 return (ContentIterator)obj;
91 }
92 }
93
94 return null;
95 }
96
97 /***
98 * Appends the specified element to the end of this list.
99 *
100 * @param o element to be appended to this list.
101 */
102 public synchronized void add( final ContentIterator iter ) {
103 ensureCapacity(size + 1);
104 cache[size++] = new WeakReference( iter );
105 //System.out.println( "cached iterator at pos " + size );
106 }
107
108 }
This page was automatically generated by Maven