<commit_before>package com.gocardless.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gocardless.http.*;
import com.gocardless.resources.Subscription;

import com.google.common.collect.ImmutableMap;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;

/**
 * Service class for working with subscription resources.
 *
 * Subscriptions create [payments](#core-endpoints-payments) according to a schedule.
 * 
 * ###
 * Recurrence Rules
 * 
 * The following rules apply when specifying recurrence:
 * 
 * - The first
 * payment must be charged within 1 year.
 * - When neither `month` nor `day_of_month` are present,
 * the subscription will recur from the `start_date` based on the `interval_unit`.
 * - If `month` or
 * `day_of_month` are present, the recurrence rules will be applied from the `start_date`, and the
 * following validations apply:
 * 
 * | interval_unit   | month                                       
 *   | day_of_month                            |
 * | :-------------- |
 * :--------------------------------------------- | :-------------------------------------- |
 * |
 * yearly          | optional (required if `day_of_month` provided) | optional (required if `month`
 * provided) |
 * | monthly         | invalid                                        | required       
 *                         |
 * | weekly          | invalid                                        |
 * invalid                                 |
 * 
 * Examples:
 * 
 * | interval_unit   | interval   |
 * month   | day_of_month   | valid?                                             |
 * |
 * :-------------- | :--------- | :------ | :------------- |
 * :------------------------------------------------- |
 * | yearly          | 1          | january |
 * -1             | valid                                              |
 * | yearly          | 1     
 *     | march   |                | invalid - missing `day_of_month`                   |
 * | monthly 
 *        | 6          |         | 12             | valid                                            
 *  |
 * | monthly         | 6          | august  | 12             | invalid - `month` must be blank  
 *                  |
 * | weekly          | 2          |         |                | valid            
 *                                  |
 * | weekly          | 2          | october | 10             |
 * invalid - `month` and `day_of_month` must be blank |
 * 
 * ### Rolling dates
 * 
 * When a charge
 * date falls on a non-business day, one of two things will happen:
 * 
 * - if the recurrence rule
 * specified `-1` as the `day_of_month`, the charge date will be rolled __backwards__ to the previous
 * business day (i.e., the last working day of the month).
 * - otherwise the charge date will be
 * rolled __forwards__ to the next business day.
 * 
 */
public class SubscriptionService {
    private final HttpClient httpClient;

    /**
     * Constructor.  Users of this library should have no need to call this - an instance
     * of this class can be obtained by calling
      {@link com.gocardless.GoCardlessClient#subscriptions() }.
     */
    public SubscriptionService(HttpClient httpClient) {
        this.httpClient = httpClient;
    }

    /**
     * Creates a new subscription object
     */
    public SubscriptionCreateRequest create() {
        return new SubscriptionCreateRequest(httpClient);
    }

    /**
     * Returns a [cursor-paginated](#api-usage-cursor-pagination) list of your subscriptions.
     */
    public SubscriptionListRequest<ListResponse<Subscription>> list() {
        return new SubscriptionListRequest<>(httpClient, ListRequest.<Subscription>pagingExecutor());
    }

    public SubscriptionListRequest<Iterable<Subscription>> all() {
        return new SubscriptionListRequest<>(httpClient,
                ListRequest.<Subscription>iteratingExecutor());
    }

    /**
     * Retrieves the details of a single subscription.
     */
    public SubscriptionGetRequest get(String identity) {
        return new SubscriptionGetRequest(httpClient, identity);
    }

    /**
     * Updates a subscription object.
     */
    public SubscriptionUpdateRequest update(String identity) {
        return new SubscriptionUpdateRequest(httpClient, identity);
    }

    /**
     * Immediately cancels a subscription; no more payments will be created under it. Any metadata
     * supplied to this endpoint will be stored on the payment cancellation event it causes.
     * 
     * This
     * will fail with a cancellation_failed error if the subscription is already cancelled or finished.
     */
    public SubscriptionCancelRequest cancel(String identity) {
        return new SubscriptionCancelRequest(httpClient, identity);
    }

    /**
     * Request class for {@link SubscriptionService#create }.
     *
     * Creates a new subscription object
     */
    public static final class SubscriptionCreateRequest extends IdempotentPostRequest<Subscription> {
        private Integer amount;
        private Integer count;
        private String currency;
        private Integer dayOfMonth;
        private String endDate;
        private Integer interval;
        private IntervalUnit intervalUnit;
        private Links links;
        private Map<String, String> metadata;
        private Month month;
        private String name;
        private String paymentReference;
        private String startDate;

        /**
         * Amount in pence or cents.
         */
        public SubscriptionCreateRequest withAmount(Integer amount) {
            this.amount = amount;
            return this;
        }

