22
33package com.openai.models.batches
44
5- import com.fasterxml.jackson.annotation.JsonAnyGetter
6- import com.fasterxml.jackson.annotation.JsonAnySetter
7- import com.fasterxml.jackson.annotation.JsonCreator
8- import com.fasterxml.jackson.annotation.JsonProperty
9- import com.openai.core.ExcludeMissing
10- import com.openai.core.JsonField
11- import com.openai.core.JsonMissing
12- import com.openai.core.JsonValue
13- import com.openai.errors.OpenAIInvalidDataException
5+ import com.openai.core.checkRequired
146import com.openai.services.blocking.BatchService
15- import java.util.Collections
167import java.util.Objects
178import java.util.Optional
189import java.util.stream.Stream
1910import java.util.stream.StreamSupport
2011import kotlin.jvm.optionals.getOrNull
2112
22- /* * List your organization's batches. */
13+ /* * @see [BatchService.list] */
2314class BatchListPage
2415private constructor (
25- private val batchesService : BatchService ,
16+ private val service : BatchService ,
2617 private val params: BatchListParams ,
27- private val response: Response ,
18+ private val response: BatchListPageResponse ,
2819) {
2920
30- fun response (): Response = response
21+ /* *
22+ * Delegates to [BatchListPageResponse], but gracefully handles missing data.
23+ *
24+ * @see [BatchListPageResponse.data]
25+ */
26+ fun data (): List <Batch > = response._data ().getOptional(" data" ).getOrNull() ? : emptyList()
3127
32- fun data (): List <Batch > = response().data()
28+ /* *
29+ * Delegates to [BatchListPageResponse], but gracefully handles missing data.
30+ *
31+ * @see [BatchListPageResponse.hasMore]
32+ */
33+ fun hasMore (): Optional <Boolean > = response._hasMore ().getOptional(" has_more" )
3334
34- fun hasMore (): Optional <Boolean > = response().hasMore()
35-
36- override fun equals (other : Any? ): Boolean {
37- if (this == = other) {
38- return true
39- }
40-
41- return /* spotless:off */ other is BatchListPage && batchesService == other.batchesService && params == other.params && response == other.response /* spotless:on */
42- }
43-
44- override fun hashCode (): Int = /* spotless:off */ Objects .hash(batchesService, params, response) /* spotless:on */
45-
46- override fun toString () =
47- " BatchListPage{batchesService=$batchesService , params=$params , response=$response }"
48-
49- fun hasNextPage (): Boolean {
50- return ! data().isEmpty()
51- }
35+ fun hasNextPage (): Boolean = data().isNotEmpty()
5236
5337 fun getNextPageParams (): Optional <BatchListParams > {
5438 if (! hasNextPage()) {
5539 return Optional .empty()
5640 }
5741
58- return Optional .of(params.toBuilder().after(data().last().id( )).build())
42+ return Optional .of(params.toBuilder().after(data().last()._id ().getOptional( " id " )).build())
5943 }
6044
61- fun getNextPage (): Optional <BatchListPage > {
62- return getNextPageParams().map { batchesService.list(it) }
63- }
45+ fun getNextPage (): Optional <BatchListPage > = getNextPageParams().map { service.list(it) }
6446
6547 fun autoPager (): AutoPager = AutoPager (this )
6648
67- companion object {
68-
69- @JvmStatic
70- fun of (batchesService : BatchService , params : BatchListParams , response : Response ) =
71- BatchListPage (batchesService, params, response)
72- }
73-
74- class Response (
75- private val data : JsonField <List <Batch >>,
76- private val hasMore : JsonField <Boolean >,
77- private val additionalProperties : MutableMap <String , JsonValue >,
78- ) {
79-
80- @JsonCreator
81- private constructor (
82- @JsonProperty(" data" ) data: JsonField <List <Batch >> = JsonMissing .of(),
83- @JsonProperty(" has_more" ) hasMore: JsonField <Boolean > = JsonMissing .of(),
84- ) : this (data, hasMore, mutableMapOf ())
85-
86- fun data (): List <Batch > = data.getOptional(" data" ).getOrNull() ? : listOf ()
87-
88- fun hasMore (): Optional <Boolean > = hasMore.getOptional(" has_more" )
89-
90- @JsonProperty(" data" )
91- fun _data (): Optional <JsonField <List <Batch >>> = Optional .ofNullable(data)
92-
93- @JsonProperty(" has_more" )
94- fun _hasMore (): Optional <JsonField <Boolean >> = Optional .ofNullable(hasMore)
49+ /* * The parameters that were used to request this page. */
50+ fun params (): BatchListParams = params
9551
96- @JsonAnySetter
97- private fun putAdditionalProperty (key : String , value : JsonValue ) {
98- additionalProperties.put(key, value)
99- }
100-
101- @JsonAnyGetter
102- @ExcludeMissing
103- fun _additionalProperties (): Map <String , JsonValue > =
104- Collections .unmodifiableMap(additionalProperties)
52+ /* * The response that this page was parsed from. */
53+ fun response (): BatchListPageResponse = response
10554
106- private var validated: Boolean = false
107-
108- fun validate (): Response = apply {
109- if (validated) {
110- return @apply
111- }
55+ fun toBuilder () = Builder ().from(this )
11256
113- data().map { it.validate() }
114- hasMore()
115- validated = true
116- }
117-
118- fun isValid (): Boolean =
119- try {
120- validate()
121- true
122- } catch (e: OpenAIInvalidDataException ) {
123- false
124- }
125-
126- fun toBuilder () = Builder ().from(this )
127-
128- override fun equals (other : Any? ): Boolean {
129- if (this == = other) {
130- return true
131- }
132-
133- return /* spotless:off */ other is Response && data == other.data && hasMore == other.hasMore && additionalProperties == other.additionalProperties /* spotless:on */
134- }
57+ companion object {
13558
136- override fun hashCode (): Int = /* spotless:off */ Objects .hash(data, hasMore, additionalProperties) /* spotless:on */
59+ /* *
60+ * Returns a mutable builder for constructing an instance of [BatchListPage].
61+ *
62+ * The following fields are required:
63+ * ```java
64+ * .service()
65+ * .params()
66+ * .response()
67+ * ```
68+ */
69+ @JvmStatic fun builder () = Builder ()
70+ }
13771
138- override fun toString () =
139- " Response{data= $data , hasMore= $hasMore , additionalProperties= $additionalProperties } "
72+ /* * A builder for [BatchListPage]. */
73+ class Builder internal constructor() {
14074
141- companion object {
75+ private var service: BatchService ? = null
76+ private var params: BatchListParams ? = null
77+ private var response: BatchListPageResponse ? = null
14278
143- /* * Returns a mutable builder for constructing an instance of [BatchListPage]. */
144- @JvmStatic fun builder () = Builder ()
79+ @JvmSynthetic
80+ internal fun from (batchListPage : BatchListPage ) = apply {
81+ service = batchListPage.service
82+ params = batchListPage.params
83+ response = batchListPage.response
14584 }
14685
147- class Builder {
148-
149- private var data: JsonField <List <Batch >> = JsonMissing .of()
150- private var hasMore: JsonField <Boolean > = JsonMissing .of()
151- private var additionalProperties: MutableMap <String , JsonValue > = mutableMapOf ()
152-
153- @JvmSynthetic
154- internal fun from (page : Response ) = apply {
155- this .data = page.data
156- this .hasMore = page.hasMore
157- this .additionalProperties.putAll(page.additionalProperties)
158- }
159-
160- fun data (data : List <Batch >) = data(JsonField .of(data))
161-
162- fun data (data : JsonField <List <Batch >>) = apply { this .data = data }
163-
164- fun hasMore (hasMore : Boolean ) = hasMore(JsonField .of(hasMore))
165-
166- fun hasMore (hasMore : JsonField <Boolean >) = apply { this .hasMore = hasMore }
167-
168- fun putAdditionalProperty (key : String , value : JsonValue ) = apply {
169- this .additionalProperties.put(key, value)
170- }
171-
172- /* *
173- * Returns an immutable instance of [Response].
174- *
175- * Further updates to this [Builder] will not mutate the returned instance.
176- */
177- fun build (): Response = Response (data, hasMore, additionalProperties.toMutableMap())
178- }
86+ fun service (service : BatchService ) = apply { this .service = service }
87+
88+ /* * The parameters that were used to request this page. */
89+ fun params (params : BatchListParams ) = apply { this .params = params }
90+
91+ /* * The response that this page was parsed from. */
92+ fun response (response : BatchListPageResponse ) = apply { this .response = response }
93+
94+ /* *
95+ * Returns an immutable instance of [BatchListPage].
96+ *
97+ * Further updates to this [Builder] will not mutate the returned instance.
98+ *
99+ * The following fields are required:
100+ * ```java
101+ * .service()
102+ * .params()
103+ * .response()
104+ * ```
105+ *
106+ * @throws IllegalStateException if any required field is unset.
107+ */
108+ fun build (): BatchListPage =
109+ BatchListPage (
110+ checkRequired(" service" , service),
111+ checkRequired(" params" , params),
112+ checkRequired(" response" , response),
113+ )
179114 }
180115
181116 class AutoPager (private val firstPage : BatchListPage ) : Iterable<Batch> {
@@ -196,4 +131,16 @@ private constructor(
196131 return StreamSupport .stream(spliterator(), false )
197132 }
198133 }
134+
135+ override fun equals (other : Any? ): Boolean {
136+ if (this == = other) {
137+ return true
138+ }
139+
140+ return /* spotless:off */ other is BatchListPage && service == other.service && params == other.params && response == other.response /* spotless:on */
141+ }
142+
143+ override fun hashCode (): Int = /* spotless:off */ Objects .hash(service, params, response) /* spotless:on */
144+
145+ override fun toString () = " BatchListPage{service=$service , params=$params , response=$response }"
199146}
0 commit comments