Class ConnectionBuilder
- java.lang.Object
-
- eliasstar.jsonrpc.ConnectionBuilder
-
public final class ConnectionBuilder extends Object
Use this Builder to construct a customizedConnection
instance.To get a
Connection
with default settings, you can useJsonRpc.connect(String)
.- Since:
- 0.1.0
- Author:
- Elias*
-
-
Constructor Summary
Constructors Constructor Description ConnectionBuilder(HttpClient client)
Creates aConnectionBuilder
with the specifiedHttpClient
.ConnectionBuilder(HttpClient client, String url)
Creates aConnectionBuilder
with the specifiedHttpClient
and url.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Connection
build()
Creates aConnection
.ConnectionBuilder
changeClient(HttpClient newClient)
Changes the client used for a newConnection
.ConnectionBuilder
setGson(com.google.gson.Gson gson)
Sets theGsonBuilder
to be used by a createdConnection
.ConnectionBuilder
setGson(com.google.gson.GsonBuilder gson)
Sets the gson builder used for creating the gson instance used by a newConnection
.ConnectionBuilder
setRequestBuilder(HttpRequest.Builder reqBuilder)
Sets the request builder used by a newConnection
.ConnectionBuilder
withId(String id)
Sets the id used by the createdConnection
.ConnectionBuilder
withUrl(String url)
Sets the url of a newConnection
.ConnectionBuilder
withUrl(URI url)
Sets the url of a newConnection
.
-
-
-
Constructor Detail
-
ConnectionBuilder
public ConnectionBuilder(HttpClient client)
Creates aConnectionBuilder
with the specifiedHttpClient
.- Parameters:
client
- The client used a newConnection
-
ConnectionBuilder
public ConnectionBuilder(HttpClient client, String url)
Creates aConnectionBuilder
with the specifiedHttpClient
and url.- Parameters:
client
- The client used a newConnection
url
- 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
Connection
without an id will be created.- Parameters:
id
- The id of the newConnection
- Returns:
this
to 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:
this
to 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:
this
to 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:
this
to 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:
this
to satisfy the builder pattern
-
setGson
public ConnectionBuilder setGson(com.google.gson.Gson gson)
Sets theGsonBuilder
to be used by a createdConnection
.Creates a new
GsonBuilder
with the same settings as theGson
instance.- Parameters:
gson
- TheGson
from which aGsonBuilder
is created- Returns:
this
to 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
- TheGsonBuilder
to be used for a createdConnection
- Returns:
this
to satisfy the builder pattern
-
build
public Connection build()
Creates aConnection
.If no
Gson
orGsonBuilder
was set, a new one is created with default settings.The
"Content-Type"
header is set to"application/json"
on theHttpRequest.Builder
.OptionalTypeAdapterFactory
andRpcTypeAdapterFactory
are registered to theGsonBuilder
instance. Before buildingGsonBuilder.serializeNulls()
is called.- Returns:
- A new
Connection
with the settings of thisConnectionBuilder
- Throws:
IllegalStateException
- If neither aHttpRequest.Builder
nor aURI
was set
-
-