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.Iterator;
25
26/**
27 * This class is used to hold complex argument(s) to doclava
28 */
29public class DoclavaJavadocOptionFileOption extends
30        AbstractJavadocOptionFileOption<Iterable<String>> {
31
32    public DoclavaJavadocOptionFileOption(String option) {
33        super(option, null);
34    }
35
36    public DoclavaJavadocOptionFileOption(String option, Iterable<String> value) {
37        super(option, value);
38    }
39
40    @Override
41    public void write(JavadocOptionFileWriterContext writerContext) throws IOException {
42        writerContext.writeOptionHeader(getOption());
43
44        final Iterable<String> args = getValue();
45        if (args != null) {
46            final Iterator<String> iter = args.iterator();
47            while (true) {
48                writerContext.writeValue(iter.next());
49                if (!iter.hasNext()) {
50                    break;
51                }
52                writerContext.write(" ");
53            }
54        }
55
56        writerContext.newLine();
57    }
58
59    /**
60     * @return a deep copy of the option
61     */
62    public DoclavaJavadocOptionFileOption duplicate() {
63        final Iterable<String> value = getValue();
64        final ArrayList<String> valueCopy;
65        if (value != null) {
66            valueCopy = new ArrayList<>();
67            for (String item : value) {
68                valueCopy.add(item);
69            }
70        } else {
71            valueCopy = null;
72        }
73        return new DoclavaJavadocOptionFileOption(getOption(), valueCopy);
74    }
75}
76