summaryrefslogtreecommitdiffstats
path: root/sched/src/com/android/sched/vfs/direct/DirectDir.java
blob: c097cd5735d8c57e1737d4145e92f6242afffb60 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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.android.sched.vfs.direct;

import com.android.sched.util.ConcurrentIOException;
import com.android.sched.util.file.CannotCreateFileException;
import com.android.sched.util.file.Directory;
import com.android.sched.util.file.NotFileOrDirectoryException;
import com.android.sched.util.location.DirectoryLocation;
import com.android.sched.util.location.FileLocation;
import com.android.sched.util.location.Location;
import com.android.sched.vfs.AbstractVElement;
import com.android.sched.vfs.InputOutputVDir;
import com.android.sched.vfs.InputRootVDir;
import com.android.sched.vfs.InputVElement;
import com.android.sched.vfs.InputVFile;
import com.android.sched.vfs.OutputVFile;
import com.android.sched.vfs.VPath;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

/**
 * Directory in the file system.
 */
public class DirectDir extends AbstractVElement implements InputRootVDir, InputOutputVDir {

  @Nonnull
  private final File dir;
  @CheckForNull
  private ArrayList<InputVElement> list;
  @Nonnull
  private final Location location;

  public DirectDir(@Nonnull Directory directory) {
    dir = directory.getFile();
    location = directory.getLocation();
  }

  public DirectDir(@Nonnull File dir) throws NotFileOrDirectoryException {
    if (!dir.isDirectory()) {
      throw new NotFileOrDirectoryException(new DirectoryLocation(dir));
    }
    this.dir = dir;
    location = new FileLocation(dir);
  }

  @Nonnull
  @Override
  public String getName() {
    return dir.getName();
  }

  @Nonnull
  @Override
  public synchronized Collection<? extends InputVElement> list() {
    if (list == null) {
      File[] subs = dir.listFiles();
      if (subs == null) {
        throw new ConcurrentIOException(new ListDirException(dir));
      }
      if (subs.length == 0) {
        return Collections.emptyList();
      }

      list = new ArrayList<InputVElement>(subs.length);
      for (File sub : subs) {
        try {
          if (sub.isFile()) {
            list.add(new DirectFile(sub));
          } else {
            list.add(new DirectDir(sub));
          }
        } catch (NotFileOrDirectoryException e) {
          throw new ConcurrentIOException(e);
        }
      }
    }

    assert list != null;
    return list;
  }

  @Override
  @Nonnull
  public Location getLocation() {
    return location;
  }

  @Override
  @Nonnull
  public InputVFile getInputVFile(@Nonnull VPath path) throws NotFileOrDirectoryException {
    File file = new File(dir, path.getPathAsString(File.separatorChar));
    if (!file.isFile()) {
      throw new NotFileOrDirectoryException(new FileLocation(file));
    }
    return new DirectFile(file);
  }

  @Override
  @Nonnull
  public OutputVFile createOutputVFile(@Nonnull VPath path) throws CannotCreateFileException {
    File file = new File(dir, path.getPathAsString(getSeparator()));
    if (!file.getParentFile().mkdirs() && !file.getParentFile().isDirectory()) {
      throw new CannotCreateFileException(new DirectoryLocation(file.getParentFile()));
    }
    return new DirectFile(file);
  }

  @Override
  public char getSeparator() {
    return File.separatorChar;
  }

  @Override
  public boolean isVDir() {
    return true;
  }
}