DATAMONGO-1813 - Polishing.
Add since tag. Add non-null guard. Refactor conditional resource mapping to Optional. Apply code formatter. Optimize array construction from List. Original pull request: #543.
This commit is contained in:
@@ -160,8 +160,10 @@ public interface GridFsOperations extends ResourcePatternResolver {
|
||||
|
||||
/**
|
||||
* Returns the {@link GridFsResource} for a {@link com.mongodb.client.gridfs.model.GridFSFile}.
|
||||
*
|
||||
* @param file must not be {@literal null}.
|
||||
* @return the resource for the file.
|
||||
* @since 2.1
|
||||
*/
|
||||
GridFsResource getResource(com.mongodb.client.gridfs.model.GridFSFile file);
|
||||
|
||||
|
||||
@@ -227,9 +227,7 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
|
||||
* @see org.springframework.core.io.ResourceLoader#getResource(java.lang.String)
|
||||
*/
|
||||
public GridFsResource getResource(String location) {
|
||||
|
||||
GridFSFile file = findOne(query(whereFilename().is(location)));
|
||||
return file != null ? getResource(file) : null;
|
||||
return Optional.ofNullable(findOne(query(whereFilename().is(location)))).map(this::getResource).orElse(null);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -238,6 +236,8 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
|
||||
*/
|
||||
public GridFsResource getResource(GridFSFile file) {
|
||||
|
||||
Assert.notNull(file, "GridFSFile must not be null!");
|
||||
|
||||
return new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename()));
|
||||
}
|
||||
|
||||
@@ -256,13 +256,13 @@ public class GridFsTemplate implements GridFsOperations, ResourcePatternResolver
|
||||
if (path.isPattern()) {
|
||||
|
||||
GridFSFindIterable files = find(query(whereFilename().regex(path.toRegex())));
|
||||
List<GridFsResource> resources = new ArrayList<GridFsResource>();
|
||||
List<GridFsResource> resources = new ArrayList<>();
|
||||
|
||||
for (GridFSFile file : files) {
|
||||
resources.add(new GridFsResource(file, getGridFs().openDownloadStream(file.getFilename())));
|
||||
}
|
||||
|
||||
return resources.toArray(new GridFsResource[resources.size()]);
|
||||
return resources.toArray(new GridFsResource[0]);
|
||||
}
|
||||
|
||||
return new GridFsResource[] { getResource(locationPattern) };
|
||||
|
||||
@@ -24,7 +24,6 @@ import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.bson.BsonObjectId;
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
@@ -233,7 +232,6 @@ public class GridFsTemplateIntegrationTests {
|
||||
Document metadata = new Document("key", "value");
|
||||
ObjectId reference = operations.store(resource.getInputStream(), "foobar", metadata);
|
||||
|
||||
List<com.mongodb.client.gridfs.model.GridFSFile> files = new ArrayList<com.mongodb.client.gridfs.model.GridFSFile>();
|
||||
GridFSFile file = operations.findOne(query(whereMetaData("key").is("value")));
|
||||
GridFsResource result = operations.getResource(file);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user