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.voice.ncco;
017
018import com.vonage.client.JsonableBaseObject;
019
020/**
021 * DTMF (Dial Tone Multi Frequency) settings for Input Actions that will be added to a NCCO object.
022 */
023public class DtmfSettings extends JsonableBaseObject {
024    private Integer timeOut, maxDigits;
025    private Boolean submitOnHash;
026
027    /**
028     * Constructor.
029     * @deprecated Use {@link #builder()}. This will be made package-private in the next major release.
030     */
031    @Deprecated
032    public DtmfSettings() {}
033
034    private DtmfSettings(Builder builder) {
035        submitOnHash = builder.submitOnHash;
036        if ((timeOut = builder.timeOut) != null && (timeOut < 0 || timeOut > 10)) {
037            throw new IllegalArgumentException("'timeOut' must be positive and less than 10 seconds.");
038        }
039        if ((maxDigits = builder.maxDigits) != null && (maxDigits < 0 || maxDigits > 20)) {
040            throw new IllegalArgumentException("'maxDigits' must be positive and less than 20.");
041        }
042    }
043
044    /**
045     * Time to wait in seconds before submitting the event. Default value is 3.
046     *
047     * @return The DTMF input timeout in seconds as an integer, or {@code null} if unspecified.
048     */
049    public Integer getTimeOut() {
050        return timeOut;
051    }
052
053    /**
054     * The result of the callee's activity is sent to the {@code eventUrl} webhook endpoint
055     * {@code timeOut} seconds after the last action. The default value is 3. Max is 10.
056     *
057     * @param timeOut The DTMF input timeout in seconds as an int.
058     * @deprecated Use the {@linkplain #builder()}. This will be removed in the next major release.
059     */
060    @Deprecated
061    public void setTimeOut(Integer timeOut) {
062        this.timeOut = timeOut;
063    }
064
065    /**
066     * The number of digits the user can press. The maximum value is 20, the default is 4 digits.
067     *
068     * @return The number of digits as an integer, or {@code null} if unspecified.
069     */
070    public Integer getMaxDigits() {
071        return maxDigits;
072    }
073
074    /**
075     * The number of digits the user can press. The maximum value is 20, the default is 4 digits.
076     *
077     * @param maxDigits The number of digits as an int.
078     * @deprecated Use the {@linkplain #builder()}. This will be removed in the next major release.
079     */
080    @Deprecated
081    public void setMaxDigits(Integer maxDigits) {
082        this.maxDigits = maxDigits;
083    }
084
085    /**
086     * Determines if the callee's activity is sent to your webhook endpoint after pressing the hash key.
087     *
088     * @return {@code true} if the input is submitted on {@code #}, or {@code null} if unspecified.
089     */
090    public Boolean isSubmitOnHash() {
091        return submitOnHash;
092    }
093
094    /**
095     * Set to {@code true} so the callee's activity is sent to your webhook endpoint at {@code eventUrl}
096     * after they press {@code #}. If # is not pressed the result is submitted after {@code timeOut} seconds.
097     * The default value is {@code false}. That is, the result is sent to your webhook endpoint after
098     * {@code timeOut} seconds.
099     *
100     * @param submitOnHash Whether to submit the input after pressing the hash key.
101     * @deprecated Use the {@linkplain #builder()}. This will be removed in the next major release.
102     */
103    @Deprecated
104    public void setSubmitOnHash(Boolean submitOnHash) {
105        this.submitOnHash = submitOnHash;
106    }
107
108    /**
109     * Entrypoint for constructing an instance of this class.
110     *
111     * @return A new Builder.
112     * @since 8.9.4
113     */
114    public static Builder builder() {
115        return new Builder();
116    }
117
118    /**
119     * Builder for specifying DTMF settings.
120     *
121     * @since 8.9.4
122     */
123    public static final class Builder {
124        private Integer timeOut, maxDigits;
125        private Boolean submitOnHash;
126
127        private Builder() {}
128
129        /**
130         * The result of the callee's activity is sent to the {@code eventUrl} webhook endpoint
131         * {@code timeOut} seconds after the last action. The default value is 3. Max is 10.
132         *
133         * @param timeOut The DTMF input timeout in seconds as an int.
134         * @return This builder.
135         */
136        public Builder timeOut(int timeOut) {
137            this.timeOut = timeOut;
138            return this;
139        }
140
141        /**
142         * The number of digits the user can press. The maximum value is 20, the default is 4 digits.
143         *
144         * @param maxDigits The number of digits as an int.
145         * @return This builder.
146         */
147        public Builder maxDigits(int maxDigits) {
148            this.maxDigits = maxDigits;
149            return this;
150        }
151
152        /**
153         * Set to {@code true} so the callee's activity is sent to your webhook endpoint at {@code eventUrl}
154         * after they press {@code #}. If # is not pressed the result is submitted after {@code timeOut}
155         * seconds. The default value is {@code false}. That is, the result is sent to your webhook
156         * endpoint after {@code timeOut} seconds.
157         *
158         * @param submitOnHash Whether to submit the input after pressing the hash key.
159         * @return This builder.
160         */
161        public Builder submitOnHash(boolean submitOnHash) {
162            this.submitOnHash = submitOnHash;
163            return this;
164        }
165
166        /**
167         * Builds the DtmfSettings with this builder's properties.
168         *
169         * @return A new DtmfSettings instance.
170         */
171        public DtmfSettings build() {
172            return new DtmfSettings(this);
173        }
174    }
175}