Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Porting code to be compatible with Java 1.6 #42

Merged
merged 2 commits into from
Oct 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply plugin: 'jacoco'
apply plugin: 'com.github.kt3k.coveralls'

group = 'com.google.maps'
sourceCompatibility = 1.7
sourceCompatibility = 1.6

repositories {
mavenCentral()
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/google/maps/GeoApiContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ <T, R extends ApiResponse<T>> PendingResult<T> get(Class<R> clazz, String path,
try {
query.append(URLEncoder.encode(param.getValue(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
return new ExceptionResult<>(e);
return new ExceptionResult<T>(e);
}
}
return getWithPath(clazz, FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES, path,
Expand Down Expand Up @@ -93,7 +93,7 @@ <T, R extends ApiResponse<T>> PendingResult<T> get(Class<R> clazz,
try {
query.append(URLEncoder.encode(params[i], "UTF-8"));
} catch (UnsupportedEncodingException e) {
return new ExceptionResult<>(e);
return new ExceptionResult<T>(e);
}
}

Expand All @@ -120,7 +120,7 @@ private <T, R extends ApiResponse<T>> PendingResult<T> getWithPath(Class<R> claz
String signature = urlSigner.getSignature(url.toString());
url.append("&signature=").append(signature);
} catch (Exception e) {
return new ExceptionResult<>(e);
return new ExceptionResult<T>(e);
}
}

Expand All @@ -131,7 +131,7 @@ private <T, R extends ApiResponse<T>> PendingResult<T> getWithPath(Class<R> claz

log.log(Level.INFO, "Request: {0}", host + url);

return new OkHttpPendingResult<>(req, client, clazz, fieldNamingPolicy, errorTimeout);
return new OkHttpPendingResult<T, R>(req, client, clazz, fieldNamingPolicy, errorTimeout);
}

private void checkContext() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/google/maps/PendingResultBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class PendingResultBase<T, A extends PendingResultBase<T, A, R>,
implements PendingResult<T> {

private final GeoApiContext context;
private HashMap<String, String> params = new HashMap<>();
private HashMap<String, String> params = new HashMap<String, String>();
private PendingResult<T> delegate;
private Class<R> responseClass;
private String base;
Expand Down
35 changes: 17 additions & 18 deletions src/main/java/com/google/maps/errors/ApiException.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,23 @@ protected ApiException(String message) {
* @return The appropriate ApiException based on the status or null if no error occurred.
*/
public static ApiException from(String status, String errorMessage) {
switch (status) {
case "OK":
return null;
case "INVALID_REQUEST":
return new InvalidRequestException(errorMessage);
case "MAX_ELEMENTS_EXCEEDED":
return new MaxElementsExceededException(errorMessage);
case "NOT_FOUND":
return new NotFoundException(errorMessage);
case "OVER_QUERY_LIMIT":
return new OverQueryLimitException(errorMessage);
case "REQUEST_DENIED":
return new RequestDeniedException(errorMessage);
case "UNKNOWN_ERROR":
return new UnknownErrorException(errorMessage);
case "ZERO_RESULTS":
return new ZeroResultsException(errorMessage);
}
if ("OK".equals(status)) {
return null;
} else if ("INVALID_REQUEST".equals(status)) {
return new InvalidRequestException(errorMessage);
} else if ("MAX_ELEMENTS_EXCEEDED".equals(status)) {
return new MaxElementsExceededException(errorMessage);
} else if ("NOT_FOUND".equals(status)) {
return new NotFoundException(errorMessage);
} else if ("OVER_QUERY_LIMIT".equals(status)) {
return new OverQueryLimitException(errorMessage);
} else if ("REQUEST_DENIED".equals(status)) {
return new RequestDeniedException(errorMessage);
} else if ("UNKNOWN_ERROR".equals(status)) {
return new UnknownErrorException(errorMessage);
} else if ("ZERO_RESULTS".equals(status)) {
return new ZeroResultsException(errorMessage);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you flip these equals comparators?

"OK".equals(status)
"INVALID_REQUEST".equals(status)

etc.


// We've hit an unknown error. This is not a state we should hit,
// but we don't want to crash a user's application if we introduce a new error.
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/google/maps/internal/OkHttpPendingResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
Expand Down Expand Up @@ -133,7 +133,7 @@ public T await() throws Exception {
}
}

final BlockingQueue<QueuedResponse> waiter = new ArrayBlockingQueue<>(1);
final BlockingQueue<QueuedResponse> waiter = new ArrayBlockingQueue<QueuedResponse>(1);
final OkHttpPendingResult<T, R> parent = this;

// This callback will be called on another thread, handled by the RateLimitExecutorService.
Expand Down Expand Up @@ -207,15 +207,15 @@ private T parseResponse(OkHttpPendingResult<T, R> request, Response response) th
.registerTypeAdapter(Distance.class, new DistanceAdapter())
.registerTypeAdapter(Duration.class, new DurationAdapter())
.registerTypeAdapter(AddressComponentType.class,
new SafeEnumAdapter<>(AddressComponentType.UNKNOWN))
.registerTypeAdapter(AddressType.class, new SafeEnumAdapter<>(AddressType.UNKNOWN))
.registerTypeAdapter(TravelMode.class, new SafeEnumAdapter<>(TravelMode.UNKNOWN))
.registerTypeAdapter(LocationType.class, new SafeEnumAdapter<>(LocationType.UNKNOWN))
new SafeEnumAdapter<AddressComponentType>(AddressComponentType.UNKNOWN))
.registerTypeAdapter(AddressType.class, new SafeEnumAdapter<AddressType>(AddressType.UNKNOWN))
.registerTypeAdapter(TravelMode.class, new SafeEnumAdapter<TravelMode>(TravelMode.UNKNOWN))
.registerTypeAdapter(LocationType.class, new SafeEnumAdapter<LocationType>(LocationType.UNKNOWN))
.setFieldNamingPolicy(fieldNamingPolicy)
.create();

InputStream in = response.body().byteStream();
CharsetDecoder decoder = StandardCharsets.UTF_8.newDecoder();
CharsetDecoder decoder = Charset.forName("utf8").newDecoder();
// Handle illegal UTF-8 by skipping it.
decoder.onMalformedInput(CodingErrorAction.IGNORE);
Reader reader = new InputStreamReader(in, decoder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static List<LatLng> decode(final String encodedPath) {

// For speed we preallocate to an upper bound on the final length, then
// truncate the array before returning.
final List<LatLng> path = new ArrayList<>();
final List<LatLng> path = new ArrayList<LatLng>();
int index = 0;
int lat = 0;
int lng = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/google/maps/AuthenticatedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public class AuthenticatedTest {
@Parameters
public static Collection<Object[]> contexts() {
Collection<Object[]> contexts = new ArrayList<>();
Collection<Object[]> contexts = new ArrayList<Object[]>();

// Travis can't run authorized tests from pull requests.
// http://docs.travis-ci.com/user/pull-requests/#Security-Restrictions-when-testing-Pull-Requests
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/google/maps/GeoApiContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void testGetIncludesDefaultUserAgent() throws Exception {
// Set up a mock request
ApiResponse<String> fakeResponse = mock(ApiResponse.class);
String path = "/";
Map<String, String> params = new HashMap<>(1);
Map<String, String> params = new HashMap<String, String>(1);
params.put("key", "value");

// Set up the fake web server
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/google/maps/GeocodingApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testSimpleGeocode() throws Exception {

@Test
public void testAsync() throws Exception {
final List<GeocodingResult[]> resps = new ArrayList<>();
final List<GeocodingResult[]> resps = new ArrayList<GeocodingResult[]>();

PendingResult.Callback<GeocodingResult[]> callback =
new PendingResult.Callback<GeocodingResult[]>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class RateLimitExecutorServiceTest {
public void testRateLimitDoesNotExceedSuppliedQps() throws Exception {
int qps = 10;
RateLimitExecutorService service = new RateLimitExecutorService(qps, 50);
final ConcurrentHashMap<Integer, Integer> executedTimestamps = new ConcurrentHashMap<>();
final ConcurrentHashMap<Integer, Integer> executedTimestamps = new ConcurrentHashMap<Integer, Integer>();

for (int i = 0; i < 100; i++) {
Runnable emptyTask = new Runnable() {
Expand Down