1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.doclava;
18
19import org.gradle.external.javadoc.internal.AbstractJavadocOptionFileOption;
20import org.gradle.external.javadoc.internal.JavadocOptionFileWriterContext;
21
22import java.io.IOException;
23import java.util.ArrayList;
24import java.util.Arrays;
25import java.util.Iterator;
26import java.util.List;
27
28/**
29 * This class is used to hold complex argument(s) to doclava
30 */
31public class DoclavaMultilineJavadocOptionFileOption extends
32        AbstractJavadocOptionFileOption<List<List<String>>> {
33
34    public DoclavaMultilineJavadocOptionFileOption(String option) {
35        super(option, null);
36    }
37
38    public DoclavaMultilineJavadocOptionFileOption(String option, List<List<String>> value) {
39        super(option, value);
40    }
41
42    @Override
43    public void setValue(List<List<String>> value) {
44        final List<List<String>> args = getValue();
45        if (args == null) {
46            super.setValue(new ArrayList<List<String>>(value));
47        } else {
48            args.addAll(value);
49        }
50    }
51
52    public void add(List<String>... moreArgs) {
53        final List<List<String>> args = getValue();
54        if (args == null) {
55            super.setValue(new ArrayList<List<String>>(Arrays.asList(moreArgs)));
56        } else {
57            args.addAll(Arrays.asList(moreArgs));
58        }
59    }
60
61    @Override
62    public void write(JavadocOptionFileWriterContext writerContext) throws IOException {
63        final List<List<String>> args = getValue();
64        if (args != null && !args.isEmpty()) {
65            for (List<String> arg : args) {
66                writerContext.writeOptionHeader(getOption());
67                if (!arg.isEmpty()) {
68                    final Iterator<String> iter = arg.iterator();
69                    while (true) {
70                        writerContext.writeValue(iter.next());
71                        if (!iter.hasNext()) {
72                            break;
73                        }
74                        writerContext.write(" ");
75                    }
76                }
77                writerContext.newLine();
78            }
79        }
80    }
81
82    /**
83     * @return a deep copy of the option
84     */
85    public DoclavaMultilineJavadocOptionFileOption duplicate() {
86        final List<List<String>> value = getValue();
87        final ArrayList<List<String>> valueCopy = new ArrayList<>(value.size());
88        for (List<String> item : value) {
89            valueCopy.add(new ArrayList<>(item));
90        }
91        return new DoclavaMultilineJavadocOptionFileOption(getOption(), valueCopy);
92    }
93}
94