001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.numbers;
017
018import java.util.Map;
019import java.util.UUID;
020
021/**
022 * Filter criteria used in {@link NumbersClient#listNumbers(ListNumbersFilter)}.
023 */
024public class ListNumbersFilter extends BaseNumbersFilter {
025    private final UUID applicationId;
026    private final Boolean hasApplication;
027
028    private ListNumbersFilter(Builder builder) {
029        super(builder);
030        applicationId = builder.applicationId;
031        hasApplication = builder.hasApplication;
032    }
033
034    /**
035     * Old constructor.
036     *
037     * @deprecated Use {@link #builder()}. This will be removed in the next major release.
038     */
039    @Deprecated
040    public ListNumbersFilter() {
041        this(null, null, null, null);
042    }
043
044    @Deprecated
045    public ListNumbersFilter(
046            Integer index,
047            Integer size,
048            String pattern,
049            SearchPattern searchPattern) {
050        this(builder(index, size, pattern, searchPattern));
051    }
052
053    /**
054     * Application to return the numbers for.
055     *
056     * @return The selected application ID to list numbers from, or {@code null} if unspecified.
057     * @since 8.10.0
058     */
059    public UUID getApplicationId() {
060        return applicationId;
061    }
062
063    /**
064     * Whether results should be filtered to numbers assigned to an application.
065     *
066     * @return {@code true} if results should contain only numbers associated with an application,
067     * {@code false} if only numbers unassigned to an application should be returned, or {@code null}
068     * if unspecified (i.e. the application assignment status is not considered).
069     *
070     * @since 8.10.0
071     */
072    public Boolean getHasApplication() {
073        return hasApplication;
074    }
075
076    @Override
077    public Map<String, String> makeParams() {
078        Map<String, String> params = super.makeParams();
079        if (applicationId != null) {
080            params.put("application_id", applicationId.toString());
081        }
082        if (hasApplication != null) {
083            params.put("has_application", hasApplication.toString());
084        }
085        return params;
086    }
087
088    @Deprecated
089    private static Builder builder(Integer index,
090                                   Integer size,
091                                   String pattern,
092                                   SearchPattern searchPattern) {
093        Builder builder = builder();
094        if (index != null) {
095            builder.index(index);
096        }
097        if (size != null) {
098            builder.size(size);
099        }
100        if (pattern != null && searchPattern != null) {
101            builder.pattern(searchPattern, pattern);
102        }
103        return builder;
104    }
105
106    /**
107     * Entrypoint for constructing an instance of this class.
108     *
109     * @return A new Builder.
110     * @since 8.10.0
111     */
112    public static Builder builder() {
113        return new Builder();
114    }
115
116    /**
117     * Builder for setting the parameters of ListNumbersFilter.
118     *
119     * @since 8.10.0
120     */
121    public static final class Builder extends BaseNumbersFilter.Builder<ListNumbersFilter, Builder> {
122        private UUID applicationId;
123        private Boolean hasApplication;
124
125        Builder() {}
126
127        /**
128         * Set this to only return numbers assigned to a specific application.
129         *
130         * @param applicationId The application ID to return numbers for as a string.
131         * @return This builder.
132         */
133        public Builder applicationId(String applicationId) {
134            return applicationId(UUID.fromString(applicationId));
135        }
136
137        /**
138         * Set this to only return numbers assigned to a specific application.
139         *
140         * @param applicationId The application ID to return numbers for.
141         * @return This builder.
142         */
143        public Builder applicationId(UUID applicationId) {
144            this.applicationId = applicationId;
145            return this;
146        }
147
148        /**
149         * Set this optional field to {@code true} to restrict your results to numbers associated with any
150         * application. Set to {@code false} to find all numbers not associated with an application. Omit the
151         * field to avoid filtering on whether or not the number is assigned to an application.
152         *
153         * @param hasApplication Whether to return only numbers that are assigned to an application.
154         * @return This builder.
155         */
156        public Builder hasApplication(boolean hasApplication) {
157            this.hasApplication = hasApplication;
158            return this;
159        }
160
161        @Override
162        public ListNumbersFilter build() {
163            return new ListNumbersFilter(this);
164        }
165    }
166}