diff options
author | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-03-05 04:45:24 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2020-03-05 04:45:24 +0000 |
commit | c47b5481de3d027efa0975ba7b722003b177b583 (patch) | |
tree | f8cbc802a806455c5fdfeab7f696c63641b9f9f5 /jimfs/src/main/java/com/google/common/jimfs/FileFactory.java | |
parent | 68591711a9034281d5fe11fc7a30e535bbce125c (diff) | |
parent | e50b3b756101b544da2b0d62041d91547ce14a00 (diff) | |
download | jimfs-c47b5481de3d027efa0975ba7b722003b177b583.tar.gz |
Initial merge with upstream am: cef92d673c am: c5f71e95df am: e50b3b7561
Change-Id: I9a53b224c320efb500d8a33b41798dd4df5fc78f
Diffstat (limited to 'jimfs/src/main/java/com/google/common/jimfs/FileFactory.java')
-rw-r--r-- | jimfs/src/main/java/com/google/common/jimfs/FileFactory.java | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/jimfs/src/main/java/com/google/common/jimfs/FileFactory.java b/jimfs/src/main/java/com/google/common/jimfs/FileFactory.java new file mode 100644 index 0000000..e26d41d --- /dev/null +++ b/jimfs/src/main/java/com/google/common/jimfs/FileFactory.java @@ -0,0 +1,121 @@ +/* + * Copyright 2013 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.common.jimfs; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Supplier; +import java.io.IOException; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * Factory for creating new files and copying files. One piece of the file store implementation. + * + * @author Colin Decker + */ +final class FileFactory { + + private final AtomicInteger idGenerator = new AtomicInteger(); + + private final HeapDisk disk; + + /** Creates a new file factory using the given disk for regular files. */ + public FileFactory(HeapDisk disk) { + this.disk = checkNotNull(disk); + } + + private int nextFileId() { + return idGenerator.getAndIncrement(); + } + + /** Creates a new directory. */ + public Directory createDirectory() { + return Directory.create(nextFileId()); + } + + /** Creates a new root directory with the given name. */ + public Directory createRootDirectory(Name name) { + return Directory.createRoot(nextFileId(), name); + } + + /** Creates a new regular file. */ + @VisibleForTesting + RegularFile createRegularFile() { + return RegularFile.create(nextFileId(), disk); + } + + /** Creates a new symbolic link referencing the given target path. */ + @VisibleForTesting + SymbolicLink createSymbolicLink(JimfsPath target) { + return SymbolicLink.create(nextFileId(), target); + } + + /** Creates and returns a copy of the given file. */ + public File copyWithoutContent(File file) throws IOException { + return file.copyWithoutContent(nextFileId()); + } + + // suppliers to act as file creation callbacks + + private final Supplier<Directory> directorySupplier = new DirectorySupplier(); + + private final Supplier<RegularFile> regularFileSupplier = new RegularFileSupplier(); + + /** Returns a supplier that creates directories. */ + public Supplier<Directory> directoryCreator() { + return directorySupplier; + } + + /** Returns a supplier that creates regular files. */ + public Supplier<RegularFile> regularFileCreator() { + return regularFileSupplier; + } + + /** Returns a supplier that creates a symbolic links to the given path. */ + public Supplier<SymbolicLink> symbolicLinkCreator(JimfsPath target) { + return new SymbolicLinkSupplier(target); + } + + private final class DirectorySupplier implements Supplier<Directory> { + @Override + public Directory get() { + return createDirectory(); + } + } + + private final class RegularFileSupplier implements Supplier<RegularFile> { + @Override + public RegularFile get() { + return createRegularFile(); + } + } + + private final class SymbolicLinkSupplier implements Supplier<SymbolicLink> { + + private final JimfsPath target; + + protected SymbolicLinkSupplier(JimfsPath target) { + this.target = checkNotNull(target); + } + + @Override + public SymbolicLink get() { + return createSymbolicLink(target); + } + } +} |