KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > turbo > keys > DiskFileKey


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.turbo.keys;
21
22 import org.openide.filesystems.FileObject;
23 import org.openide.filesystems.FileSystem;
24 import org.openide.filesystems.FileUtil;
25 import org.openide.filesystems.FileStateInvalidException;
26 import org.openide.ErrorManager;
27
28 import java.io.File JavaDoc;
29
30 /**
31  * Key for FileObject with identity given by disk files.
32  * It means that keys can be equal for non-equal FileObjects.
33  *
34  * @author Petr Kuzel
35  */

36 public final class DiskFileKey {
37     private final FileObject fileObject;
38     private final int hashCode;
39     private String JavaDoc absolutePath;
40
41
42     public static DiskFileKey createKey(FileObject fo) {
43         return new DiskFileKey(fo);
44     }
45
46     private DiskFileKey(FileObject fo) {
47
48         // PERFORMANCE optimalization, it saves memory because elimintes nedd for creating absolute paths.
49
// XXX unwrap from MasterFileSystem, hidden dependency on "VCS-Native-FileObject" attribute knowledge
50
// Unfortunately MasterFileSystem API does not support generic unwrapping.
51
FileObject nativeFileObject = (FileObject) fo.getAttribute("VCS-Native-FileObject"); // NOI18N
52
if (nativeFileObject == null) nativeFileObject = fo;
53
54
55         fileObject = fo;
56         hashCode = fo.getNameExt().hashCode();
57     }
58
59     public boolean equals(Object JavaDoc o) {
60         if (this == o) return true;
61
62         if (o instanceof DiskFileKey) {
63
64             DiskFileKey key = (DiskFileKey) o;
65
66             if (hashCode != key.hashCode) return false;
67             FileObject fo2 = key.fileObject;
68             FileObject fo = fileObject;
69
70             if (fo == fo2) return true;
71
72             try {
73                 FileSystem fs = fo.getFileSystem();
74                 FileSystem fs2 = fo2.getFileSystem();
75                 if (fs.equals(fs2)) {
76                     return fo.equals(fo2);
77                 } else {
78                     // fallback use absolute paths (cache them)
79
if (absolutePath == null) {
80                         File JavaDoc f = FileUtil.toFile(fo);
81                         absolutePath = f.getAbsolutePath();
82                     }
83                     if (key.absolutePath == null) {
84                         File JavaDoc f2 = FileUtil.toFile(fo2);
85                         key.absolutePath = f2.getAbsolutePath();
86                     }
87                     return absolutePath.equals(key.absolutePath);
88                 }
89             } catch (FileStateInvalidException e) {
90                 ErrorManager err = ErrorManager.getDefault();
91                 err.notify(e);
92             }
93         }
94         return false;
95     }
96
97     public int hashCode() {
98         return hashCode;
99     }
100
101     public String JavaDoc toString() {
102         if (absolutePath != null) {
103             return absolutePath;
104         }
105         return fileObject.toString();
106     }
107 }
108
Popular Tags