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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.cnd.modelimpl.trace;
041:
042: import java.util.ArrayList;
043: import java.util.Collection;
044: import java.util.HashMap;
045: import java.util.List;
046: import java.util.Map;
047:
048: /**
049: *
050: * @author Vladimir Voskresenky
051: */
052: public final class XRefResultSet {
053:
054: public static Collection<ContextScope> sortedContextScopes(
055: XRefResultSet bag, boolean byEntries) {
056: List<ContextScope> out = new ArrayList(
057: ContextScope.values().length);
058: for (ContextScope scope : ContextScope.values()) {
059: boolean added = false;
060: int scopeNum;
061: if (byEntries) {
062: scopeNum = bag.getEntries(scope).size();
063: } else {
064: scopeNum = bag.getNumberOfContexts(scope, false);
065: }
066: for (int i = 0; i < out.size(); i++) {
067: int curScopeNum;
068: ContextScope curScope = out.get(i);
069: if (byEntries) {
070: curScopeNum = bag.getEntries(curScope).size();
071: } else {
072: curScopeNum = bag.getNumberOfContexts(curScope,
073: false);
074: }
075: if (curScopeNum <= scopeNum) {
076: out.add(i, scope);
077: added = true;
078: break;
079: }
080: }
081: if (!added) {
082: out.add(scope);
083: }
084: }
085:
086: return out;
087: }
088:
089: private final Map<ContextScope, Collection<ContextEntry>> scopeEntries;
090: private final Map<ContextScope, Integer> scopes;
091:
092: public XRefResultSet() {
093: scopeEntries = new HashMap<ContextScope, Collection<ContextEntry>>(
094: ContextScope.values().length);
095: scopes = new HashMap<ContextScope, Integer>(ContextScope
096: .values().length);
097: for (ContextScope scopeContext : ContextScope.values()) {
098: scopeEntries.put(scopeContext, new ArrayList<ContextEntry>(
099: 1024));
100: scopes.put(scopeContext, new Integer(0));
101: }
102: }
103:
104: public final void addEntry(ContextScope contextScope,
105: ContextEntry entry) {
106: scopeEntries.get(contextScope).add(entry);
107: }
108:
109: public final Collection<ContextEntry> getEntries(
110: ContextScope contextScope) {
111: return scopeEntries.get(contextScope);
112: }
113:
114: public final void incrementScopeCounter(ContextScope contextScope) {
115: int val = scopes.get(contextScope);
116: scopes.put(contextScope, ++val);
117: }
118:
119: public final int getNumberOfAllContexts() {
120: int out = 0;
121: for (int val : scopes.values()) {
122: out += val;
123: }
124: return out;
125: }
126:
127: public final int getNumberOfContexts(ContextScope contextScope,
128: boolean relative) {
129: int num = scopes.get(contextScope);
130: if (relative && (num != 0)) {
131: assert num > 0;
132: num = (num * 100) / getNumberOfAllContexts();
133: }
134: return num;
135: }
136:
137: public enum ContextScope {
138: GLOBAL_FUNCTION, NAMESPACE_FUNCTION, FILE_LOCAL_FUNCTION, METHOD, CONSTRUCTOR, INLINED_METHOD, INLINED_CONSTRUCTOR, UNRESOLVED,
139: };
140:
141: public enum DeclarationKind {
142: CLASSIFIER, ENUMERATOR, VARIABLE, PARAMETER, FUNCTION, NAMESPACE, CLASS_FORWARD, MACRO, UNRESOLVED,
143: }
144:
145: public enum DeclarationScope {
146: FUNCTION_THIS, CLASSIFIER_THIS, CLASSIFIER_PARENT, PROJECT_CLASSIFIER, LIBRARY_CLASSIFIER, NAMESPACE_THIS, NAMESPACE_PARENT, PROJECT_NAMESPACE, LIBRARY_NAMESPACE, FILE_THIS, PROJECT_FILE, LIBRARY_FILE, PROJECT_GLOBAL, LIBRARY_GLOBAL, UNRESOLVED,
147: }
148:
149: public enum IncludeLevel {
150: THIS_FILE, PROJECT_DIRECT, LIBRARY_DIRECT, PROJECT_DEEP, LIBRARY_DEEP, UNRESOLVED,
151: }
152:
153: public enum UsageStatistics {
154: FIRST_USAGE, SECOND_USAGE, NEXT_USAGE, UNKNOWN,
155: }
156:
157: public static final class ContextEntry {
158: public final DeclarationKind declaration;
159: public final DeclarationScope declarationScope;
160: public final IncludeLevel declarationIncludeLevel;
161: public final UsageStatistics usageStatistics;
162:
163: public final static ContextEntry UNRESOLVED = new ContextEntry(
164: DeclarationKind.UNRESOLVED,
165: DeclarationScope.UNRESOLVED, IncludeLevel.UNRESOLVED,
166: UsageStatistics.UNKNOWN);
167:
168: public ContextEntry(DeclarationKind declaration,
169: DeclarationScope declarationScope,
170: IncludeLevel declarationIncludeLevel,
171: UsageStatistics usageStatistics) {
172: this.declaration = declaration;
173: this.declarationScope = declarationScope;
174: this.declarationIncludeLevel = declarationIncludeLevel;
175: this.usageStatistics = usageStatistics;
176: }
177: }
178:
179: }
|