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-2006 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.turbo.keys;
043:
044: import org.openide.filesystems.FileObject;
045: import org.openide.filesystems.FileSystem;
046: import org.openide.filesystems.FileUtil;
047: import org.openide.filesystems.FileStateInvalidException;
048: import org.openide.ErrorManager;
049:
050: import java.io.File;
051:
052: /**
053: * Key for FileObject with identity given by disk files.
054: * It means that keys can be equal for non-equal FileObjects.
055: *
056: * @author Petr Kuzel
057: */
058: public final class DiskFileKey {
059: private final FileObject fileObject;
060: private final int hashCode;
061: private String absolutePath;
062:
063: public static DiskFileKey createKey(FileObject fo) {
064: return new DiskFileKey(fo);
065: }
066:
067: private DiskFileKey(FileObject fo) {
068:
069: // PERFORMANCE optimalization, it saves memory because elimintes nedd for creating absolute paths.
070: // XXX unwrap from MasterFileSystem, hidden dependency on "VCS-Native-FileObject" attribute knowledge
071: // Unfortunately MasterFileSystem API does not support generic unwrapping.
072: FileObject nativeFileObject = (FileObject) fo
073: .getAttribute("VCS-Native-FileObject"); // NOI18N
074: if (nativeFileObject == null)
075: nativeFileObject = fo;
076:
077: fileObject = fo;
078: hashCode = fo.getNameExt().hashCode();
079: }
080:
081: public boolean equals(Object o) {
082: if (this == o)
083: return true;
084:
085: if (o instanceof DiskFileKey) {
086:
087: DiskFileKey key = (DiskFileKey) o;
088:
089: if (hashCode != key.hashCode)
090: return false;
091: FileObject fo2 = key.fileObject;
092: FileObject fo = fileObject;
093:
094: if (fo == fo2)
095: return true;
096:
097: try {
098: FileSystem fs = fo.getFileSystem();
099: FileSystem fs2 = fo2.getFileSystem();
100: if (fs.equals(fs2)) {
101: return fo.equals(fo2);
102: } else {
103: // fallback use absolute paths (cache them)
104: if (absolutePath == null) {
105: File f = FileUtil.toFile(fo);
106: absolutePath = f.getAbsolutePath();
107: }
108: if (key.absolutePath == null) {
109: File f2 = FileUtil.toFile(fo2);
110: key.absolutePath = f2.getAbsolutePath();
111: }
112: return absolutePath.equals(key.absolutePath);
113: }
114: } catch (FileStateInvalidException e) {
115: ErrorManager err = ErrorManager.getDefault();
116: err.notify(e);
117: }
118: }
119: return false;
120: }
121:
122: public int hashCode() {
123: return hashCode;
124: }
125:
126: public String toString() {
127: if (absolutePath != null) {
128: return absolutePath;
129: }
130: return fileObject.toString();
131: }
132: }
|