Skip to content

Instantly share code, notes, and snippets.

@ipapaste
Created September 4, 2017 20:48
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/d7350cd071e72ab01800b8b0d076c8cc to your computer and use it in GitHub Desktop.
Save ipapaste/d7350cd071e72ab01800b8b0d076c8cc to your computer and use it in GitHub Desktop.
public Result<FileDto> getFromFile(File f) {
Result<FileDto> result = new Result<>();
FileDto dto = new FileDto();
dto.setName(f.getName());
dto.setPath(f.getAbsolutePath());
dto.setDirectory(f.isDirectory());
dto.setSize(f.length());
dto.setModifiedAt(f.lastModified());
try {
BasicFileAttributes attr = Files.readAttributes(f.toPath(), BasicFileAttributes.class);
dto.setCreatedAt(attr.creationTime().toMillis());
}
catch(Exception e)
{
result.setIncomplete(true);
result.getReasons().add(new Exception("Unable to transform " +f.getAbsolutePath() + " to DTO.", e));
}
return result;
}
public static class Result<T>
{
T result;
boolean incomplete;
List<Exception> reasons = new ArrayList<>();
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<Exception> getReasons() {
return reasons;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment