View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2006, 2009
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Endre Bak, ebak@de.ibm.com
12   * 
13   * Flag       Date        Prog         Description
14   * -------------------------------------------------------------------------------
15   * 1663270    2007-02-19  ebak         Minor performance problems
16   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
17   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
18   * 2531371    2009-02-10  raman_arora  Upgrade client to JDK 1.5 (Phase 2) 
19   */
20  
21  package org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax;
22  
23  /*-
24   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
25   * WBEM Java Client
26   * ჻჻჻჻჻჻
27   * Copyright 2023 - 2025 MetricsHub
28   * ჻჻჻჻჻჻
29   * Licensed under the Apache License, Version 2.0 (the "License");
30   * you may not use this file except in compliance with the License.
31   * You may obtain a copy of the License at
32   *
33   *      http://www.apache.org/licenses/LICENSE-2.0
34   *
35   * Unless required by applicable law or agreed to in writing, software
36   * distributed under the License is distributed on an "AS IS" BASIS,
37   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
38   * See the License for the specific language governing permissions and
39   * limitations under the License.
40   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
41   */
42  
43  import java.util.HashMap;
44  import org.metricshub.wbem.sblim.cimclient.internal.cimxml.sax.node.Node;
45  
46  /**
47   * NodePool is used by XMLDefaultHandlerImpl to store unused Node instances
48   * which can be reused later without instantiating new ones.
49   */
50  public class NodePool {
51  	private HashMap<String, PoolStack> iPoolMap = new HashMap<String, PoolStack>(512);
52  
53  	private int iHitCnt = 0;
54  
55  	private int iMissCnt = 0;
56  
57  	/**
58  	 * addNode
59  	 *
60  	 * @param pNode
61  	 */
62  	public void addNode(Node pNode) {
63  		PoolStack ps = this.iPoolMap.get(pNode.getNodeName());
64  		if (ps == null) {
65  			this.iPoolMap.put(pNode.getNodeName(), new PoolStack(pNode));
66  			return;
67  		}
68  		ps.put(pNode);
69  	}
70  
71  	/**
72  	 * getNode
73  	 *
74  	 * @param pNodeName
75  	 * @return Node
76  	 */
77  	public Node getNode(String pNodeName) {
78  		PoolStack ps = this.iPoolMap.get(pNodeName);
79  		if (ps == null) {
80  			++this.iMissCnt;
81  			return null;
82  		}
83  		Node node = ps.get();
84  		if (node == null) ++this.iMissCnt; else ++this.iHitCnt;
85  		return node;
86  	}
87  
88  	/**
89  	 * getHitCnt
90  	 *
91  	 * @return int
92  	 */
93  	public int getHitCnt() {
94  		return this.iHitCnt;
95  	}
96  
97  	/**
98  	 * getMissCnt
99  	 *
100 	 * @return int
101 	 */
102 	public int getMissCnt() {
103 		return this.iMissCnt;
104 	}
105 }
106 
107 class PoolStack {
108 	private static final int CAPACITY = 8, MAX_USECNT = CAPACITY - 1, MAX_IDX = MAX_USECNT;
109 
110 	private Node[] iNodeA = new Node[CAPACITY];
111 
112 	private int iIdx = 0, iUseCnt = 0;
113 
114 	/**
115 	 * Ctor.
116 	 *
117 	 * @param pNode
118 	 */
119 	public PoolStack(Node pNode) {
120 		put(pNode);
121 	}
122 
123 	/**
124 	 * put
125 	 *
126 	 * @param pNode
127 	 */
128 	public void put(Node pNode) {
129 		if (this.iUseCnt < MAX_USECNT) ++this.iUseCnt;
130 		this.iNodeA[this.iIdx] = pNode;
131 		incIdx();
132 	}
133 
134 	/**
135 	 * get
136 	 *
137 	 * @return Node
138 	 */
139 	public Node get() {
140 		if (this.iUseCnt == 0) return null;
141 		--this.iUseCnt;
142 		decIdx();
143 		return this.iNodeA[this.iIdx];
144 	}
145 
146 	private void decIdx() {
147 		this.iIdx = (this.iIdx == 0 ? MAX_IDX : this.iIdx - 1);
148 	}
149 
150 	private void incIdx() {
151 		this.iIdx = (this.iIdx == MAX_IDX ? 0 : this.iIdx + 1);
152 	}
153 }