Skip to content

Commit

Permalink
Apply google-java-format to all code. (google#165)
Browse files Browse the repository at this point in the history
Also adds Gradle commands to format the code and verify formatting on
all files in the tree. CONTRIBUTING.md has been updated with the
steps to format + verify changes, and the format verification has
been added to the continuous integration setup (but if it fails, the
rest of the build will still be attempted to minimize churn).

Fixes google#99
  • Loading branch information
jpd236 authored Apr 11, 2018
1 parent 5307293 commit bdc8055
Show file tree
Hide file tree
Showing 87 changed files with 1,632 additions and 1,534 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ env:

# Publish a SNAPSHOT build for all commits to master.
script:
- ./gradlew build connectedCheck && ./publish-snapshot-on-commit.sh
- ./gradlew --continue verifyGoogleJavaFormat build connectedCheck && ./publish-snapshot-on-commit.sh
16 changes: 16 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,19 @@ use GitHub pull requests for this purpose. Consult [GitHub Help] for more
information on using pull requests.

[GitHub Help]: https://help.github.com/articles/about-pull-requests/

## Preparing a pull request for review

Ensure your change is properly formatted by running:

```console
$ ./gradlew googleJavaFormat
```

Then verify that your change builds and passes tests with:

```console
$ ./gradlew --continue verifyGoogleJavaFormat build connectedCheck
```

Please correct any failures before requesting a review.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ buildscript {
}

plugins {
id "com.github.sherter.google-java-format" version "0.6"
id "net.ltgt.errorprone" version "0.0.13"
}

googleJavaFormat {
toolVersion = '1.5'
options style: 'AOSP'
}

apply plugin: 'com.android.library'

repositories {
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/android/volley/AuthFailureError.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@

import android.content.Intent;

/**
* Error indicating that there was an authentication failure when performing a Request.
*/
/** Error indicating that there was an authentication failure when performing a Request. */
@SuppressWarnings("serial")
public class AuthFailureError extends VolleyError {
/** An intent that can be used to resolve this exception. (Brings up the password dialog.) */
private Intent mResolutionIntent;

public AuthFailureError() { }
public AuthFailureError() {}

public AuthFailureError(Intent intent) {
mResolutionIntent = intent;
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/com/android/volley/Cache.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,49 @@
import java.util.List;
import java.util.Map;

/**
* An interface for a cache keyed by a String with a byte array as data.
*/
/** An interface for a cache keyed by a String with a byte array as data. */
public interface Cache {
/**
* Retrieves an entry from the cache.
*
* @param key Cache key
* @return An {@link Entry} or null in the event of a cache miss
*/
Entry get(String key);

/**
* Adds or replaces an entry to the cache.
*
* @param key Cache key
* @param entry Data to store and metadata for cache coherency, TTL, etc.
*/
void put(String key, Entry entry);

/**
* Performs any potentially long-running actions needed to initialize the cache;
* will be called from a worker thread.
* Performs any potentially long-running actions needed to initialize the cache; will be called
* from a worker thread.
*/
void initialize();

/**
* Invalidates an entry in the cache.
*
* @param key Cache key
* @param fullExpire True to fully expire the entry, false to soft expire
*/
void invalidate(String key, boolean fullExpire);

/**
* Removes an entry from the cache.
*
* @param key Cache key
*/
void remove(String key);

/**
* Empties the cache.
*/
/** Empties the cache. */
void clear();

/**
* Data and metadata for an entry returned by the cache.
*/
/** Data and metadata for an entry returned by the cache. */
class Entry {
/** The data returned from cache. */
public byte[] data;
Expand Down Expand Up @@ -110,5 +108,4 @@ public boolean refreshNeeded() {
return this.softTtl < System.currentTimeMillis();
}
}

}
77 changes: 42 additions & 35 deletions src/main/java/com/android/volley/CacheDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.android.volley;

import android.os.Process;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -27,11 +26,10 @@
/**
* Provides a thread for performing cache triage on a queue of requests.
*
* Requests added to the specified cache queue are resolved from cache.
* Any deliverable response is posted back to the caller via a
* {@link ResponseDelivery}. Cache misses and responses that require
* refresh are enqueued on the specified network queue for processing
* by a {@link NetworkDispatcher}.
* <p>Requests added to the specified cache queue are resolved from cache. Any deliverable response
* is posted back to the caller via a {@link ResponseDelivery}. Cache misses and responses that
* require refresh are enqueued on the specified network queue for processing by a {@link
* NetworkDispatcher}.
*/
public class CacheDispatcher extends Thread {

Expand All @@ -56,17 +54,19 @@ public class CacheDispatcher extends Thread {
private final WaitingRequestManager mWaitingRequestManager;

/**
* Creates a new cache triage dispatcher thread. You must call {@link #start()}
* in order to begin processing.
* Creates a new cache triage dispatcher thread. You must call {@link #start()} in order to
* begin processing.
*
* @param cacheQueue Queue of incoming requests for triage
* @param networkQueue Queue to post requests that require network to
* @param cache Cache interface to use for resolution
* @param delivery Delivery interface to use for posting responses
*/
public CacheDispatcher(
BlockingQueue<Request<?>> cacheQueue, BlockingQueue<Request<?>> networkQueue,
Cache cache, ResponseDelivery delivery) {
BlockingQueue<Request<?>> cacheQueue,
BlockingQueue<Request<?>> networkQueue,
Cache cache,
ResponseDelivery delivery) {
mCacheQueue = cacheQueue;
mNetworkQueue = networkQueue;
mCache = cache;
Expand All @@ -75,8 +75,8 @@ public CacheDispatcher(
}

/**
* Forces this dispatcher to quit immediately. If any requests are still in
* the queue, they are not guaranteed to be processed.
* Forces this dispatcher to quit immediately. If any requests are still in the queue, they are
* not guaranteed to be processed.
*/
public void quit() {
mQuit = true;
Expand Down Expand Up @@ -142,8 +142,9 @@ private void processRequest() throws InterruptedException {

// We have a cache hit; parse its data for delivery back to the request.
request.addMarker("cache-hit");
Response<?> response = request.parseNetworkResponse(
new NetworkResponse(entry.data, entry.responseHeaders));
Response<?> response =
request.parseNetworkResponse(
new NetworkResponse(entry.data, entry.responseHeaders));
request.addMarker("cache-hit-parsed");

if (!entry.refreshNeeded()) {
Expand All @@ -161,17 +162,20 @@ private void processRequest() throws InterruptedException {
if (!mWaitingRequestManager.maybeAddToWaitingRequests(request)) {
// Post the intermediate response back to the user and have
// the delivery then forward the request along to the network.
mDelivery.postResponse(request, response, new Runnable() {
@Override
public void run() {
try {
mNetworkQueue.put(request);
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
}
}
});
mDelivery.postResponse(
request,
response,
new Runnable() {
@Override
public void run() {
try {
mNetworkQueue.put(request);
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
}
}
});
} else {
// request has been added to list of waiting requests
// to receive the network response from the first request once it returns.
Expand All @@ -186,10 +190,10 @@ private static class WaitingRequestManager implements Request.NetworkRequestComp
* Staging area for requests that already have a duplicate request in flight.
*
* <ul>
* <li>containsKey(cacheKey) indicates that there is a request in flight for the given cache
* key.</li>
* <li>get(cacheKey) returns waiting requests for the given cache key. The in flight request
* is <em>not</em> contained in that list. Is null if no requests are staged.</li>
* <li>containsKey(cacheKey) indicates that there is a request in flight for the given
* cache key.
* <li>get(cacheKey) returns waiting requests for the given cache key. The in flight
* request is <em>not</em> contained in that list. Is null if no requests are staged.
* </ul>
*/
private final Map<String, List<Request<?>>> mWaitingRequests = new HashMap<>();
Expand All @@ -214,7 +218,8 @@ public void onResponseReceived(Request<?> request, Response<?> response) {
}
if (waitingRequests != null) {
if (VolleyLog.DEBUG) {
VolleyLog.v("Releasing %d waiting requests for cacheKey=%s.",
VolleyLog.v(
"Releasing %d waiting requests for cacheKey=%s.",
waitingRequests.size(), cacheKey);
}
// Process all queued up requests.
Expand All @@ -231,7 +236,8 @@ public synchronized void onNoUsableResponseReceived(Request<?> request) {
List<Request<?>> waitingRequests = mWaitingRequests.remove(cacheKey);
if (waitingRequests != null && !waitingRequests.isEmpty()) {
if (VolleyLog.DEBUG) {
VolleyLog.v("%d waiting requests for cacheKey=%s; resend to network",
VolleyLog.v(
"%d waiting requests for cacheKey=%s; resend to network",
waitingRequests.size(), cacheKey);
}
Request<?> nextInLine = waitingRequests.remove(0);
Expand All @@ -250,11 +256,12 @@ public synchronized void onNoUsableResponseReceived(Request<?> request) {
}

/**
* For cacheable requests, if a request for the same cache key is already in flight,
* add it to a queue to wait for that in-flight request to finish.
* For cacheable requests, if a request for the same cache key is already in flight, add it
* to a queue to wait for that in-flight request to finish.
*
* @return whether the request was queued. If false, we should continue issuing the request
* over the network. If true, we should put the request on hold to be processed when
* the in-flight request finishes.
* over the network. If true, we should put the request on hold to be processed when the
* in-flight request finishes.
*/
private synchronized boolean maybeAddToWaitingRequests(Request<?> request) {
String cacheKey = request.getCacheKey();
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/android/volley/ClientError.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
/**
* Indicates that the server responded with an error response indicating that the client has erred.
*
* For backwards compatibility, extends ServerError which used to be thrown for all server errors,
* including 4xx error codes indicating a client error.
* <p>For backwards compatibility, extends ServerError which used to be thrown for all server
* errors, including 4xx error codes indicating a client error.
*/
@SuppressWarnings("serial")
public class ClientError extends ServerError {
Expand All @@ -32,4 +32,3 @@ public ClientError() {
super();
}
}

26 changes: 8 additions & 18 deletions src/main/java/com/android/volley/DefaultRetryPolicy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package com.android.volley;

/**
* Default retry policy for requests.
*/
/** Default retry policy for requests. */
public class DefaultRetryPolicy implements RetryPolicy {
/** The current timeout in milliseconds. */
private int mCurrentTimeoutMs;
Expand All @@ -41,15 +39,14 @@ public class DefaultRetryPolicy implements RetryPolicy {
/** The default backoff multiplier */
public static final float DEFAULT_BACKOFF_MULT = 1f;

/**
* Constructs a new retry policy using the default timeouts.
*/
/** Constructs a new retry policy using the default timeouts. */
public DefaultRetryPolicy() {
this(DEFAULT_TIMEOUT_MS, DEFAULT_MAX_RETRIES, DEFAULT_BACKOFF_MULT);
}

/**
* Constructs a new retry policy.
*
* @param initialTimeoutMs The initial timeout for the policy.
* @param maxNumRetries The maximum number of retries.
* @param backoffMultiplier Backoff multiplier for the policy.
Expand All @@ -60,31 +57,26 @@ public DefaultRetryPolicy(int initialTimeoutMs, int maxNumRetries, float backoff
mBackoffMultiplier = backoffMultiplier;
}

/**
* Returns the current timeout.
*/
/** Returns the current timeout. */
@Override
public int getCurrentTimeout() {
return mCurrentTimeoutMs;
}

/**
* Returns the current retry count.
*/
/** Returns the current retry count. */
@Override
public int getCurrentRetryCount() {
return mCurrentRetryCount;
}

/**
* Returns the backoff multiplier for the policy.
*/
/** Returns the backoff multiplier for the policy. */
public float getBackoffMultiplier() {
return mBackoffMultiplier;
}

/**
* Prepares for the next retry by applying a backoff to the timeout.
*
* @param error The error code of the last attempt.
*/
@Override
Expand All @@ -96,9 +88,7 @@ public void retry(VolleyError error) throws VolleyError {
}
}

/**
* Returns true if this policy has attempts remaining, false otherwise.
*/
/** Returns true if this policy has attempts remaining, false otherwise. */
protected boolean hasAttemptRemaining() {
return mCurrentRetryCount <= mMaxNumRetries;
}
Expand Down
Loading

0 comments on commit bdc8055

Please sign in to comment.