KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > filesystem > provider > FileInfo


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Martin Oberhuber (Wind River) - [170317] add symbolic link support to API
11  *******************************************************************************/

12 package org.eclipse.core.filesystem.provider;
13
14 import org.eclipse.core.filesystem.EFS;
15 import org.eclipse.core.filesystem.IFileInfo;
16
17 /**
18  * This class should be used by file system providers in their implementation
19  * of API methods that return {@link IFileInfo} objects.
20  * <p>
21  * This class is not intended to be subclassed by clients.
22  * </p>
23  * @since org.eclipse.core.filesystem 1.0
24  */

25 public class FileInfo implements IFileInfo {
26     /**
27      * Internal attribute indicating if the file is a directory
28      */

29     private static final int ATTRIBUTE_DIRECTORY = 1 << 0;
30
31     /**
32      * Internal attribute indicating if the file exists.
33      */

34     private static final int ATTRIBUTE_EXISTS = 1 << 16;
35
36     /**
37      * Bit field of file attributes
38      */

39     private int attributes = 0;
40
41     /**
42      * The last modified time.
43      */

44     private long lastModified = EFS.NONE;
45
46     /**
47      * The file length.
48      */

49     private long length = EFS.NONE;
50
51     /**
52      * The file name.
53      */

54     private String JavaDoc name = ""; //$NON-NLS-1$
55

56     /**
57      * The target file name if this is a symbolic link
58      */

59     private String JavaDoc linkTarget = null;
60
61     /**
62      * Creates a new file information object with default values.
63      */

64     public FileInfo() {
65         super();
66     }
67
68     /**
69      * Creates a new file information object. All values except the file name
70      * will have default values.
71      *
72      * @param name The name of this file
73      */

74     public FileInfo(String JavaDoc name) {
75         super();
76         this.name = name;
77     }
78
79     /**
80      * Convenience method to clear a masked region of the attributes bit field.
81      *
82      * @param mask The mask to be cleared
83      */

84     private void clear(int mask) {
85         attributes &= ~mask;
86     }
87
88     /*
89      * (non-Javadoc)
90      * @see java.lang.Object#clone()
91      */

92     public Object JavaDoc clone() {
93         try {
94             return super.clone();
95         } catch (CloneNotSupportedException JavaDoc e) {
96             //we know this object is cloneable
97
return null;
98         }
99     }
100
101     /*
102      * (non-Javadoc)
103      * @see java.lang.Comparable#compareTo(java.lang.Object)
104      */

105     public int compareTo(Object JavaDoc o) {
106         return name.compareTo(((FileInfo) o).name);
107     }
108
109     /* (non-Javadoc)
110      * @see org.eclipse.core.filesystem.IFileInfo#exists()
111      */

112     public boolean exists() {
113         return getAttribute(ATTRIBUTE_EXISTS);
114     }
115
116     public boolean getAttribute(int attribute) {
117         return isSet(attribute);
118     }
119
120     /* (non-Javadoc)
121      * @see org.eclipse.core.filesystem.IFileInfo#getStringAttribute(int)
122      */

123     public String JavaDoc getStringAttribute(int attribute) {
124         if (attribute == EFS.ATTRIBUTE_LINK_TARGET)
125             return this.linkTarget;
126         return null;
127     }
128
129     /* (non-Javadoc)
130      * @see org.eclipse.core.filesystem.IFileInfo#lastModified()
131      */

132     public long getLastModified() {
133         return lastModified;
134     }
135
136     /* (non-Javadoc)
137      * @see org.eclipse.core.filesystem.IFileInfo#length()
138      */

139     public long getLength() {
140         return length;
141     }
142
143     /* (non-Javadoc)
144      * @see org.eclipse.core.filesystem.IFileInfo#getName()
145      */

146     public String JavaDoc getName() {
147         return name;
148     }
149
150     /* (non-Javadoc)
151      * @see org.eclipse.core.filesystem.IFileInfo#isDirectory()
152      */

153     public boolean isDirectory() {
154         return isSet(ATTRIBUTE_DIRECTORY);
155     }
156
157     private boolean isSet(long mask) {
158         return (attributes & mask) != 0;
159     }
160
161     private void set(int mask) {
162         attributes |= mask;
163     }
164
165     /* (non-Javadoc)
166      * @see org.eclipse.core.filesystem.IFileInfo#setAttribute(int, boolean)
167      */

168     public void setAttribute(int attribute, boolean value) {
169         if (value)
170             set(attribute);
171         else
172             clear(attribute);
173     }
174
175     /**
176      * Sets whether this is a file or directory.
177      *
178      * @param value <code>true</code> if this is a directory, and <code>false</code>
179      * if this is a file.
180      */

181     public void setDirectory(boolean value) {
182         if (value)
183             set(ATTRIBUTE_DIRECTORY);
184         else
185             clear(ATTRIBUTE_DIRECTORY);
186     }
187
188     /**
189      * Sets whether this file or directory exists.
190      *
191      * @param value <code>true</code> if this file exists, and <code>false</code>
192      * otherwise.
193      */

194     public void setExists(boolean value) {
195         if (value)
196             set(ATTRIBUTE_EXISTS);
197         else
198             clear(ATTRIBUTE_EXISTS);
199     }
200
201     /* (non-Javadoc)
202      * @see org.eclipse.core.filesystem.IFileInfo#setLastModified(long)
203      */

204     public void setLastModified(long value) {
205         lastModified = value;
206     }
207
208     /**
209      * Sets the length of this file. A value of {@link EFS#NONE}
210      * indicates the file does not exist, is a directory, or the length could not be computed.
211      *
212      * @param value the length of this file, or {@link EFS#NONE}
213      */

214     public void setLength(long value) {
215         this.length = value;
216     }
217
218     /**
219      * Sets the name of this file.
220      *
221      * @param name The file name
222      */

223     public void setName(String JavaDoc name) {
224         if (name == null)
225             throw new IllegalArgumentException JavaDoc();
226         this.name = name;
227     }
228
229     /**
230      * Sets or clears a String attribute, e.g. symbolic link target.
231      *
232      * @param attribute The kind of attribute to set. Currently only
233      * {@link EFS#ATTRIBUTE_LINK_TARGET} is supported.
234      * @param value The String attribute, or <code>null</code> to clear
235      * the attribute
236      * @since org.eclipse.core.filesystem 1.1
237      */

238     public void setStringAttribute(int attribute, String JavaDoc value) {
239         if (attribute == EFS.ATTRIBUTE_LINK_TARGET)
240             this.linkTarget = value;
241     }
242
243     /**
244      * For debugging purposes only.
245      */

246     public String JavaDoc toString() {
247         return name;
248     }
249 }
Popular Tags