Skip to content

Commit 7acf121

Browse files
refactor(client): migrate pages to builder pattern
1 parent 8fcf7b5 commit 7acf121

File tree

76 files changed

+3709
-1843
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+3709
-1843
lines changed

orb-java-core/src/main/kotlin/com/withorb/api/models/AlertListPage.kt

Lines changed: 79 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,22 @@
22

33
package com.withorb.api.models
44

5+
import com.withorb.api.core.checkRequired
56
import com.withorb.api.services.blocking.AlertService
67
import java.util.Objects
78
import java.util.Optional
89
import java.util.stream.Stream
910
import java.util.stream.StreamSupport
1011
import kotlin.jvm.optionals.getOrNull
1112

12-
/**
13-
* This endpoint returns a list of alerts within Orb.
14-
*
15-
* The request must specify one of `customer_id`, `external_customer_id`, or `subscription_id`.
16-
*
17-
* If querying by subscripion_id, the endpoint will return the subscription level alerts as well as
18-
* the plan level alerts associated with the subscription.
19-
*
20-
* The list of alerts is ordered starting from the most recently created alert. This endpoint
21-
* follows Orb's [standardized pagination format](/api-reference/pagination).
22-
*/
13+
/** @see [AlertService.list] */
2314
class AlertListPage
2415
private constructor(
25-
private val alertsService: AlertService,
16+
private val service: AlertService,
2617
private val params: AlertListParams,
2718
private val response: AlertListPageResponse,
2819
) {
2920

30-
/** Returns the response that this page was parsed from. */
31-
fun response(): AlertListPageResponse = response
32-
3321
/**
3422
* Delegates to [AlertListPageResponse], but gracefully handles missing data.
3523
*
@@ -45,19 +33,6 @@ private constructor(
4533
fun paginationMetadata(): Optional<PaginationMetadata> =
4634
response._paginationMetadata().getOptional("pagination_metadata")
4735

48-
override fun equals(other: Any?): Boolean {
49-
if (this === other) {
50-
return true
51-
}
52-
53-
return /* spotless:off */ other is AlertListPage && alertsService == other.alertsService && params == other.params && response == other.response /* spotless:on */
54-
}
55-
56-
override fun hashCode(): Int = /* spotless:off */ Objects.hash(alertsService, params, response) /* spotless:on */
57-
58-
override fun toString() =
59-
"AlertListPage{alertsService=$alertsService, params=$params, response=$response}"
60-
6136
fun hasNextPage(): Boolean =
6237
data().isNotEmpty() &&
6338
paginationMetadata().flatMap { it._nextCursor().getOptional("next_cursor") }.isPresent
@@ -79,20 +54,75 @@ private constructor(
7954
)
8055
}
8156

82-
fun getNextPage(): Optional<AlertListPage> {
83-
return getNextPageParams().map { alertsService.list(it) }
84-
}
57+
fun getNextPage(): Optional<AlertListPage> = getNextPageParams().map { service.list(it) }
8558

8659
fun autoPager(): AutoPager = AutoPager(this)
8760

61+
/** The parameters that were used to request this page. */
62+
fun params(): AlertListParams = params
63+
64+
/** The response that this page was parsed from. */
65+
fun response(): AlertListPageResponse = response
66+
67+
fun toBuilder() = Builder().from(this)
68+
8869
companion object {
8970

90-
@JvmStatic
91-
fun of(
92-
alertsService: AlertService,
93-
params: AlertListParams,
94-
response: AlertListPageResponse,
95-
) = AlertListPage(alertsService, params, response)
71+
/**
72+
* Returns a mutable builder for constructing an instance of [AlertListPage].
73+
*
74+
* The following fields are required:
75+
* ```java
76+
* .service()
77+
* .params()
78+
* .response()
79+
* ```
80+
*/
81+
@JvmStatic fun builder() = Builder()
82+
}
83+
84+
/** A builder for [AlertListPage]. */
85+
class Builder internal constructor() {
86+
87+
private var service: AlertService? = null
88+
private var params: AlertListParams? = null
89+
private var response: AlertListPageResponse? = null
90+
91+
@JvmSynthetic
92+
internal fun from(alertListPage: AlertListPage) = apply {
93+
service = alertListPage.service
94+
params = alertListPage.params
95+
response = alertListPage.response
96+
}
97+
98+
fun service(service: AlertService) = apply { this.service = service }
99+
100+
/** The parameters that were used to request this page. */
101+
fun params(params: AlertListParams) = apply { this.params = params }
102+
103+
/** The response that this page was parsed from. */
104+
fun response(response: AlertListPageResponse) = apply { this.response = response }
105+
106+
/**
107+
* Returns an immutable instance of [AlertListPage].
108+
*
109+
* Further updates to this [Builder] will not mutate the returned instance.
110+
*
111+
* The following fields are required:
112+
* ```java
113+
* .service()
114+
* .params()
115+
* .response()
116+
* ```
117+
*
118+
* @throws IllegalStateException if any required field is unset.
119+
*/
120+
fun build(): AlertListPage =
121+
AlertListPage(
122+
checkRequired("service", service),
123+
checkRequired("params", params),
124+
checkRequired("response", response),
125+
)
96126
}
97127

98128
class AutoPager(private val firstPage: AlertListPage) : Iterable<Alert> {
@@ -113,4 +143,16 @@ private constructor(
113143
return StreamSupport.stream(spliterator(), false)
114144
}
115145
}
146+
147+
override fun equals(other: Any?): Boolean {
148+
if (this === other) {
149+
return true
150+
}
151+
152+
return /* spotless:off */ other is AlertListPage && service == other.service && params == other.params && response == other.response /* spotless:on */
153+
}
154+
155+
override fun hashCode(): Int = /* spotless:off */ Objects.hash(service, params, response) /* spotless:on */
156+
157+
override fun toString() = "AlertListPage{service=$service, params=$params, response=$response}"
116158
}

orb-java-core/src/main/kotlin/com/withorb/api/models/AlertListPageAsync.kt

Lines changed: 82 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.withorb.api.models
44

5+
import com.withorb.api.core.checkRequired
56
import com.withorb.api.services.async.AlertServiceAsync
67
import java.util.Objects
78
import java.util.Optional
@@ -10,27 +11,14 @@ import java.util.concurrent.Executor
1011
import java.util.function.Predicate
1112
import kotlin.jvm.optionals.getOrNull
1213

13-
/**
14-
* This endpoint returns a list of alerts within Orb.
15-
*
16-
* The request must specify one of `customer_id`, `external_customer_id`, or `subscription_id`.
17-
*
18-
* If querying by subscripion_id, the endpoint will return the subscription level alerts as well as
19-
* the plan level alerts associated with the subscription.
20-
*
21-
* The list of alerts is ordered starting from the most recently created alert. This endpoint
22-
* follows Orb's [standardized pagination format](/api-reference/pagination).
23-
*/
14+
/** @see [AlertServiceAsync.list] */
2415
class AlertListPageAsync
2516
private constructor(
26-
private val alertsService: AlertServiceAsync,
17+
private val service: AlertServiceAsync,
2718
private val params: AlertListParams,
2819
private val response: AlertListPageResponse,
2920
) {
3021

31-
/** Returns the response that this page was parsed from. */
32-
fun response(): AlertListPageResponse = response
33-
3422
/**
3523
* Delegates to [AlertListPageResponse], but gracefully handles missing data.
3624
*
@@ -46,19 +34,6 @@ private constructor(
4634
fun paginationMetadata(): Optional<PaginationMetadata> =
4735
response._paginationMetadata().getOptional("pagination_metadata")
4836

49-
override fun equals(other: Any?): Boolean {
50-
if (this === other) {
51-
return true
52-
}
53-
54-
return /* spotless:off */ other is AlertListPageAsync && alertsService == other.alertsService && params == other.params && response == other.response /* spotless:on */
55-
}
56-
57-
override fun hashCode(): Int = /* spotless:off */ Objects.hash(alertsService, params, response) /* spotless:on */
58-
59-
override fun toString() =
60-
"AlertListPageAsync{alertsService=$alertsService, params=$params, response=$response}"
61-
6237
fun hasNextPage(): Boolean =
6338
data().isNotEmpty() &&
6439
paginationMetadata().flatMap { it._nextCursor().getOptional("next_cursor") }.isPresent
@@ -80,22 +55,78 @@ private constructor(
8055
)
8156
}
8257

