Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
shz.core.io.FileUpdater Maven / Gradle / Ivy
package shz.core.io;
import shz.core.RegexHelp;
import shz.core.reference.ZReference;
import java.io.File;
import java.util.LinkedList;
import java.util.Queue;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public abstract class FileUpdater {
private boolean recursive = true;
private boolean stop;
protected boolean filter(File file) {
return true;
}
protected void beforeFile(File dir, File file, File desDir, File des) {
}
protected abstract void accept(File dir, File file, File desDir, File des);
protected void afterFile(File dir, File file, File desDir, File des) {
}
protected void beforeDir(File dir, File desDir) {
}
protected void afterDir(File dir, File desDir) {
}
public final void update(File src, File des) {
if (src == null || des == null || !filter(src)) return;
if (src.isFile()) acceptFile(src.getParentFile(), src, des.getParentFile(), FileHelp.checkWriteFile(des));
else if (recursive) recursiveAcceptDir(src, FileHelp.checkCopyDir(des));
else acceptDir(src, FileHelp.checkCopyDir(des));
}
private void acceptFile(File dir, File file, File desDir, File des) {
beforeFile(dir, file, desDir, des);
accept(dir, file, desDir, des);
afterFile(dir, file, desDir, des);
}
private void recursiveAcceptDir(File dir, File desDir) {
beforeDir(dir, desDir);
dir.listFiles(file -> {
if (!stop && filter(file)) {
File des = new File(desDir, file.getName());
if (file.isFile()) acceptFile(dir, file, desDir, des);
else if (des.mkdirs() || des.exists()) recursiveAcceptDir(file, des);
}
return false;
});
afterDir(dir, desDir);
}
private void acceptDir(File folder, File desFolder) {
Queue dirQueue = new LinkedList<>();
Queue desDirQueue = new LinkedList<>();
dirQueue.offer(folder);
desDirQueue.offer(desFolder);
while (!stop && !dirQueue.isEmpty()) {
File dir = dirQueue.poll();
File desDir = desDirQueue.poll();
beforeDir(dir, desDir);
dir.listFiles(file -> {
if (!stop && filter(file)) {
File des = new File(desDir, file.getName());
if (file.isFile()) acceptFile(dir, file, desDir, des);
else if (des.mkdirs() || des.exists()) {
dirQueue.offer(file);
desDirQueue.offer(des);
}
}
return false;
});
afterDir(dir, desDir);
}
}
public final void update(File src) {
update(src, FileHelp.getDcf(src));
}
public final boolean isRecursive() {
return recursive;
}
public final void setRecursive(boolean recursive) {
this.recursive = recursive;
}
public final boolean isStop() {
return stop;
}
public final void setStop(boolean stop) {
this.stop = stop;
}
@FunctionalInterface
public interface FileSimpleUpdater {
boolean accept(File dir, File file, File desDir, File des);
}
public static void update(File src, File des, FileSimpleUpdater updater, boolean recursive) {
if (src == null || des == null || updater == null) return;
if (src.isFile()) updater.accept(src.getParentFile(), src, des.getParentFile(), FileHelp.checkWriteFile(des));
else if (recursive) recursiveAcceptDir(src, FileHelp.checkCopyDir(des), updater, new ZReference());
else acceptDir(src, FileHelp.checkCopyDir(des), updater);
}
private static void recursiveAcceptDir(File dir, File desDir, FileSimpleUpdater updater, ZReference stop) {
dir.listFiles(file -> {
if (!stop.get()) {
File des = new File(desDir, file.getName());
if (file.isFile()) stop.set(updater.accept(dir, file, desDir, des));
else if (des.mkdirs() || des.exists()) recursiveAcceptDir(file, des, updater, stop);
}
return false;
});
}
private static void acceptDir(File folder, File desFolder, FileSimpleUpdater updater) {
Queue dirQueue = new LinkedList<>();
Queue desDirQueue = new LinkedList<>();
dirQueue.offer(folder);
desDirQueue.offer(desFolder);
ZReference stop = new ZReference();
while (!stop.get() && !dirQueue.isEmpty()) {
File dir = dirQueue.poll();
File desDir = desDirQueue.poll();
dir.listFiles(file -> {
if (!stop.get()) {
File des = new File(desDir, file.getName());
if (file.isFile()) stop.set(updater.accept(dir, file, desDir, des));
else if (des.mkdirs() || des.exists()) {
dirQueue.offer(file);
desDirQueue.offer(des);
}
}
return false;
});
}
}
public static void update(File src, File des, FileSimpleUpdater updater) {
update(src, des, updater, true);
}
public static void updateByLine(File src, File des, Function updater) {
update(src, des, (dir, file, desDir, desFile) -> {
IOHelp.read(IOHelp.newBufferedReader(file.toPath()), IOHelp.newBufferedWriter(desFile.toPath()), updater, null);
return false;
});
}
public static void updateByLine(File src, Function updater) {
updateByLine(src, FileHelp.getDcf(src), updater);
}
public static void replace(File src, File des, String regex, Function action) {
updateByLine(src, des, l -> RegexHelp.replace(l, Pattern.compile(regex), action));
}
public static void replace(File src, String regex, Function action) {
replace(src, FileHelp.getDcf(src), regex, action);
}
public static void replace(File src, File des, String regex, String newStr) {
updateByLine(src, des, l -> RegexHelp.isMatch(l, regex) ? l.replaceAll(regex, newStr) : l);
}
public static void replace(File src, String regex, String newStr) {
replace(src, FileHelp.getDcf(src), regex, newStr);
}
}