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: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.cnd.modelimpl.csm.core;
043:
044: import java.io.DataInput;
045: import java.io.DataOutput;
046: import java.io.IOException;
047: import java.util.Map;
048: import java.util.concurrent.ConcurrentHashMap;
049: import org.netbeans.modules.cnd.api.model.CsmClassifier;
050: import org.netbeans.modules.cnd.api.model.CsmDeclaration;
051: import org.netbeans.modules.cnd.api.model.CsmUID;
052: import org.netbeans.modules.cnd.api.model.util.CsmKindUtilities;
053: import org.netbeans.modules.cnd.modelimpl.textcache.QualifiedNameCache;
054: import org.netbeans.modules.cnd.modelimpl.uid.UIDCsmConverter;
055: import org.netbeans.modules.cnd.modelimpl.uid.UIDObjectFactory;
056: import org.netbeans.modules.cnd.repository.spi.Persistent;
057: import org.netbeans.modules.cnd.repository.support.SelfPersistent;
058: import org.netbeans.modules.cnd.utils.cache.CharSequenceKey;
059:
060: /**
061: * Storage for project classifiers. Class was extracted from ProjectBase.
062: * @author Alexander Simon
063: */
064: /*package-local*/class ClassifierContainer implements Persistent,
065: SelfPersistent {
066:
067: private Map<CharSequence, CsmUID<CsmClassifier>> classifiers = new ConcurrentHashMap<CharSequence, CsmUID<CsmClassifier>>();
068: private Map<CharSequence, CsmUID<CsmClassifier>> typedefs = new ConcurrentHashMap<CharSequence, CsmUID<CsmClassifier>>();
069:
070: /** Creates a new instance of ClassifierContainer */
071: public ClassifierContainer() {
072: }
073:
074: public ClassifierContainer(DataInput input) throws IOException {
075: read(input);
076: }
077:
078: public CsmClassifier getClassifier(CharSequence qualifiedName) {
079: CsmClassifier result;
080: qualifiedName = CharSequenceKey.create(qualifiedName);
081: CsmUID<CsmClassifier> uid = classifiers.get(qualifiedName);
082: if (uid == null) {
083: uid = typedefs.get(qualifiedName);
084: }
085: result = UIDCsmConverter.UIDtoDeclaration(uid);
086: return result;
087: }
088:
089: public boolean putClassifier(CsmClassifier decl) {
090: CharSequence qn = decl.getQualifiedName();
091: Map<CharSequence, CsmUID<CsmClassifier>> map;
092: if (isTypedef(decl)) {
093: map = typedefs;
094: } else {
095: map = classifiers;
096: }
097: if (!map.containsKey(qn)) {
098: CsmUID<CsmClassifier> uid = UIDCsmConverter
099: .declarationToUID(decl);
100: assert uid != null;
101: map.put(qn, uid);
102: assert (UIDCsmConverter.UIDtoDeclaration(uid) != null);
103: return true;
104: }
105: return false;
106: }
107:
108: public void removeClassifier(CsmDeclaration decl) {
109: Map<CharSequence, CsmUID<CsmClassifier>> map;
110: if (isTypedef(decl)) {
111: map = typedefs;
112: } else {
113: map = classifiers;
114: }
115: CsmUID<CsmClassifier> uid = map.remove(decl.getQualifiedName());
116: assert (uid == null)
117: || (UIDCsmConverter.UIDtoCsmObject(uid) != null) : " no object for UID "
118: + uid;
119: }
120:
121: public void clearClassifiers() {
122: classifiers.clear();
123: typedefs.clear();
124: }
125:
126: private boolean isTypedef(CsmDeclaration decl) {
127: return CsmKindUtilities.isTypedef(decl);
128: }
129:
130: public void write(DataOutput output) throws IOException {
131: UIDObjectFactory.getDefaultFactory().writeStringToUIDMap(
132: this .classifiers, output, false);
133: UIDObjectFactory.getDefaultFactory().writeStringToUIDMap(
134: this .typedefs, output, false);
135: }
136:
137: private void read(DataInput input) throws IOException {
138: UIDObjectFactory.getDefaultFactory().readStringToUIDMap(
139: this.classifiers, input,
140: QualifiedNameCache.getManager());
141: UIDObjectFactory.getDefaultFactory().readStringToUIDMap(
142: this.typedefs, input, QualifiedNameCache.getManager());
143: }
144: }
|