Skip to content

Instantly share code, notes, and snippets.

@ipapaste
Created September 4, 2017 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipapaste/6f38c4c9af4bd1c21a55c93e4536fc65 to your computer and use it in GitHub Desktop.
Save ipapaste/6f38c4c9af4bd1c21a55c93e4536fc65 to your computer and use it in GitHub Desktop.
public Result<FileDto> getFromFile(File f) {
FileDto dto = new FileDto();
Result<FileDto> result = new Result<>(dto);
result.apply(r -> r.setName(f.getName()));
result.apply(r -> r.setPath(f.getAbsolutePath()));
result.apply(r -> r.setDirectory(f.isDirectory()));
result.apply(r -> r.setSize(f.length()));
result.apply(r -> r.setModifiedAt(f.lastModified()));
result.apply(r -> {
BasicFileAttributes attr = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
dto.setCreatedAt(attr.creationTime().toMillis());
});
return result;
}
public static class Result<T>
{
T result;
boolean incomplete;
List<Throwable> reasons = new ArrayList<>();
public Result(T t)
{
result = t;
}
public void apply(ExceptionalConsumer<T> consumer)
{
try
{
consumer.accept(result);
}
catch(Throwable e)
{
getReasons().add(e);
}
}
public T getResult() {
return result;
}
public void setResult(T result) {
this.result = result;
}
public boolean isIncomplete() {
return incomplete;
}
public void setIncomplete(boolean incomplete) {
this.incomplete = incomplete;
}
public List<Throwable> getReasons() {
return reasons;
}
public static interface ExceptionalConsumer<T>
{
public void accept(T object) throws Throwable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment