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.fasterxml.jackson.annotation.JsonValue;
019import com.fasterxml.jackson.core.JsonProcessingException;
020import com.fasterxml.jackson.databind.ObjectMapper;
021import com.fasterxml.jackson.databind.ObjectWriter;
022import com.vonage.client.Jsonable;
023import com.vonage.client.VonageUnexpectedException;
024import java.util.Arrays;
025import java.util.Collection;
026import java.util.Collections;
027
028/**
029 * Vonage Call Control Object for controlling the flow of a Voice API call.
030 */
031public class Ncco implements Jsonable {
032    @JsonValue
033    private Collection<? extends Action> actions;
034    private ObjectWriter writer;
035
036    public Ncco() {
037        this(new ObjectMapper().writer(), Collections.emptyList());
038    }
039
040    public Ncco(Collection<Action> actions) {
041        this(Jsonable.createDefaultObjectMapper().writer(), actions);
042    }
043
044    @Deprecated
045    public Ncco(ObjectWriter writer) {
046        this(writer, Collections.emptyList());
047    }
048
049    @Deprecated
050    public Ncco(ObjectWriter writer, Collection<? extends Action> actions) {
051        this.writer = writer;
052        this.actions = actions;
053    }
054
055    @Deprecated
056    public Ncco(ObjectWriter writer, Action... action) {
057        this(writer, Arrays.asList(action));
058    }
059
060    public Ncco(Action... action) {
061        this(Arrays.asList(action));
062    }
063
064    public Collection<? extends Action> getActions() {
065        return this.actions;
066    }
067
068    @Override
069    public String toJson() {
070        try {
071            return this.writer.writeValueAsString(this.actions);
072        } catch (JsonProcessingException e) {
073            throw new VonageUnexpectedException("Unable to convert NCCO Object to JSON.");
074        }
075    }
076}