83-
fun getNextPage(): CompletableFuture<Optional<AlertListPageAsync>> {
84-
return getNextPageParams()
85-
.map { alertsService.list(it).thenApply { Optional.of(it) } }
58+
fun getNextPage(): CompletableFuture<Optional<AlertListPageAsync>> =
59+
getNextPageParams()
60+
.map { service.list(it).thenApply { Optional.of(it) } }
8661
.orElseGet { CompletableFuture.completedFuture(Optional.empty()) }
87-
}
8862

8963
fun autoPager(): AutoPager = AutoPager(this)
9064

65+
/** The parameters that were used to request this page. */
66+
fun params(): AlertListParams = params
67+
68+
/** The response that this page was parsed from. */
69+
fun response(): AlertListPageResponse = response
70+
71+
fun toBuilder() = Builder().from(this)
72+
9173
companion object {
9274

93-
@JvmStatic
94-
fun of(
95-
alertsService: AlertServiceAsync,
96-
params: AlertListParams,
97-
response: AlertListPageResponse,
98-
) = AlertListPageAsync(alertsService, params, response)
75+
/**
76+
* Returns a mutable builder for constructing an instance of [AlertListPageAsync].
77+
*
78+
* The following fields are required:
79+
* ```java
80+
* .service()
81+
* .params()
82+
* .response()
83+
* ```
84+
*/
85+
@JvmStatic fun builder() = Builder()
86+
}
87+
88+
/** A builder for [AlertListPageAsync]. */
89+
class Builder internal constructor() {
90+
91+
private var service: AlertServiceAsync? = null
92+
private var params: AlertListParams? = null
93+
private var response: AlertListPageResponse? = null
94+
95+
@JvmSynthetic
96+
internal fun from(alertListPageAsync: AlertListPageAsync) = apply {
97+
service = alertListPageAsync.service
98+
params = alertListPageAsync.params
99+
response = alertListPageAsync.response
100+
}
101+
102+
fun service(service: AlertServiceAsync) = apply { this.service = service }
103+
104+
/** The parameters that were used to request this page. */
105+
fun params(params: AlertListParams) = apply { this.params = params }
106+
107+
/** The response that this page was parsed from. */
108+
fun response(response: AlertListPageResponse) = apply { this.response = response }
109+
110+
/**
111+
* Returns an immutable instance of [AlertListPageAsync].
112+
*
113+
* Further updates to this [Builder] will not mutate the returned instance.
114+
*
115+
* The following fields are required:
116+
* ```java
117+
* .service()
118+
* .params()
119+
* .response()
120+
* ```
121+
*
122+
* @throws IllegalStateException if any required field is unset.
123+
*/
124+
fun build(): AlertListPageAsync =
125+
AlertListPageAsync(
126+
checkRequired("service", service),
127+
checkRequired("params", params),
128+
checkRequired("response", response),
129+
)
99130
}
100131

101132
class AutoPager(private val firstPage: AlertListPageAsync) {
@@ -123,4 +154,17 @@ private constructor(
123154
return forEach(values::add, executor).thenApply { values }
124155
}
125156
}
157+
158+
override fun equals(other: Any?): Boolean {
159+
if (this === other) {
160+
return true
161+
}
162+
163+
return /* spotless:off */ other is AlertListPageAsync && service == other.service && params == other.params && response == other.response /* spotless:on */
164+
}
165+
166+
override fun hashCode(): Int = /* spotless:off */ Objects.hash(service, params, response) /* spotless:on */
167+
168+
override fun toString() =
169+
"AlertListPageAsync{service=$service, params=$params, response=$response}"
126170
}

0 commit comments

Comments
 (0)