001    /*
002     * Copyright 2012 Mark Slater
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
005     *
006     *      http://www.apache.org/licenses/LICENSE-2.0
007     *
008     * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
009     */
010    
011    package argo.jdom;
012    
013    import java.util.LinkedList;
014    import java.util.List;
015    
016    /**
017     * Builder for {@code JsonRootNode}s representing JSON arrays.
018     */
019    public final class JsonArrayNodeBuilder implements JsonNodeBuilder<JsonRootNode> {
020    
021        private final List<JsonNodeBuilder> elementBuilders = new LinkedList<JsonNodeBuilder>();
022    
023        JsonArrayNodeBuilder() {
024        }
025    
026        /**
027         * Adds the given element to the array that will be built.
028         *
029         * @param elementBuilder a builder for the element to add to the array.
030         * @return the modified builder.
031         */
032        public JsonArrayNodeBuilder withElement(final JsonNodeBuilder elementBuilder) {
033            elementBuilders.add(elementBuilder);
034            return this;
035        }
036    
037        public JsonRootNode build() {
038            final List<JsonNode> elements = new LinkedList<JsonNode>();
039            for (JsonNodeBuilder elementBuilder : elementBuilders) {
040                elements.add(elementBuilder.build());
041            }
042            return JsonNodeFactories.array(elements);
043        }
044    }