Skip to content

Commit cfb65ab

Browse files
Fixing more Storage code style issues (#594)
1 parent 38ad4b0 commit cfb65ab

File tree

7 files changed

+101
-103
lines changed

7 files changed

+101
-103
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/FirebaseFirestore.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,15 +261,15 @@ public Query collectionGroup(@NonNull String collectionId) {
261261
* @param executor The executor to run the transaction callback on.
262262
* @return The task returned from the updateFunction.
263263
*/
264-
private <TResult> Task<TResult> runTransaction(
265-
Transaction.Function<TResult> updateFunction, Executor executor) {
264+
private <ResultT> Task<ResultT> runTransaction(
265+
Transaction.Function<ResultT> updateFunction, Executor executor) {
266266
ensureClientConfigured();
267267

268268
// We wrap the function they provide in order to
269269
// 1. Use internal implementation classes for Transaction,
270270
// 2. Convert exceptions they throw into Tasks, and
271271
// 3. Run the user callback on the user queue.
272-
Function<com.google.firebase.firestore.core.Transaction, Task<TResult>> wrappedUpdateFunction =
272+
Function<com.google.firebase.firestore.core.Transaction, Task<ResultT>> wrappedUpdateFunction =
273273
internalTransaction ->
274274
Tasks.call(
275275
executor,

firebase-storage/src/main/java/com/google/firebase/storage/StreamDownloadTask.java

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,24 @@
3838
public class StreamDownloadTask extends StorageTask<StreamDownloadTask.TaskSnapshot> {
3939
static final long PREFERRED_CHUNK_SIZE = 256 * 1024;
4040
private static final String TAG = "StreamDownloadTask";
41-
private StorageReference mStorageRef;
42-
private ExponentialBackoffSender mSender;
43-
private volatile Exception mException = null;
44-
private volatile int mResultCode = 0;
45-
private StreamProcessor mProcessor;
46-
47-
private long mTotalBytes = -1;
48-
private long mBytesDownloaded;
49-
private long mBytesDownloadedSnapped;
50-
private InputStream mInputStream;
51-
private NetworkRequest mRequest;
52-
private String mETagVerification;
41+
private StorageReference storageRef;
42+
private ExponentialBackoffSender sender;
43+
private volatile Exception exception = null;
44+
private volatile int resultCode = 0;
45+
private StreamProcessor processor;
46+
47+
private long totalBytes = -1;
48+
private long bytesDownloaded;
49+
private long bytesDownloadedSnapped;
50+
private InputStream inputStream;
51+
private NetworkRequest request;
52+
private String eTagVerification;
5353

5454
/*package*/ StreamDownloadTask(@NonNull StorageReference storageRef) {
55-
mStorageRef = storageRef;
55+
this.storageRef = storageRef;
5656

57-
FirebaseStorage storage = mStorageRef.getStorage();
58-
mSender =
57+
FirebaseStorage storage = this.storageRef.getStorage();
58+
sender =
5959
new ExponentialBackoffSender(
6060
storage.getApp().getApplicationContext(),
6161
storage.getAuthProvider(),
@@ -71,16 +71,16 @@ public class StreamDownloadTask extends StorageTask<StreamDownloadTask.TaskSnaps
7171
*/
7272
/*package*/ StreamDownloadTask setStreamProcessor(@NonNull StreamProcessor processor) {
7373
Preconditions.checkNotNull(processor);
74-
Preconditions.checkState(mProcessor == null);
75-
this.mProcessor = processor;
74+
Preconditions.checkState(this.processor == null);
75+
this.processor = processor;
7676
return this;
7777
}
7878

7979
/** @return the target of the download. */
8080
@Override
8181
@NonNull
8282
/*package*/ StorageReference getStorage() {
83-
return mStorageRef;
83+
return storageRef;
8484
}
8585

8686
/**
@@ -89,16 +89,16 @@ public class StreamDownloadTask extends StorageTask<StreamDownloadTask.TaskSnaps
8989
*/
9090
@SuppressWarnings("unused")
9191
/*package*/ long getTotalBytes() {
92-
return mTotalBytes;
92+
return totalBytes;
9393
}
9494

9595
void recordDownloadedBytes(long bytesDownloaded) {
96-
mBytesDownloaded += bytesDownloaded;
97-
if (mBytesDownloadedSnapped + PREFERRED_CHUNK_SIZE <= mBytesDownloaded) {
96+
this.bytesDownloaded += bytesDownloaded;
97+
if (bytesDownloadedSnapped + PREFERRED_CHUNK_SIZE <= this.bytesDownloaded) {
9898
if (getInternalState() == INTERNAL_STATE_IN_PROGRESS) {
9999
tryChangeState(INTERNAL_STATE_IN_PROGRESS, false);
100100
} else {
101-
mBytesDownloadedSnapped = mBytesDownloaded;
101+
bytesDownloadedSnapped = this.bytesDownloaded;
102102
}
103103
}
104104
}
@@ -112,37 +112,37 @@ protected void schedule() {
112112

113113
@SuppressWarnings({"JavaDoc", "ThrowableResultOfMethodCallIgnored"})
114114
private InputStream createDownloadStream() throws Exception {
115-
mSender.reset();
115+
sender.reset();
116116

117-
if (mRequest != null) {
118-
mRequest.performRequestEnd();
117+
if (request != null) {
118+
request.performRequestEnd();
119119
}
120120

121-
mRequest =
122-
new GetNetworkRequest(mStorageRef.getStorageUri(), mStorageRef.getApp(), mBytesDownloaded);
121+
request =
122+
new GetNetworkRequest(storageRef.getStorageUri(), storageRef.getApp(), bytesDownloaded);
123123

124-
mSender.sendWithExponentialBackoff(mRequest, false);
125-
mResultCode = mRequest.getResultCode();
126-
mException = mRequest.getException() != null ? mRequest.getException() : mException;
124+
sender.sendWithExponentialBackoff(request, false);
125+
resultCode = request.getResultCode();
126+
exception = request.getException() != null ? request.getException() : exception;
127127
boolean success =
128-
isValidHttpResponseCode(mResultCode)
129-
&& mException == null
128+
isValidHttpResponseCode(resultCode)
129+
&& exception == null
130130
&& getInternalState() == INTERNAL_STATE_IN_PROGRESS;
131131

132132
if (success) {
133-
String newEtag = mRequest.getResultString("ETag");
133+
String newEtag = request.getResultString("ETag");
134134
if (!TextUtils.isEmpty(newEtag)
135-
&& mETagVerification != null
136-
&& !mETagVerification.equals(newEtag)) {
137-
mResultCode = HttpURLConnection.HTTP_CONFLICT;
135+
&& eTagVerification != null
136+
&& !eTagVerification.equals(newEtag)) {
137+
resultCode = HttpURLConnection.HTTP_CONFLICT;
138138
throw new IOException("The ETag on the server changed.");
139139
}
140140

141-
mETagVerification = newEtag;
142-
if (mTotalBytes == -1) {
143-
mTotalBytes = mRequest.getResultingContentLength();
141+
eTagVerification = newEtag;
142+
if (totalBytes == -1) {
143+
totalBytes = request.getResultingContentLength();
144144
}
145-
return mRequest.getStream();
145+
return request.getStream();
146146
} else {
147147
throw new IOException("Could not open resulting stream.");
148148
}
@@ -152,7 +152,7 @@ private InputStream createDownloadStream() throws Exception {
152152
@SuppressWarnings({"JavaDoc", "ThrowableResultOfMethodCallIgnored"})
153153
@Override
154154
/*package*/ void run() {
155-
if (mException != null) {
155+
if (exception != null) {
156156
tryChangeState(INTERNAL_STATE_FAILURE, false);
157157
return;
158158
}
@@ -170,31 +170,31 @@ public InputStream call() throws Exception {
170170
}
171171
},
172172
StreamDownloadTask.this);
173-
mInputStream = new BufferedInputStream(streamWrapper);
173+
inputStream = new BufferedInputStream(streamWrapper);
174174

175175
try {
176176
// Open stream to fetch initial state.
177177
streamWrapper.ensureStream();
178178

179-
if (mProcessor != null) {
179+
if (processor != null) {
180180
try {
181-
mProcessor.doInBackground(snapState(), mInputStream);
181+
processor.doInBackground(snapState(), inputStream);
182182
} catch (Exception e) {
183183
Log.w(TAG, "Exception occurred calling doInBackground.", e);
184-
mException = e;
184+
exception = e;
185185
}
186186
}
187187
} catch (IOException e) {
188188
Log.d(TAG, "Initial opening of Stream failed", e);
189-
mException = e;
189+
exception = e;
190190
}
191191

192-
if (mInputStream == null) {
193-
mRequest.performRequestEnd();
194-
mRequest = null;
192+
if (inputStream == null) {
193+
request.performRequestEnd();
194+
request = null;
195195
}
196196

197-
boolean success = mException == null && getInternalState() == INTERNAL_STATE_IN_PROGRESS;
197+
boolean success = exception == null && getInternalState() == INTERNAL_STATE_IN_PROGRESS;
198198

199199
if (success) {
200200
tryChangeState(INTERNAL_STATE_IN_PROGRESS, false);
@@ -228,19 +228,18 @@ public boolean pause() {
228228
@Override
229229
TaskSnapshot snapStateImpl() {
230230
return new TaskSnapshot(
231-
StorageException.fromExceptionAndHttpCode(mException, mResultCode),
232-
mBytesDownloadedSnapped);
231+
StorageException.fromExceptionAndHttpCode(exception, resultCode), bytesDownloadedSnapped);
233232
}
234233

235234
@Override
236235
protected void onCanceled() {
237-
mSender.cancel();
238-
mException = StorageException.fromErrorStatus(Status.RESULT_CANCELED);
236+
sender.cancel();
237+
exception = StorageException.fromErrorStatus(Status.RESULT_CANCELED);
239238
}
240239

241240
@Override
242241
protected void onProgress() {
243-
mBytesDownloadedSnapped = mBytesDownloaded;
242+
bytesDownloadedSnapped = bytesDownloaded;
244243
}
245244

246245
private boolean isValidHttpResponseCode(int code) {
@@ -377,9 +376,9 @@ public void close() throws IOException {
377376
mWrappedStream.close();
378377
}
379378
mStreamClosed = true;
380-
if (mParentTask != null && mParentTask.mRequest != null) {
381-
mParentTask.mRequest.performRequestEnd();
382-
mParentTask.mRequest = null;
379+
if (mParentTask != null && mParentTask.request != null) {
380+
mParentTask.request.performRequestEnd();
381+
mParentTask.request = null;
383382
}
384383

385384
checkCancel();
@@ -506,7 +505,7 @@ public long getTotalByteCount() {
506505
*/
507506
@PublicApi
508507
public InputStream getStream() {
509-
return StreamDownloadTask.this.mInputStream;
508+
return StreamDownloadTask.this.inputStream;
510509
}
511510
}
512511
}

firebase-storage/src/main/java/com/google/firebase/storage/network/NetworkRequest.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,16 @@ public static String getPathWithoutBucket(@NonNull Uri gsUri) {
137137
return path;
138138
}
139139

140+
/**
141+
* Returns the path of the object but excludes the bucket name
142+
*
143+
* @return the path in string form.
144+
*/
145+
@Nullable
146+
public String getPathWithoutBucket() {
147+
return getPathWithoutBucket(mGsUri);
148+
}
149+
140150
@NonNull
141151
protected abstract String getAction();
142152

@@ -150,16 +160,6 @@ protected String getURL() {
150160
return getDefaultURL(mGsUri);
151161
}
152162

153-
/**
154-
* Returns the path of the object but excludes the bucket name
155-
*
156-
* @return the path in string form.
157-
*/
158-
@Nullable
159-
public String getPathWithoutBucket() {
160-
return getPathWithoutBucket(mGsUri);
161-
}
162-
163163
/**
164164
* Can be overridden to return a JSONObject to populate the request body.
165165
*
@@ -418,22 +418,6 @@ private void parseResponse(@NonNull HttpURLConnection conn) throws IOException {
418418
}
419419
}
420420

421-
private void processResponseStream() throws IOException {
422-
if (isResultSuccess()) {
423-
parseSuccessulResponse(resultInputStream);
424-
} else {
425-
parseErrorResponse(resultInputStream);
426-
}
427-
}
428-
429-
protected void parseSuccessulResponse(@Nullable InputStream resultStream) throws IOException {
430-
parseResponse(resultStream);
431-
}
432-
433-
protected void parseErrorResponse(@Nullable InputStream resultStream) throws IOException {
434-
parseResponse(resultStream);
435-
}
436-
437421
@SuppressWarnings("TryFinallyCanBeTryWithResources")
438422
private void parseResponse(@Nullable InputStream resultStream) throws IOException {
439423
StringBuilder sb = new StringBuilder();
@@ -455,6 +439,22 @@ private void parseResponse(@Nullable InputStream resultStream) throws IOExceptio
455439
}
456440
}
457441

442+
private void processResponseStream() throws IOException {
443+
if (isResultSuccess()) {
444+
parseSuccessulResponse(resultInputStream);
445+
} else {
446+
parseErrorResponse(resultInputStream);
447+
}
448+
}
449+
450+
protected void parseSuccessulResponse(@Nullable InputStream resultStream) throws IOException {
451+
parseResponse(resultStream);
452+
}
453+
454+
protected void parseErrorResponse(@Nullable InputStream resultStream) throws IOException {
455+
parseResponse(resultStream);
456+
}
457+
458458
@Nullable
459459
public String getRawResult() {
460460
return rawStringResponse;

firebase-storage/src/test/java/com/google/firebase/storage/DependencyTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,9 @@ public class DependencyTest {
7373
* If this test fails, its because you added a new method/overload to Task and you need to let
7474
* someone in Firebase Storage know. Otherwise users will see NotImplementedException on these new
7575
* methods for Storage Tasks. Please contact benwu@ for more info.
76-
*
77-
* @throws Exception
7876
*/
7977
@Test
80-
public void catchNewTaskMethods() throws Exception {
78+
public void catchNewTaskMethods() {
8179
StringBuilder builder = new StringBuilder();
8280

8381
try {

firebase-storage/src/test/java/com/google/firebase/storage/RetryRule.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020

2121
/** Retries tests that fail due to the test harnesses' unpredictable threading behavior. */
2222
public class RetryRule implements TestRule {
23-
private int retryCount;
23+
private final int retryCount;
2424

2525
public RetryRule(int retryCount) {
2626
this.retryCount = retryCount;
2727
}
2828

29+
@Override
2930
public Statement apply(final Statement base, Description description) {
3031
return new Statement() {
3132
@Override

firebase-storage/src/test/java/com/google/firebase/storage/TestUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private static void verifyTaskStateChanges(@Nullable InputStream inputStream, St
111111
} else {
112112
if (!originalLine.equals(newLine)) {
113113
System.err.println("Original:");
114-
System.err.println(baselineContents.toString());
114+
System.err.println(baselineContents);
115115
System.err.println("New:");
116116
System.err.println(contents);
117117
}

0 commit comments

Comments
 (0)