Skip to content

Refactor the client's generic type parameters #27

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

Merged
merged 2 commits into from
Mar 25, 2017
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
16 changes: 8 additions & 8 deletions ParseLiveQuery/src/main/java/com/parse/ParseLiveQueryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
import java.net.URI;
import java.util.concurrent.Executor;

public interface ParseLiveQueryClient<T extends ParseObject> {
public interface ParseLiveQueryClient {

SubscriptionHandling<T> subscribe(ParseQuery<T> query);
<T extends ParseObject> SubscriptionHandling<T> subscribe(ParseQuery<T> query);

void unsubscribe(final ParseQuery<T> query);
<T extends ParseObject> void unsubscribe(final ParseQuery<T> query);

void unsubscribe(final ParseQuery<T> query, final SubscriptionHandling subscriptionHandling);
<T extends ParseObject> void unsubscribe(final ParseQuery<T> query, final SubscriptionHandling<T> subscriptionHandling);

void reconnect();

void disconnect();

class Factory {

public static <T extends ParseObject> ParseLiveQueryClient<T> getClient(URI uri) {
return new ParseLiveQueryClientImpl<>(uri);
public static ParseLiveQueryClient getClient(URI uri) {
return new ParseLiveQueryClientImpl(uri);
}

/* package */
static <T extends ParseObject> ParseLiveQueryClient<T> getClient(URI uri, WebSocketClientFactory webSocketClientFactory, Executor taskExecutor) {
return new ParseLiveQueryClientImpl<>(uri, webSocketClientFactory, taskExecutor);
static ParseLiveQueryClient getClient(URI uri, WebSocketClientFactory webSocketClientFactory, Executor taskExecutor) {
return new ParseLiveQueryClientImpl(uri, webSocketClientFactory, taskExecutor);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

import static com.parse.Parse.checkInit;

/* package */ class ParseLiveQueryClientImpl<T extends ParseObject> implements ParseLiveQueryClient<T> {
/* package */ class ParseLiveQueryClientImpl implements ParseLiveQueryClient {

private static final String LOG_TAG = "ParseLiveQueryClient";

private final Executor taskExecutor;
private final String applicationId;
private final String clientKey;
private final SparseArray<Subscription<T>> subscriptions = new SparseArray<>();
private final SparseArray<Subscription<? extends ParseObject>> subscriptions = new SparseArray<>();
private final URI uri;
private final WebSocketClientFactory webSocketClientFactory;
private final WebSocketClient.WebSocketClientCallback webSocketClientCallback;
Expand All @@ -46,7 +46,7 @@
}

@Override
public SubscriptionHandling<T> subscribe(ParseQuery<T> query) {
public <T extends ParseObject> SubscriptionHandling<T> subscribe(ParseQuery<T> query) {
int requestId = requestIdGenerator();
Subscription<T> subscription = new Subscription<>(requestId, query);
subscriptions.append(requestId, subscription);
Expand All @@ -63,7 +63,7 @@ public SubscriptionHandling<T> subscribe(ParseQuery<T> query) {
}

@Override
public void unsubscribe(final ParseQuery<T> query) {
public <T extends ParseObject> void unsubscribe(final ParseQuery<T> query) {
if (query != null) {
for (int i = 0; i < subscriptions.size(); i++) {
Subscription subscription = subscriptions.valueAt(i);
Expand All @@ -75,7 +75,7 @@ public void unsubscribe(final ParseQuery<T> query) {
}

@Override
public void unsubscribe(final ParseQuery<T> query, final SubscriptionHandling subscriptionHandling) {
public <T extends ParseObject> void unsubscribe(final ParseQuery<T> query, final SubscriptionHandling<T> subscriptionHandling) {
if (query != null && subscriptionHandling != null) {
for (int i = 0; i < subscriptions.size(); i++) {
Subscription subscription = subscriptions.valueAt(i);
Expand Down Expand Up @@ -199,15 +199,15 @@ private void parseMessage(String message) throws LiveQueryException {
}
}

private void handleSubscribedEvent(JSONObject jsonObject) throws JSONException {
private <T extends ParseObject> void handleSubscribedEvent(JSONObject jsonObject) throws JSONException {
final int requestId = jsonObject.getInt("requestId");
final Subscription<T> subscription = subscriptionForRequestId(requestId);
if (subscription != null) {
subscription.didSubscribe(subscription.getQuery());
}
}

private void handleUnsubscribedEvent(JSONObject jsonObject) throws JSONException {
private <T extends ParseObject> void handleUnsubscribedEvent(JSONObject jsonObject) throws JSONException {
final int requestId = jsonObject.getInt("requestId");
final Subscription<T> subscription = subscriptionForRequestId(requestId);
if (subscription != null) {
Expand All @@ -216,7 +216,7 @@ private void handleUnsubscribedEvent(JSONObject jsonObject) throws JSONException
}
}

private void handleObjectEvent(Subscription.Event event, JSONObject jsonObject) throws JSONException {
private <T extends ParseObject> void handleObjectEvent(Subscription.Event event, JSONObject jsonObject) throws JSONException {
final int requestId = jsonObject.getInt("requestId");
final Subscription<T> subscription = subscriptionForRequestId(requestId);
if (subscription != null) {
Expand All @@ -225,7 +225,7 @@ private void handleObjectEvent(Subscription.Event event, JSONObject jsonObject)
}
}

private void handleErrorEvent(JSONObject jsonObject) throws JSONException {
private <T extends ParseObject> void handleErrorEvent(JSONObject jsonObject) throws JSONException {
int requestId = jsonObject.getInt("requestId");
int code = jsonObject.getInt("code");
String error = jsonObject.getString("error");
Expand All @@ -236,11 +236,12 @@ private void handleErrorEvent(JSONObject jsonObject) throws JSONException {
}
}

private Subscription<T> subscriptionForRequestId(int requestId) {
return subscriptions.get(requestId);
private <T extends ParseObject> Subscription<T> subscriptionForRequestId(int requestId) {
//noinspection unchecked
return (Subscription<T>) subscriptions.get(requestId);
}

private void sendSubscription(final Subscription<T> subscription) {
private <T extends ParseObject> void sendSubscription(final Subscription<T> subscription) {
ParseUser.getCurrentSessionTokenAsync().onSuccess(new Continuation<String, Void>() {
@Override
public Void then(Task<String> task) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.robolectric.RobolectricGradleTestRunner;
Expand Down Expand Up @@ -42,7 +43,7 @@ public class TestParseLiveQueryClient {
private PauseableExecutor executor;
private WebSocketClient webSocketClient;
private WebSocketClient.WebSocketClientCallback webSocketClientCallback;
private ParseLiveQueryClient<ParseObject> parseLiveQueryClient;
private ParseLiveQueryClient parseLiveQueryClient;

private ParseUser mockUser;

Expand Down Expand Up @@ -163,6 +164,48 @@ public void testErrorWhenSubscribedToCallback() throws Exception {
assertEquals(serverError.isReconnect(), true);
}

@Test
public void testHeterogeneousSubscriptions() throws Exception {
ParseObject.registerSubclass(MockClassA.class);
ParseObject.registerSubclass(MockClassB.class);

ParseQuery<MockClassA> query1 = ParseQuery.getQuery(MockClassA.class);
ParseQuery<MockClassB> query2 = ParseQuery.getQuery(MockClassB.class);
SubscriptionHandling<MockClassA> handle1 = parseLiveQueryClient.subscribe(query1);
SubscriptionHandling<MockClassB> handle2 = parseLiveQueryClient.subscribe(query2);

handle1.handleError(new SubscriptionHandling.HandleErrorCallback<MockClassA>() {
@Override
public void onError(ParseQuery<MockClassA> query, LiveQueryException exception) {
throw new RuntimeException(exception);
}
});
handle2.handleError(new SubscriptionHandling.HandleErrorCallback<MockClassB>() {
@Override
public void onError(ParseQuery<MockClassB> query, LiveQueryException exception) {
throw new RuntimeException(exception);
}
});

SubscriptionHandling.HandleEventCallback<MockClassA> eventMockCallback1 = mock(SubscriptionHandling.HandleEventCallback.class);
SubscriptionHandling.HandleEventCallback<MockClassB> eventMockCallback2 = mock(SubscriptionHandling.HandleEventCallback.class);

handle1.handleEvent(SubscriptionHandling.Event.CREATE, eventMockCallback1);
handle2.handleEvent(SubscriptionHandling.Event.CREATE, eventMockCallback2);

ParseObject parseObject1 = new MockClassA();
parseObject1.setObjectId("testId1");

ParseObject parseObject2 = new MockClassB();
parseObject2.setObjectId("testId2");

webSocketClientCallback.onMessage(createObjectCreateMessage(handle1.getRequestId(), parseObject1).toString());
webSocketClientCallback.onMessage(createObjectCreateMessage(handle2.getRequestId(), parseObject2).toString());

validateSameObject((SubscriptionHandling.HandleEventCallback) eventMockCallback1, (ParseQuery) query1, parseObject1);
validateSameObject((SubscriptionHandling.HandleEventCallback) eventMockCallback2, (ParseQuery) query2, parseObject2);
}

@Test
public void testCreateEventWhenSubscribedToCallback() throws Exception {
ParseQuery<ParseObject> parseQuery = new ParseQuery<>("test");
Expand Down Expand Up @@ -370,6 +413,7 @@ private void validateSameObject(SubscriptionHandling.HandleEventCallback<ParseOb

ParseObject newParseObject = objectCaptor.getValue();

assertEquals(originalParseObject.getClassName(), newParseObject.getClassName());
assertEquals(originalParseObject.getObjectId(), newParseObject.getObjectId());
}

Expand Down Expand Up @@ -488,4 +532,12 @@ public void execute(Runnable runnable) {
}
}
}

@ParseClassName("MockA")
static class MockClassA extends ParseObject {
}

@ParseClassName("MockB")
static class MockClassB extends ParseObject {
}
}