com.amit.api.compiler.generator.GeneratedFileList Maven / Gradle / Ivy
The newest version!
/******************************************************************************
* Copyright 2014-2018 Alexandru Motriuc *
* *
******************************************************************************
* 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.amit.api.compiler.generator;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.Set;
public class GeneratedFileList {
private final Path outPath;
private final Path filePath;
private Set files = new HashSet();
public GeneratedFileList(Path outPath) throws IOException {
this.outPath = outPath;
this.filePath = outPath.resolve(".generated.amit");
load();
cleanup();
}
public void addFile(Path file) {
files.add(file);
}
public void save() throws FileNotFoundException,
UnsupportedEncodingException {
try (PrintWriter writer = new PrintWriter(filePath.toFile(), "UTF-8")) {
for (Path p : files) {
writer.println(p.toString());
}
}
}
private void load() throws IOException {
try {
for (String file : Files.readAllLines(filePath)) {
addFile(Paths.get(file));
}
} catch (NoSuchFileException e) {
// do nothing
}
}
private void cleanup() {
for (Path path : files) {
Path todelete = outPath.resolve(path);
todelete.toFile().delete();
}
files.clear();
}
}