001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.results.cpu;
042:
043: import java.util.ArrayList;
044: import java.util.Hashtable;
045: import java.util.ResourceBundle;
046:
047: /**
048: * This class provides a map between method ids and class (package) ids, which is needed when
049: * constructing an aggregated class- (package-) level view of CPU profiling results out of the
050: * initial method-level view
051: *
052: * @author Misha Dmitriev
053: */
054: public class MethodIdMap {
055: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
056:
057: // -----
058: // I18N String constants
059: private static final ResourceBundle messages = ResourceBundle
060: .getBundle("org.netbeans.lib.profiler.results.cpu.Bundle"); // NOI18N
061: private static final String ANONYMOUS_PACKAGE_STRING = messages
062: .getString("MethodIdMap_AnonymousPackageString"); // NOI18N
063: // -----
064:
065: //~ Instance fields ----------------------------------------------------------------------------------------------------------
066:
067: private ArrayList classOrPackageNames;
068: private Hashtable classIdCache; // Maps a class (package) name to its integer id
069: private int[] classIds;
070: private int curClassId;
071: private int newView;
072:
073: //~ Constructors -------------------------------------------------------------------------------------------------------------
074:
075: /**
076: * @param methodLevelInstrClassNames names of classes for instrumented methods. The total number of entries is
077: * equal to the number of instrumented methods, but some entries may be the same of
078: * course.
079: * @param nInstrMethods number of entries in this array that are actually used
080: * @param newView the new view for which we are creeating ids - class-level or package-level
081: */
082: public MethodIdMap(String[] methodLevelInstrClassNames,
083: int nInstrMethods, int newView) {
084: this .newView = newView;
085: classIds = new int[nInstrMethods];
086: classIdCache = new Hashtable();
087: classOrPackageNames = new ArrayList();
088: curClassId = 0;
089: classOrPackageNames.add(methodLevelInstrClassNames[0]);
090:
091: classIds[0] = 0; // The hidden "Thread" quazi-method transforms into "Thread" quazi-class
092:
093: for (int i = 1; i < nInstrMethods; i++) {
094: classIds[i] = getClassId(methodLevelInstrClassNames[i]);
095: }
096:
097: classIdCache = null; // Not needed anymore - free memory
098: }
099:
100: //~ Methods ------------------------------------------------------------------------------------------------------------------
101:
102: public int getClassOrPackageIdForMethodId(int methodId) {
103: return classIds[methodId];
104: }
105:
106: public String[] getInstrClassesOrPackages() {
107: String[] ret = (String[]) classOrPackageNames
108: .toArray(new String[classOrPackageNames.size()]);
109: classOrPackageNames = null;
110:
111: return ret;
112: }
113:
114: public int getNInstrClassesOrPackages() {
115: return curClassId + 1;
116: }
117:
118: private int getClassId(String className) {
119: String name = (newView == CPUResultsSnapshot.CLASS_LEVEL_VIEW) ? className
120: : getPackageName(className);
121: Integer classId = (Integer) classIdCache.get(name);
122:
123: if (classId == null) {
124: curClassId++;
125: classOrPackageNames.add(name);
126: classIdCache.put(name, new Integer(curClassId));
127:
128: return curClassId;
129: } else {
130: return classId.intValue();
131: }
132: }
133:
134: private String getPackageName(String className) {
135: int lastDivPos = className.lastIndexOf('.'); // NOI18N
136:
137: if (lastDivPos == -1) {
138: return ANONYMOUS_PACKAGE_STRING;
139: } else {
140: return className.substring(0, lastDivPos).intern();
141: }
142: }
143: }
|