        /**
         * An alternative way to set `end_date`. The total number of payments that should be taken by this
         * subscription. This will set `end_date` automatically.<commit_msg>Crankshaft[BU000003N1T1PS] Java client @ 2017-03-14 12:45:07 +0000<commit_after>package com.gocardless.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gocardless.http.*;
import com.gocardless.resources.Subscription;

import com.google.common.collect.ImmutableMap;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;

/**
 * Service class for working with subscription resources.
 *
 * Subscriptions create [payments](#core-endpoints-payments) according to a schedule.
 * 
 * ###
 * Recurrence Rules
 * 
 * The following rules apply when specifying recurrence:
 * 
 * - The first
 * payment must be charged within 1 year.
 * - When neither `month` nor `day_of_month` are present,
 * the subscription will recur from the `start_date` based on the `interval_unit`.
 * - If `month` or
 * `day_of_month` are present, the recurrence rules will be applied from the `start_date`, and the
 * following validations apply:
 * 
 * | interval_unit   | month                                       
 *   | day_of_month                            |
 * | :-------------- |
 * :--------------------------------------------- | :-------------------------------------- |
 * |
 * yearly          | optional (required if `day_of_month` provided) | optional (required if `month`
 * provided) |
 * | monthly         | invalid                                        | required       
 *                         |
 * | weekly          | invalid                                        |
 * invalid                                 |
 * 
 * Examples:
 * 
 * | interval_unit   | interval   |
 * month   | day_of_month   | valid?                                             |
 * |
 * :-------------- | :--------- | :------ | :------------- |
 * :------------------------------------------------- |
 * | yearly          | 1          | january |
 * -1             | valid                                              |
 * | yearly          | 1     
 *     | march   |                | invalid - missing `day_of_month`                   |
 * | monthly 
 *        | 6          |         | 12             | valid                                            
 *  |
 * | monthly         | 6          | august  | 12             | invalid - `month` must be blank  
 *                  |
 * | weekly          | 2          |         |                | valid            
 *                                  |
 * | weekly          | 2          | october | 10             |
 * invalid - `month` and `day_of_month` must be blank |
 * 
 * ### Rolling dates
 * 
 * When a charge
 * date falls on a non-business day, one of two things will happen:
 * 
 * - if the recurrence rule
 * specified `-1` as the `day_of_month`, the charge date will be rolled __backwards__ to the previous
 * business day (i.e., the last working day of the month).
 * - otherwise the charge date will be
 * rolled __forwards__ to the next business day.
 * 
 */
public class SubscriptionService {
    private final HttpClient httpClient;

    /**
     * Constructor.  Users of this library should have no need to call this - an instance
     * of this class can be obtained by calling
      {@link com.gocardless.GoCardlessClient#subscriptions() }.
     */
    public SubscriptionService(HttpClient httpClient) {
        this.httpClient = httpClient;
    }

    /**
     * Creates a new subscription object
     */
    public SubscriptionCreateRequest create() {
        return new SubscriptionCreateRequest(httpClient);
    }

    /**
     * Returns a [cursor-paginated](#api-usage-cursor-pagination) list of your subscriptions.
     */
    public SubscriptionListRequest<ListResponse<Subscription>> list() {
        return new SubscriptionListRequest<>(httpClient, ListRequest.<Subscription>pagingExecutor());
    }

    public SubscriptionListRequest<Iterable<Subscription>> all() {
        return new SubscriptionListRequest<>(httpClient,
                ListRequest.<Subscription>iteratingExecutor());
    }

    /**
     * Retrieves the details of a single subscription.
     */
    public SubscriptionGetRequest get(String identity) {
        return new SubscriptionGetRequest(httpClient, identity);
    }

    /**
     * Updates a subscription object.
     */
    public SubscriptionUpdateRequest update(String identity) {
        return new SubscriptionUpdateRequest(httpClient, identity);
    }

    /**
     * Immediately cancels a subscription; no more payments will be created under it. Any metadata
     * supplied to this endpoint will be stored on the payment cancellation event it causes.
     * 
     * This
     * will fail with a cancellation_failed error if the subscription is already cancelled or finished.
     */
    public SubscriptionCancelRequest cancel(String identity) {
        return new SubscriptionCancelRequest(httpClient, identity);
    }

    /**
     * Request class for {@link SubscriptionService#create }.
     *
     * Creates a new subscription object
     */
    public static final class SubscriptionCreateRequest extends IdempotentPostRequest<Subscription> {
        private Integer amount;
        private Integer appFee;
        private Integer count;
        private String currency;
        private Integer dayOfMonth;
        private String endDate;
        private Integer interval;
        private IntervalUnit intervalUnit;
        private Links links;
        private Map<String, String> metadata;
        private Month month;
        private String name;
        private String paymentReference;
        private String startDate;

        /**
         * Amount in pence or cents.
         */
        public SubscriptionCreateRequest withAmount(Integer amount) {
            this.amount = amount;
            return this;
        }

        /**
         * The amount to be deducted from the payment as the OAuth app's fee, in pence or cents.
         */
        public SubscriptionCreateRequest withAppFee(Integer appFee) {
            this.appFee = appFee;
            return this;
        }

        /**
         * An alternative way to set `end_date`. The total number of payments that should be taken by this
         * subscription. This will set `end_date` automatically.
{'fraction_non_alphanumeric': 0.10973801285219971, 'fraction_numerical': 0.00461361014994233, 'mean_word_length': 1.7377086152458276, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '827491', 'n_tokens_mistral': 2984, 'n_tokens_neox': 2859, 'n_words': 932}