copyright update
[fedora-idea.git] / platform-api / src / com / intellij / openapi / vfs / VirtualFileWrapper.java
blob710c2f2127f0b4ea413abb6dc3075dda1eca4749
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.openapi.vfs;
19 import org.jetbrains.annotations.NotNull;
20 import org.jetbrains.annotations.Nullable;
22 import java.io.File;
23 import java.io.IOException;
25 /**
26 * Utility class for work with both java.io.File and VirtualFile. It helps
27 * to create new VirtualFiles by java.io.Files. It is helpful for storing
28 * information about non-existing files with possibility to create VirtualFile
29 * on demand.
31 * @author Konstantin Bulenkov
32 * @see com.intellij.openapi.vfs.VirtualFile
33 * @since 9.0
35 public class VirtualFileWrapper {
36 private final File myFile;
38 /**
39 * Constructs wrapper based on java.io.File
41 * @param file java.io.File
43 public VirtualFileWrapper(@NotNull File file) {
44 myFile = file;
47 /**
48 * Tests whether the file passed to constructor exists.
50 * @return <code>true</code> if and only if the file passed to constructor
51 * exists; <code>false</code> otherwise
53 * @throws SecurityException
54 * If a security manager exists and its <code>{@link
55 * java.lang.SecurityManager#checkRead(java.lang.String)}</code>
56 * method denies read access to the file or directory
58 public boolean exists() {
59 return myFile.exists();
62 /**
63 * Refreshes LocalFileSystem and looks for VirtualFile
65 * @return VirtualFile or null if file is not exist
67 @Nullable
68 public VirtualFile getVirtualFile() {
69 return LocalFileSystem.getInstance().refreshAndFindFileByIoFile(myFile);
72 /**
73 * Looks for VirtualFile and tries to create new VirtualFile if
74 * <tt>createIfNotExist</tt> is <tt>true</tt> and file is not exist.
76 * @param createIfNotExist if <tt>true</tt> and java.io.File is not exist,
77 * VirtualFileWrapper will try to create new file
79 * @return virtual file
81 @Nullable
82 public VirtualFile getVirtualFile(boolean createIfNotExist) {
83 if (createIfNotExist && !myFile.exists()) {
84 try {
85 if (!myFile.createNewFile()) {
86 return null;
88 } catch (IOException e) {//
91 return getVirtualFile();
94 /**
95 * Returns original java.io.File
97 * @return original java.io.File
99 @NotNull
100 public File getFile() {
101 return myFile;