aboutsummaryrefslogtreecommitdiff
path: root/jimfs/src/main/java/com/google/common/jimfs/FileFactory.java
blob: e26d41dec1de12e2691d216597f2bc3bc97ec5cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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);
    }
  }
}