|
| 1 | +package com.stripe; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.Collections; |
| 6 | +import java.util.List; |
| 7 | +import lombok.EqualsAndHashCode; |
| 8 | + |
| 9 | +/** |
| 10 | + * The StripeContext class provides an immutable container for interacting with the `Stripe-Context` |
| 11 | + * header. |
| 12 | + * |
| 13 | + * <p>You can use it whenever you're initializing a `StripeClient` or sending `stripe_context` with |
| 14 | + * a request. It's also found in the `EventNotification.context` property. |
| 15 | + */ |
| 16 | +@EqualsAndHashCode |
| 17 | +public final class StripeContext { |
| 18 | + private final List<String> segments; |
| 19 | + |
| 20 | + /** Creates a new StripeContext with no segments. */ |
| 21 | + public StripeContext() { |
| 22 | + this(null); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Creates a new StripeContext with the specified segments. |
| 27 | + * |
| 28 | + * @param segments the list of context segments |
| 29 | + */ |
| 30 | + public StripeContext(List<String> segments) { |
| 31 | + this.segments = |
| 32 | + segments == null |
| 33 | + ? Collections.emptyList() |
| 34 | + : Collections.unmodifiableList(new ArrayList<>(segments)); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns a new StripeContext with the given segment added to the end. |
| 39 | + * |
| 40 | + * @param segment the segment to add |
| 41 | + * @return a new StripeContext instance with the segment appended |
| 42 | + */ |
| 43 | + public StripeContext push(String segment) { |
| 44 | + List<String> newSegments = new ArrayList<>(this.segments); |
| 45 | + newSegments.add(segment); |
| 46 | + return new StripeContext(newSegments); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns a new StripeContext with the last segment removed. |
| 51 | + * |
| 52 | + * @return a new StripeContext instance with the last segment removed |
| 53 | + */ |
| 54 | + public StripeContext pop() { |
| 55 | + if (segments.isEmpty()) { |
| 56 | + throw new IllegalStateException("Cannot pop from an empty StripeContext"); |
| 57 | + } |
| 58 | + |
| 59 | + List<String> newSegments = new ArrayList<>(this.segments); |
| 60 | + newSegments.remove(newSegments.size() - 1); |
| 61 | + return new StripeContext(newSegments); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Converts the context to a string by joining segments with '/'. |
| 66 | + * |
| 67 | + * @return string representation of the context segments joined by '/', `null` if there are no |
| 68 | + * segments (useful for clearing context) |
| 69 | + */ |
| 70 | + @Override |
| 71 | + public String toString() { |
| 72 | + return String.join("/", segments); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Parse a context string into a StripeContext instance. |
| 77 | + * |
| 78 | + * @param contextStr string to parse (segments separated by '/') |
| 79 | + * @return StripeContext instance with segments from the string |
| 80 | + */ |
| 81 | + public static StripeContext parse(String contextStr) { |
| 82 | + if (contextStr == null || contextStr.isEmpty()) { |
| 83 | + return new StripeContext(); |
| 84 | + } |
| 85 | + |
| 86 | + List<String> segments = Arrays.asList(contextStr.split("/")); |
| 87 | + return new StripeContext(segments); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns an unmodifiable list of the current segments. |
| 92 | + * |
| 93 | + * @return the list of segments |
| 94 | + */ |
| 95 | + public List<String> getSegments() { |
| 96 | + return segments; |
| 97 | + } |
| 98 | +} |
0 commit comments