More Java Docs (#3138)

This commit is contained in:
Mikael Mengistu 2018-10-19 14:31:05 -07:00 committed by GitHub
parent d82a3e6c99
commit 95ccb1ee52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 201 additions and 1 deletions

View File

@ -3,6 +3,9 @@
package com.microsoft.signalr;
/**
* A callback that takes no parameters.
*/
@FunctionalInterface
public interface Action {
void invoke();

View File

@ -3,6 +3,11 @@
package com.microsoft.signalr;
/**
* A callback that takes one parameter.
*
* @param <T1> The type of the first parameter to the callback.
*/
@FunctionalInterface
public interface Action1<T1> {
void invoke(T1 param1);

View File

@ -3,6 +3,12 @@
package com.microsoft.signalr;
/**
* A callback that takes two parameters.
*
* @param <T1> The type of the first parameter to the callback.
* @param <T2> The type of the second parameter to the callback.
*/
@FunctionalInterface
public interface Action2<T1, T2> {
void invoke(T1 param1, T2 param2);

View File

@ -3,6 +3,13 @@
package com.microsoft.signalr;
/**
* A callback that takes three parameters.
*
* @param <T1> The type of the first parameter to the callback.
* @param <T2> The type of the second parameter to the callback.
* @param <T3> The type of the third parameter to the callback.
*/
@FunctionalInterface
public interface Action3<T1, T2, T3> {
void invoke(T1 param1, T2 param2, T3 param3);

View File

@ -3,6 +3,14 @@
package com.microsoft.signalr;
/**
* A callback that takes four parameters.
*
* @param <T1> The type of the first parameter to the callback.
* @param <T2> The type of the second parameter to the callback.
* @param <T3> The type of the third parameter to the callback.
* @param <T4> The type of the fourth parameter to the callback.
*/
@FunctionalInterface
public interface Action4<T1, T2, T3, T4> {
void invoke(T1 param1, T2 param2, T3 param3, T4 param4);

View File

@ -3,6 +3,15 @@
package com.microsoft.signalr;
/**
* A callback that takes five parameter.
*
* @param <T1> The type of the first parameter to the callback.
* @param <T2> The type of the second parameter to the callback.
* @param <T3> The type of the third parameter to the callback.
* @param <T4> The type of the fourth parameter to the callback.
* @param <T5> The type of the fifth parameter to the callback.
*/
@FunctionalInterface
public interface Action5<T1, T2, T3, T4, T5> {
void invoke(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5);

View File

@ -3,6 +3,16 @@
package com.microsoft.signalr;
/**
* A callback that takes six parameters.
*
* @param <T1> The type of the first parameter to the callback.
* @param <T2> The type of the second parameter to the callback.
* @param <T3> The type of the third parameter to the callback.
* @param <T4> The type of the fourth parameter to the callback.
* @param <T5> The type of the fifth parameter to the callback.
* @param <T6> The type of the sixth parameter to the callback.
*/
@FunctionalInterface
public interface Action6<T1, T2, T3, T4, T5, T6> {
void invoke(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6);

View File

@ -3,6 +3,17 @@
package com.microsoft.signalr;
/**
* A callback that takes seven parameters.
*
* @param <T1> The type of the first parameter to the callback.
* @param <T2> The type of the second parameter to the callback.
* @param <T3> The type of the third parameter to the callback.
* @param <T4> The type of the fourth parameter to the callback.
* @param <T5> The type of the fifth parameter to the callback.
* @param <T6> The type of the sixth parameter to the callback.
* @param <T7> The type of the seventh parameter to the callback.
*/
@FunctionalInterface
public interface Action7<T1, T2, T3, T4, T5, T6, T7> {
void invoke(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7);

View File

@ -3,6 +3,18 @@
package com.microsoft.signalr;
/**
* A callback that takes eight parameters.
*
* @param <T1> The type of the first parameter to the callback.
* @param <T2> The type of the second parameter to the callback.
* @param <T3> The type of the third parameter to the callback.
* @param <T4> The type of the fourth parameter to the callback.
* @param <T5> The type of the fifth parameter to the callback.
* @param <T6> The type of the sixth parameter to the callback.
* @param <T7> The type of the seventh parameter to the callback.
* @param <T8> The type of the eighth parameter to the callback.
*/
@FunctionalInterface
public interface Action8<T1, T2, T3, T4, T5, T6, T7, T8> {
void invoke(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8);

View File

@ -9,6 +9,9 @@ import java.util.Map;
import io.reactivex.Single;
/**
* A builder for configuring {@link HubConnection} instances.
*/
public class HttpHubConnectionBuilder {
private final String url;
private Transport transport;
@ -22,36 +25,80 @@ public class HttpHubConnectionBuilder {
this.url = url;
}
/**
* Sets the transport to be used by the {@link HubConnection}.
*
* @param transport The transport to be used.
* @return This instance of the HttpHubConnectionBuilder.
*/
HttpHubConnectionBuilder withTransport(Transport transport) {
this.transport = transport;
return this;
}
/**
* Sets the {@link HttpClient} to be used by the {@link HubConnection}.
*
* @param httpClient The {@link HttpClient} to be used by the {@link HubConnection}.
* @return This instance of the HttpHubConnectionBuilder.
*/
HttpHubConnectionBuilder withHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}
/**
* Indicates to the {@link HubConnection} that it should skip the negotiate process.
* Note: This option only works with the Websockets transport and the Azure SignalR Service require the negotiate step.
*
* @param skipNegotiate Boolean indicating if the {@link HubConnection} should skip the negotiate step.
* @return This instance of the HttpHubConnectionBuilder.
*/
public HttpHubConnectionBuilder shouldSkipNegotiate(boolean skipNegotiate) {
this.skipNegotiate = skipNegotiate;
return this;
}
/**
* Sets the access token provider for the {@link HubConnection}.
*
* @param accessTokenProvider The access token provider to be used by the {@link HubConnection}.
* @return This instance of the HttpHubConnectionBuilder.
*/
public HttpHubConnectionBuilder withAccessTokenProvider(Single<String> accessTokenProvider) {
this.accessTokenProvider = accessTokenProvider;
return this;
}
/**
* Sets the duration the {@link HubConnection} should wait for a Handshake Response from the server.
*
* @param timeout The duration that the {@link HubConnection} should wait for a Handshake Response from the server.
* @return This instance of the HttpHubConnectionBuilder.
*/
public HttpHubConnectionBuilder withHandshakeResponseTimeout(Duration timeout) {
this.handshakeResponseTimeout = timeout;
return this;
}
/**
* Sets a collection of Headers for the {@link HubConnection} to send with every Http request.
*
* @param headers A Map representing the collection of Headers that the {@link HubConnection} should send.
* @return This instance of the HttpHubConnectionBuilder.
*/
public HttpHubConnectionBuilder withHeaders(Map<String, String> headers) {
this.headers = headers;
return this;
}
/**
* Sets a single header for the {@link HubConnection} to send.
*
* @param name The name of the header to set.
* @param value The value of the header to be set.
* @return This instance of the HttpHubConnectionBuilder.
*/
public HttpHubConnectionBuilder withHeader(String name, String value) {
if (headers == null) {
this.headers = new HashMap<>();
@ -60,6 +107,11 @@ public class HttpHubConnectionBuilder {
return this;
}
/**
* Builds a new instance of {@link HubConnection}.
*
* @return A new instance of {@link HubConnection}.
*/
public HubConnection build() {
return new HubConnection(url, transport, skipNegotiate, httpClient, accessTokenProvider, handshakeResponseTimeout, headers);
}

View File

@ -55,18 +55,38 @@ public class HubConnection {
private final Logger logger = LoggerFactory.getLogger(HubConnection.class);
/**
* Sets the server timeout interval for the connection.
*
* @param serverTimeout The server timeout duration.
*/
public void setServerTimeout(Duration serverTimeout) {
this.serverTimeout = serverTimeout;
}
/**
* Gets the server timeout duration.
*
* @return The server timeout duration.
*/
public Duration getServerTimeout() {
return this.serverTimeout;
}
/**
* Sets the keep alive interval duration.
*
* @param keepAliveInterval The interval at which the connection should send keep alive messages.
*/
public void setKeepAliveInterval(Duration keepAliveInterval) {
this.keepAliveInterval = keepAliveInterval;
}
/**
* Gets the keep alive interval.
*
* @return The interval between keep alive messages.
*/
public Duration getKeepAliveInterval() {
return this.keepAliveInterval;
}
@ -339,6 +359,7 @@ public class HubConnection {
/**
* Stops a connection to the server.
*
* @param errorMessage An error message if the connected needs to be stopped because of an error.
* @return A Completable that completes when the connection has been stopped.
*/
@ -364,6 +385,7 @@ public class HubConnection {
/**
* Stops a connection to the server.
*
* @return A Completable that completes when the connection has been stopped.
*/
public Completable stop() {
@ -416,6 +438,15 @@ public class HubConnection {
sendHubMessage(invocationMessage);
}
/**
* Invokes a hub method on the server using the specified method name and arguments.
*
* @param returnType The expected return type.
* @param method The name of the server method to invoke.
* @param args The arguments used to invoke the server method.
* @param <T> The expected return type.
* @return A Single that yields the return value when the invocation has completed.
*/
@SuppressWarnings("unchecked")
public <T> Single<T> invoke(Class<T> returnType, String method, Object... args) {
String id = connectionState.getNextInvocationId();
@ -478,6 +509,11 @@ public class HubConnection {
logger.trace("Removing handlers for client method: {}.", name);
}
/**
* Registers a callback to run when the connection is closed.
*
* @param callback A callback to run when the connection closes.
*/
public void onClosed(Consumer<Exception> callback) {
if (onClosedCallbackList == null) {
onClosedCallbackList = new ArrayList<>();

View File

@ -3,7 +3,16 @@
package com.microsoft.signalr;
/**
* A builder for configuring {@link HubConnection} instances.
*/
public abstract class HubConnectionBuilder {
/**
* Creates a new instance of {@link HttpHubConnectionBuilder}.
*
* @param url The URL of the SignalR hub to connect to.
* @return An instance of {@link HttpHubConnectionBuilder}.
*/
public static HttpHubConnectionBuilder create(String url) {
if (url == null || url.isEmpty()) {
throw new IllegalArgumentException("A valid url is required.");
@ -11,5 +20,10 @@ public abstract class HubConnectionBuilder {
return new HttpHubConnectionBuilder(url);
}
/**
* Builds a new instance of {@link HubConnection}.
*
* @return A new instance of {@link HubConnection}.
*/
public abstract HubConnection build();
}

View File

@ -3,6 +3,9 @@
package com.microsoft.signalr;
/**
* Indicates the state of the {@link HubConnection}.
*/
public enum HubConnectionState {
CONNECTED,
DISCONNECTED,

View File

@ -3,16 +3,34 @@
package com.microsoft.signalr;
/**
* An exception thrown when the server fails to invoke a Hub method.
*/
public class HubException extends RuntimeException {
private static final long serialVersionUID = -572019264269821519L;
/**
* Initializes a new instance of the {@link HubException} class.
*/
public HubException() {
}
/**
* Initializes a new instance of the {@link HubException} class with a specified error message.
*
* @param errorMessage The error message that explains the reason for the exception.
*/
public HubException(String errorMessage) {
super(errorMessage);
}
/**
* Initializes a new instance of the {@link HubException} class with a specified error message and a reference
* to the exception that is the cause of this exception.
*
* @param errorMessage The error message that explains the reason for the exception.
* @param innerException The exception that is the cause of the current exception, or null if no inner exception is specified.
*/
public HubException(String errorMessage, Exception innerException) {
super(errorMessage, innerException);
}

View File

@ -5,17 +5,23 @@ package com.microsoft.signalr;
import java.util.List;
/**
* Represents the registration of a handler for a client method.
*/
public class Subscription {
private final CallbackMap handlers;
private final InvocationHandler handler;
private final String target;
public Subscription(CallbackMap handlers, InvocationHandler handler, String target) {
Subscription(CallbackMap handlers, InvocationHandler handler, String target) {
this.handlers = handlers;
this.handler = handler;
this.target = target;
}
/**
* Removes the client method handler represented by this subscription.
*/
public void unsubscribe() {
List<InvocationHandler> handler = this.handlers.get(target);
if (handler != null) {