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.uid;
043:
044: import org.netbeans.modules.cnd.api.model.CsmUID;
045: import org.netbeans.modules.cnd.utils.cache.WeakSharedSet;
046:
047: /**
048: *
049: * @author Vladimir Voskresensky
050: */
051: public class UIDManager {
052: private final UIDStorage storage;
053: private static final int UID_MANAGER_DEFAULT_CAPACITY = 1024;
054: private static final int UID_MANAGER_DEFAULT_SLICED_NUMBER = 29;
055:
056: private static final UIDManager instance = new UIDManager();
057:
058: /** Creates a new instance of UIDManager */
059: private UIDManager() {
060: storage = new UIDStorage(UID_MANAGER_DEFAULT_SLICED_NUMBER,
061: UID_MANAGER_DEFAULT_CAPACITY);
062: }
063:
064: public static UIDManager instance() {
065: return instance;
066: }
067:
068: // we need exclusive copy of string => use "new String(String)" constructor
069: private final String lock = new String("lock in UIDManager"); // NOI18N
070:
071: /**
072: * returns shared uid instance equal to input one.
073: *
074: * @param uid - interested shared uid
075: * @return the shared instance of uid
076: * @exception NullPointerException If the <code>uid</code> parameter
077: * is <code>null</code>.
078: */
079: public final CsmUID getSharedUID(CsmUID uid) {
080: if (uid == null) {
081: throw new NullPointerException(
082: "null string is illegal to share"); // NOI18N
083: }
084: CsmUID outUID = null;
085: synchronized (lock) {
086: outUID = storage.getSharedUID(uid);
087: }
088: assert (outUID != null);
089: assert (outUID.equals(uid));
090: return outUID;
091: }
092:
093: public final void dispose() {
094: storage.dispose();
095: }
096:
097: private static final class UIDStorage {
098: private final WeakSharedSet<CsmUID>[] instances;
099: private final int sliceNumber; // primary number for better distribution
100: private final int initialCapacity;
101:
102: private UIDStorage(int sliceNumber, int initialCapacity) {
103: this .sliceNumber = sliceNumber;
104: this .initialCapacity = initialCapacity;
105: instances = new WeakSharedSet[sliceNumber];
106: for (int i = 0; i < instances.length; i++) {
107: instances[i] = new WeakSharedSet<CsmUID>(
108: initialCapacity);
109: }
110: }
111:
112: private WeakSharedSet<CsmUID> getDelegate(CsmUID uid) {
113: int index = uid.hashCode() % sliceNumber;
114: if (index < 0) {
115: index += sliceNumber;
116: }
117: return instances[index];
118: }
119:
120: public final CsmUID getSharedUID(CsmUID uid) {
121: return getDelegate(uid).addOrGet(uid);
122: }
123:
124: public final void dispose() {
125: for (int i = 0; i < instances.length; i++) {
126: if (instances[i].size() > 0) {
127: instances[i].clear();
128: instances[i].resize(initialCapacity);
129: }
130: }
131: }
132: }
133: }
|