1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16package com.google.currysrc.api.input;
17
18import com.google.common.collect.Lists;
19import com.google.currysrc.api.input.InputFileGenerator;
20
21import java.io.File;
22import java.util.List;
23
24/**
25 * Generates a set of .java input files beneath a given base directory.
26 */
27public final class DirectoryInputFileGenerator implements InputFileGenerator {
28
29  private final File baseDir;
30
31  public DirectoryInputFileGenerator(File baseDir) {
32    if (baseDir == null) {
33      throw new NullPointerException("Null baseDir");
34    }
35    this.baseDir = baseDir;
36  }
37
38  @Override
39  public Iterable<? extends File> generate() {
40    List<File> files = Lists.newArrayList();
41    collectFiles(baseDir, files);
42    return files;
43  }
44
45  private void collectFiles(File baseDir, List<File> files) {
46    if (!baseDir.exists()) {
47      throw new IllegalArgumentException("Not found: " + baseDir.getAbsolutePath());
48    }
49    if (!baseDir.isDirectory()) {
50      throw new IllegalArgumentException("Not a directory: " + baseDir.getAbsolutePath());
51    }
52    for (File file : baseDir.listFiles()) {
53      if (file.isDirectory()) {
54        collectFiles(file, files);
55      } else if (file.getName().endsWith(".java")) {
56        files.add(file);
57      } else {
58        System.out.println("Ignoring file: " + file);
59      }
60    }
61  }
62}
63