Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 55799e2486ba0ded2d7961e0b1f4260f3094cecd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*******************************************************************************
 * Copyright (c) 2003, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 * IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.common.internal.emf.resource;


import java.util.ArrayList;
import java.util.Collection;

import org.xml.sax.Attributes;

/**
 * Provides an instance pool of reusable CacheEventNodes. The pool will default to 10 live
 * instances. When its available instances reach five times its initial capacity, it will shrink
 * itself down to the initial capacity.
 * 
 * @author mdelder
 */
public class CacheEventPool {

	public static final int DEFAULT_CAPACITY = 10;
	public static final int DEFAULT_CAPACITY_INCREMENT = 25;

	private int poolCapacity = DEFAULT_CAPACITY;
	private CacheEventStack availablePool = null;
	private Collection inusePool = null;

	public CacheEventPool() {
		this(DEFAULT_CAPACITY);
	}

	/**
	 * Create a CacheEventPOol with the given initial capacity
	 * 
	 * @param initialCapacity
	 *            The number of available instances to create.
	 */
	public CacheEventPool(int initialCapacity) {
		poolCapacity = (initialCapacity > 0) ? initialCapacity : DEFAULT_CAPACITY;
		availablePool = new CacheEventStack();
		inusePool = new ArrayList(poolCapacity);
	}

	/**
	 * Create a CacheEventNode (CENO) initialized to use the given resource as its EMF Owner.
	 * 
	 * THIS METHOD SHOULD ONLY BE USED TO CREATE ROOT NODES.
	 * 
	 * @param resource
	 *            the resource that will be populated
	 * @return a CacheEventNode to serve as the root.
	 */
	public CacheEventNode createCacheEventNode(TranslatorResource resource) {
		CacheEventNode adapter = fetchFreeNode();
		adapter.init(resource);
		return adapter;
	}

	/**
	 * Create child CacheEventNodes (CENOs) that will branch from the given parent.
	 * 
	 * @param parent
	 *            the containing CENO
	 * @param nodeName
	 *            The value of the XML element node name
	 * @param attributes
	 *            The attributes that were part of the given XML element
	 * @return A CENO that has been properly initialized.
	 */
	public CacheEventNode createCacheEventNode(CacheEventNode parent, String nodeName, Attributes attributes) {
		CacheEventNode adapter = fetchFreeNode();
		adapter.init(parent, nodeName, attributes);
		return adapter;
	}

	/**
	 * Release the CacheEventNode CENO back to the pool of availabe instances. This method should
	 * not be invoked directly. CENOs which are acquired from a given pool will automatically
	 * release themselves when necessary.
	 * 
	 * @param adapter
	 */
	public void releaseNode(CacheEventNode adapter) {
		freeNode(adapter);
	}

	/**
	 * freezePool() should be invoked to free any unused resources. After freezePool has been
	 * invoked, warmPool() will need to be invoked before the pool can be used again.
	 *  
	 */
	public void freezePool() {
		availablePool.clear();
		availablePool = null;
	}

	/**
	 * warmPool() must be invoked to notify the pool it is about to be used. This should occur only
	 * once per document rendering. Until the pool is in use, it contains no available
	 * CacheEventNodes (CENOs) in order to limit the size of the in-memory footprint of the
	 * EMF2SAXWriter.
	 *  
	 */
	public void warmPool() {
		ensureMinimumCapacity();
	}

	private CacheEventNode fetchFreeNode() {
		CacheEventNode result = null;

		if (availablePool == null || availablePool.isEmpty())
			warmPool();

		result = availablePool.pop();
		inusePool.add(result);

		return result;
	}

	private void freeNode(CacheEventNode adapter) {
		if (inusePool.remove(adapter))
			availablePool.push(adapter);
		//else
		//	throw new IllegalStateException("Adapter not contained in pool!");
		if (availablePool.size() > (5 * poolCapacity)) {
			availablePool.clear();
			ensureMinimumCapacity();
		}
	}

	private void ensureMinimumCapacity() {
		if (availablePool == null) {
			availablePool = new CacheEventStack();
		}
		if (availablePool.size() < poolCapacity) {
			final int minimumCapacity = poolCapacity - availablePool.size();
			for (int i = 0; i < minimumCapacity; i++)
				availablePool.push(new CacheEventNode(this));
		}
	}

}

Back to the top