Skip to content

Commit 55fee27

Browse files
committed
DATAMONGO-625 - Polishing.
Introduce default interface methods where possible. Rename save(…) to store(…) to align with method naming. Reduce constructor visibility to avoid invalid API usage. Replace mutable object in builder with fields to avoid mutation of already built objects when reusing the builder. Remove Options.chunked(…) factory method to avoid confusion with chunkSize method. Reformat code, strip trailing whitespaces. Original pull request: spring-projects#842.
1 parent ffba352 commit 55fee27

File tree

10 files changed

+140
-121
lines changed

10 files changed

+140
-121
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsObject.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import com.mongodb.client.gridfs.model.GridFSFile;
2222

2323
/**
24-
* A common interface when dealing with GridFs items using Spring Data.o
24+
* A common interface when dealing with GridFs items using Spring Data.
2525
*
2626
* @author Christoph Strobl
2727
* @since 3.0
@@ -31,42 +31,43 @@ public interface GridFsObject<ID, CONTENT> {
3131
/**
3232
* The {@link GridFSFile#getId()} value converted into its simple java type. <br />
3333
* A {@link org.bson.BsonString} will be converted to plain {@link String}.
34-
*
34+
*
3535
* @return can be {@literal null} depending on the implementation.
3636
*/
3737
@Nullable
3838
ID getFileId();
3939

4040
/**
4141
* The filename.
42-
*
42+
*
4343
* @return
4444
*/
4545
String getFilename();
4646

4747
/**
4848
* The actual file content.
49-
*
49+
*
5050
* @return
51+
* @throws IllegalStateException if the content cannot be obtained.
5152
*/
5253
CONTENT getContent();
5354

5455
/**
5556
* Additional information like file metadata (eg. contentType).
56-
*
57+
*
5758
* @return never {@literal null}.
5859
*/
5960
Options getOptions();
6061

6162
/**
6263
* Additional, context relevant information.
63-
*
64+
*
6465
* @author Christoph Strobl
6566
*/
6667
class Options {
6768

68-
private Document metadata = new Document();
69-
private int chunkSize = -1;
69+
private final Document metadata;
70+
private final int chunkSize;
7071

7172
private Options(Document metadata, int chunkSize) {
7273

@@ -83,16 +84,6 @@ public static Options none() {
8384
return new Options(new Document(), -1);
8485
}
8586

86-
/**
87-
* Static factory method to create {@link Options} with given chunk size.
88-
*
89-
* @param chunkSize
90-
* @return new instance of {@link Options}.
91-
*/
92-
public static Options chunked(int chunkSize) {
93-
return new Options(new Document(), chunkSize);
94-
}
95-
9687
/**
9788
* Static factory method to create {@link Options} with given content type.
9889
*
@@ -115,7 +106,7 @@ public static Options from(@Nullable GridFSFile gridFSFile) {
115106

116107
/**
117108
* Set the associated content type.
118-
*
109+
*
119110
* @param contentType must not be {@literal null}.
120111
* @return new instance of {@link Options}.
121112
*/

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsOperations.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ default ObjectId store(InputStream content, String filename) {
5959
* @param metadata can be {@literal null}.
6060
* @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created.
6161
*/
62-
ObjectId store(InputStream content, @Nullable Object metadata);
62+
default ObjectId store(InputStream content, @Nullable Object metadata) {
63+
return store(content, null, metadata);
64+
}
6365

6466
/**
6567
* Stores the given content into a file with the given name.
@@ -80,7 +82,9 @@ default ObjectId store(InputStream content, @Nullable Document metadata) {
8082
* @param contentType can be {@literal null}.
8183
* @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created.
8284
*/
83-
ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType);
85+
default ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType) {
86+
return store(content, filename, contentType, null);
87+
}
8488

8589
/**
8690
* Stores the given content into a file with the given name using the given metadata. The metadata object will be
@@ -91,7 +95,9 @@ default ObjectId store(InputStream content, @Nullable Document metadata) {
9195
* @param metadata can be {@literal null}.
9296
* @return the {@link ObjectId} of the {@link com.mongodb.client.gridfs.model.GridFSFile} just created.
9397
*/
94-
ObjectId store(InputStream content, @Nullable String filename, @Nullable Object metadata);
98+
default ObjectId store(InputStream content, @Nullable String filename, @Nullable Object metadata) {
99+
return store(content, filename, null, metadata);
100+
}
95101

96102
/**
97103
* Stores the given content into a file with the given name and content type using the given metadata. The metadata
@@ -141,21 +147,21 @@ default ObjectId store(InputStream content, @Nullable String filename, @Nullable
141147
uploadBuilder.metadata(metadata);
142148
}
143149

144-
return save(uploadBuilder.build());
150+
return store(uploadBuilder.build());
145151
}
146152

147153
/**
148154
* Stores the given {@link GridFsObject}, likely a {@link GridFsUpload}, into into a file with given
149155
* {@link GridFsObject#getFilename() name}. If the {@link GridFsObject#getFileId()} is set, the file will be stored
150156
* with that id, otherwise the server auto creates a new id. <br />
151-
*
157+
*
152158
* @param upload the {@link GridFsObject} (most likely a {@link GridFsUpload}) to be stored.
153159
* @param <T> id type of the underlying {@link com.mongodb.client.gridfs.model.GridFSFile}
154160
* @return the id of the stored file. Either an auto created value or {@link GridFsObject#getFileId()}, but never
155161
* {@literal null}.
156162
* @since 3.0
157163
*/
158-
<T> T save(GridFsObject<T, InputStream> upload);
164+
<T> T store(GridFsObject<T, InputStream> upload);
159165

160166
/**
161167
* Returns all files matching the given query. Note, that currently {@link Sort} criterias defined at the

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/gridfs/GridFsTemplate.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,31 +87,6 @@ public GridFsTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter,
8787
this.bucket = bucket;
8888
}
8989

90-
/*
91-
* (non-Javadoc)
92-
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.Object)
93-
*/
94-
@Override
95-
public ObjectId store(InputStream content, @Nullable Object metadata) {
96-
return store(content, null, metadata);
97-
}
98-
99-
/*
100-
* (non-Javadoc)
101-
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.String)
102-
*/
103-
public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType) {
104-
return store(content, filename, contentType, (Object) null);
105-
}
106-
107-
/*
108-
* (non-Javadoc)
109-
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.Object)
110-
*/
111-
public ObjectId store(InputStream content, @Nullable String filename, @Nullable Object metadata) {
112-
return store(content, filename, null, metadata);
113-
}
114-
11590
/*
11691
* (non-Javadoc)
11792
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.String, java.lang.Object)
@@ -125,7 +100,7 @@ public ObjectId store(InputStream content, @Nullable String filename, @Nullable
125100
* (non-Javadoc)
126101
* @see org.springframework.data.mongodb.gridfs.GridFsOperations#save(org.springframework.data.mongodb.gridfs.GridFsObject)
127102
*/
128-
public <T> T save(GridFsObject<T, InputStream> upload) {
103+
public <T> T store(GridFsObject<T, InputStream> upload) {
129104

130105
GridFSUploadOptions uploadOptions = computeUploadOptionsFor(upload.getOptions().getContentType(),
131106
upload.getOptions().getMetadata());

0 commit comments

Comments
 (0)