• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-java / #16493

03 Oct 2024 07:15PM UTC coverage: 12.942% (+0.08%) from 12.864%
#16493

push

github

web-flow
Merge Stripe-java v27.0.0 to beta branch (#1888)

409 of 1651 new or added lines in 88 files covered. (24.77%)

31 existing lines in 7 files now uncovered.

18773 of 145050 relevant lines covered (12.94%)

0.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.5
/src/main/java/com/stripe/model/v2/StripeCollection.java
1
package com.stripe.model.v2;
2

3
import com.google.gson.annotations.SerializedName;
4
import com.stripe.exception.StripeException;
5
import com.stripe.model.StripeActiveObject;
6
import com.stripe.model.StripeObject;
7
import com.stripe.model.StripeObjectInterface;
8
import com.stripe.net.*;
9
import java.lang.reflect.Type;
10
import java.util.HashMap;
11
import java.util.Iterator;
12
import java.util.List;
13
import lombok.EqualsAndHashCode;
14
import lombok.Getter;
15
import lombok.Setter;
16

17
/**
18
 * Provides a representation of a single page worth of data from the Stripe API.
19
 *
20
 * <p>The following code will have the effect of iterating through a single page worth of invoice
21
 * data retrieve from the API:
22
 *
23
 * <p>
24
 *
25
 * <pre>{@code
26
 * foreach (Invoice invoice : Invoice.list(...).getData()) {
27
 *   System.out.println("Current invoice = " + invoice.toString());
28
 * }
29
 * }</pre>
30
 *
31
 * <p>The class also provides a helper for iterating over collections that may be longer than a
32
 * single page:
33
 *
34
 * <p>
35
 *
36
 * <pre>{@code
37
 * foreach (Invoice invoice : Invoice.list(...).autoPagingIterable()) {
38
 *   System.out.println("Current invoice = " + invoice.toString());
39
 * }
40
 * }</pre>
41
 */
42
@EqualsAndHashCode(callSuper = false)
43
public class StripeCollection<T extends StripeObjectInterface> extends StripeObject
1✔
44
    implements StripeActiveObject {
45
  private transient StripeResponseGetter responseGetter;
46

47
  @Setter private transient Type pageTypeToken;
48

49
  @Getter
50
  @SerializedName("data")
51
  List<T> data;
52

53
  @Getter
54
  @SerializedName("next_page_url")
55
  String nextPageUrl;
56

57
  @Getter
58
  @SerializedName("previous_page_url")
59
  String previousPageUrl;
60

61
  @Getter @Setter private transient RequestOptions requestOptions;
62

63
  private static class Page<T> {
64
    List<T> data;
65
    String nextPageUrl;
66

67
    Page(List<T> data, String nextPageUrl) {
1✔
68
      this.data = data;
1✔
69
      this.nextPageUrl = nextPageUrl;
1✔
70
    }
1✔
71
  }
72

73
  /**
74
   * An Iterable implementation that starts from the StripeCollection data and will fetch next pages
75
   * automatically.
76
   */
77
  private class PagingIterable implements Iterable<T> {
78
    RequestOptions options;
79

80
    public PagingIterable() {
1✔
81
      this.options = StripeCollection.this.getRequestOptions();
1✔
82
    }
1✔
83

84
    public PagingIterable(RequestOptions options) {
1✔
85
      this.options = options;
1✔
86
    }
1✔
87

88
    private Page<T> getPage(String nextPageUrl) throws StripeException {
89
      if (nextPageUrl == null) {
1✔
NEW
90
        throw new IllegalArgumentException("nextPageUrl cannot be null");
×
91
      }
92

93
      StripeCollection<T> response =
1✔
94
          StripeCollection.this.responseGetter.request(
1✔
95
              new ApiRequest(
96
                  BaseAddress.API,
97
                  ApiResource.RequestMethod.GET,
98
                  nextPageUrl,
99
                  new HashMap<>(),
100
                  this.options),
101
              StripeCollection.this.pageTypeToken);
1✔
102
      return new Page<T>(response.getData(), response.getNextPageUrl());
1✔
103
    }
104

105
    @Override
106
    public Iterator<T> iterator() {
107
      return new PagingIterator(
1✔
108
          StripeCollection.this.getData(), StripeCollection.this.getNextPageUrl());
1✔
109
    }
110

111
    private class PagingIterator implements Iterator<T> {
112
      Iterator<T> currentDataIterator;
113
      String nextPageUrl;
114

115
      public PagingIterator(List<T> currentPage, String nextPageUrl) {
1✔
116
        this.currentDataIterator = currentPage.iterator();
1✔
117
        this.nextPageUrl = nextPageUrl;
1✔
118
      }
1✔
119

120
      @Override
121
      public T next() {
122
        if (!currentDataIterator.hasNext() && this.nextPageUrl != null) {
1✔
123
          try {
124
            Page<T> p = PagingIterable.this.getPage(this.nextPageUrl);
1✔
125
            this.currentDataIterator = p.data.iterator();
1✔
126
            this.nextPageUrl = p.nextPageUrl;
1✔
NEW
127
          } catch (final Exception e) {
×
NEW
128
            throw new RuntimeException("Unable to paginate", e);
×
129
          }
1✔
130
        }
131
        return this.currentDataIterator.next();
1✔
132
      }
133

134
      @Override
135
      public boolean hasNext() {
136
        return this.currentDataIterator.hasNext() || this.nextPageUrl != null;
1✔
137
      }
138
    }
139
  }
140

141
  /**
142
   * Constructs an iterable that can be used to iterate across all objects across all pages. As page
143
   * boundaries are encountered, the next page will be fetched automatically for continued
144
   * iteration.
145
   *
146
   * <p>This utilizes the options from the initial list request.
147
   */
148
  public Iterable<T> autoPagingIterable() {
149
    return new PagingIterable();
1✔
150
  }
151

152
  /**
153
   * Constructs an iterable that can be used to iterate across all objects across all pages. As page
154
   * boundaries are encountered, the next page will be fetched automatically for continued
155
   * iteration.
156
   *
157
   * @param options request options (will override the options from the initial list request)
158
   */
159
  public Iterable<T> autoPagingIterable(RequestOptions options) {
160
    return new PagingIterable(options);
1✔
161
  }
162

163
  @Override
164
  public void setResponseGetter(StripeResponseGetter responseGetter) {
165
    this.responseGetter = responseGetter;
1✔
166

167
    if (this.data != null) {
1✔
168
      for (T item : data) {
1✔
169
        trySetResponseGetter(item, responseGetter);
1✔
170
      }
1✔
171
    }
172
  }
1✔
173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc