Created
September 4, 2017 21:11
-
-
Save ipapaste/6f38c4c9af4bd1c21a55c93e4536fc65 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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