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.cache.impl;
043:
044: import java.io.IOException;
045: import java.io.ObjectInputStream;
046: import java.io.ObjectOutputStream;
047: import java.io.Serializable;
048: import org.netbeans.modules.cnd.api.model.CsmFile;
049: import org.netbeans.modules.cnd.utils.cache.FilePathCache;
050:
051: /**
052: * cache index support for project cache manager
053: *
054: * @author Vladimir Voskresensky
055: */
056: final class ProjectIndex extends AbstractCacheIndex implements
057: Serializable {
058: private static final long serialVersionUID = -7790789617759717723L;
059:
060: // map contains absolute path of file and entry (cacheName,lastModified)
061:
062: public Entry getFileEntry(CsmFile file) {
063: Entry value = (Entry) super .get(file);
064: return value;
065: }
066:
067: public Entry putFile(CsmFile file) {
068: Entry value = (Entry) super .put(file);
069: return value;
070: }
071:
072: public void invalidateFile(String absPath) {
073: Entry value = (Entry) super .get(absPath);
074: if (value != null) {
075: value.setLastModified(0);
076: }
077: }
078:
079: ////////////////////////////////////////////////////////////////////////////
080: // save/load implmentation
081:
082: @Override
083: protected void loadData(ObjectInputStream ois) throws IOException,
084: ClassNotFoundException {
085: int version = ois.readInt();
086: if (version >= 1) {
087: // version 1
088: // - load base data
089: super .loadData(ois);
090: }
091: }
092:
093: @Override
094: protected void saveData(ObjectOutputStream oos) throws IOException {
095: int version = 1;
096: oos.writeInt(version);
097:
098: // version 1
099: // - save base data
100: super .saveData(oos);
101: }
102:
103: ////////////////////////////////////////////////////////////////////////////
104: // index map content support
105:
106: protected CharSequence getIndexKey(Object obj) {
107: // use absolute path os the key
108: CharSequence key;
109: if (obj instanceof String) {
110: key = (String) obj;
111: } else {
112: CsmFile file = (CsmFile) obj;
113: key = file.getAbsolutePath();
114: }
115: return FilePathCache.getString(key);
116: }
117:
118: protected CharSequence getBaseCacheName(Object obj) {
119: // use name for the file
120: CsmFile file = (CsmFile) obj;
121: CharSequence base = file.getName();
122: return base;
123: }
124:
125: protected Object createValue(CharSequence cacheName,
126: Object obj2cache) {
127: return new Entry(cacheName, 0/*((FileImpl)obj2cache).getBuffer().getFile().lastModified()*/);
128: }
129:
130: protected boolean isEqual(Object value, CharSequence checkCacheName) {
131: return ((Entry) value).getCacheFileName()
132: .equals(checkCacheName);
133: }
134:
135: ////////////////////////////////////////////////////////////////////////////
136: // value entry
137: final static class Entry implements Serializable {
138: private static final long serialVersionUID = -7790789617759717723L;
139:
140: transient private CharSequence cacheFileName;
141: transient private long lastModified;
142:
143: private Entry(CharSequence name, long modified) {
144: setCacheFileName(name);
145: setLastModified(modified);
146: }
147:
148: public CharSequence getCacheFileName() {
149: return cacheFileName;
150: }
151:
152: public void setCacheFileName(CharSequence cacheFileName) {
153: this .cacheFileName = cacheFileName;
154: }
155:
156: public long getLastModified() {
157: return lastModified;
158: }
159:
160: public void setLastModified(long lastModified) {
161: this .lastModified = lastModified;
162: }
163:
164: @Override
165: public String toString() {
166: String retValue;
167:
168: retValue = "[" + cacheFileName + ";" + lastModified + "]"; // NOI18N
169: return retValue;
170: }
171:
172: private void writeObject(java.io.ObjectOutputStream out)
173: throws IOException {
174: out.defaultWriteObject();
175: out.writeObject(cacheFileName);
176: out.writeLong(lastModified);
177: }
178:
179: private void readObject(java.io.ObjectInputStream in)
180: throws IOException, ClassNotFoundException {
181: in.defaultReadObject();
182: cacheFileName = (String) in.readObject();
183: lastModified = in.readLong();
184: }
185: }
186: }
|