io.datakernel.loader.FileNamesLoadingService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of datakernel-http Show documentation
Show all versions of datakernel-http Show documentation
High-performance asynchronous HTTP clients and servers collection.
Package contains a bunch of different built-in servlets for request dispatching, loading of a static content, etc.
package io.datakernel.loader;
import io.datakernel.async.Stage;
import io.datakernel.eventloop.Eventloop;
import io.datakernel.eventloop.EventloopService;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.stream.Stream;
public class FileNamesLoadingService implements EventloopService {
private final Eventloop eventloop;
private final ExecutorService executorService;
private final Path path;
private Set fileNames;
private FileNamesLoadingService(Eventloop eventloop, ExecutorService executorService, Path path) {
this.eventloop = eventloop;
this.executorService = executorService;
this.path = path;
}
public static FileNamesLoadingService create(Eventloop eventloop, ExecutorService executorService, Path path) {
return new FileNamesLoadingService(eventloop, executorService, path);
}
@Override
public Eventloop getEventloop() {
return eventloop;
}
@Override
public Stage start() {
return Stage.ofCallable(executorService,
() -> {
Set names = new HashSet<>();
try (Stream pathStream = Files.walk(path)) {
pathStream
.filter(Files::isRegularFile)
.map(path1 -> path1.toFile().getName())
.forEach(names::add);
return names;
} catch (IOException e) {
throw new RuntimeException(e);
}
})
.whenResult(strings -> fileNames = strings)
.toVoid();
}
@Override
public Stage stop() {
return Stage.of(null);
}
public Set getFileNames() {
return fileNames;
}
public boolean contains(String value) {
return fileNames.contains(value);
}
}