Class ConnectionBuilder
- java.lang.Object
-
- eliasstar.jsonrpc.ConnectionBuilder
-
public final class ConnectionBuilder extends Object
Use this Builder to construct a customizedConnectioninstance.To get a
Connectionwith default settings, you can useJsonRpc.connect(String).- Since:
- 0.1.0
- Author:
- Elias*
-
-
Constructor Summary
Constructors Constructor Description ConnectionBuilder(HttpClient client)Creates aConnectionBuilderwith the specifiedHttpClient.ConnectionBuilder(HttpClient client, String url)Creates aConnectionBuilderwith the specifiedHttpClientand url.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Connectionbuild()Creates aConnection.ConnectionBuilderchangeClient(HttpClient newClient)Changes the client used for a newConnection.ConnectionBuildersetGson(com.google.gson.Gson gson)Sets theGsonBuilderto be used by a createdConnection.ConnectionBuildersetGson(com.google.gson.GsonBuilder gson)Sets the gson builder used for creating the gson instance used by a newConnection.ConnectionBuildersetRequestBuilder(HttpRequest.Builder reqBuilder)Sets the request builder used by a newConnection.ConnectionBuilderwithId(String id)Sets the id used by the createdConnection.ConnectionBuilderwithUrl(String url)Sets the url of a newConnection.ConnectionBuilderwithUrl(URI url)Sets the url of a newConnection.
-
-
-
Constructor Detail
-
ConnectionBuilder
public ConnectionBuilder(HttpClient client)
Creates aConnectionBuilderwith the specifiedHttpClient.- Parameters:
client- The client used a newConnection
-
ConnectionBuilder
public ConnectionBuilder(HttpClient client, String url)
Creates aConnectionBuilderwith the specifiedHttpClientand url.- Parameters:
client- The client used a newConnectionurl- The url used to create aHttpRequest.Builder
-
-
Method Detail
-
withId
public ConnectionBuilder withId(String id)
Sets the id used by the createdConnection.If null is provided, a
Connectionwithout an id will be created.- Parameters:
id- The id of the newConnection- Returns:
thisto satisfy the builder pattern
-
changeClient
public ConnectionBuilder changeClient(HttpClient newClient)
Changes the client used for a newConnection.- Parameters:
newClient- The client used by a newConnection.- Returns:
thisto satisfy the builder pattern
-
setRequestBuilder
public ConnectionBuilder setRequestBuilder(HttpRequest.Builder reqBuilder)
Sets the request builder used by a newConnection.The builder should already contain an url. If this is not the case, a url may be set afterwards using
withUrl(URI)orwithUrl(String).- Parameters:
reqBuilder- TheHttpRequest.Builder- Returns:
thisto satisfy the builder pattern
-
withUrl
public ConnectionBuilder withUrl(URI url)
Sets the url of a newConnection.If no request builder was set beforehand, a new one with default settings is created.
- Parameters:
url- The service url- Returns:
thisto satisfy the builder pattern
-
withUrl
public ConnectionBuilder withUrl(String url)
Sets the url of a newConnection.If no request builder was set beforehand, a new one with default settings is created.
- Parameters:
url- The service url- Returns:
thisto satisfy the builder pattern
-
setGson
public ConnectionBuilder setGson(com.google.gson.Gson gson)
Sets theGsonBuilderto be used by a createdConnection.Creates a new
GsonBuilderwith the same settings as theGsoninstance.- Parameters:
gson- TheGsonfrom which aGsonBuilderis created- Returns:
thisto satisfy the builder pattern
-
setGson
public ConnectionBuilder setGson(com.google.gson.GsonBuilder gson)
Sets the gson builder used for creating the gson instance used by a newConnection.- Parameters:
gson- TheGsonBuilderto be used for a createdConnection- Returns:
thisto satisfy the builder pattern
-
build
public Connection build()
Creates aConnection.If no
GsonorGsonBuilderwas set, a new one is created with default settings.The
"Content-Type"header is set to"application/json"on theHttpRequest.Builder.OptionalTypeAdapterFactoryandRpcTypeAdapterFactoryare registered to theGsonBuilderinstance. Before buildingGsonBuilder.serializeNulls()is called.- Returns:
- A new
Connectionwith the settings of thisConnectionBuilder - Throws:
IllegalStateException- If neither aHttpRequest.Buildernor aURIwas set
-
-