SimpleDocTreeVisitor.java revision 3162:f164d4c2d33e
1/*
2 * Copyright (c) 2005, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.source.util;
27
28import com.sun.source.doctree.*;
29
30/**
31 * A simple visitor for tree nodes.
32 *
33 * @param <R> the return type of this visitor's methods.  Use {@link
34 *            Void} for visitors that do not need to return results.
35 * @param <P> the type of the additional parameter to this visitor's
36 *            methods.  Use {@code Void} for visitors that do not need an
37 *            additional parameter.
38 *
39 * @since 1.8
40 */
41@jdk.Exported
42public class SimpleDocTreeVisitor<R,P> implements DocTreeVisitor<R, P> {
43    /**
44     * The default value, returned by the {@link #defaultAction default action}.
45     */
46    protected final R DEFAULT_VALUE;
47
48    /**
49     * Creates a visitor, with a DEFAULT_VALUE of {@code null}.
50     */
51    protected SimpleDocTreeVisitor() {
52        DEFAULT_VALUE = null;
53    }
54
55    /**
56     * Creates a visitor, with a specified DEFAULT_VALUE.
57     * @param defaultValue the default value to be returned by the default action.
58     */
59    protected SimpleDocTreeVisitor(R defaultValue) {
60        DEFAULT_VALUE = defaultValue;
61    }
62
63    /**
64     * The default action, used by all visit methods that are not overridden.
65     * @param node the node being visited
66     * @param p the parameter value passed to the visit method
67     * @return the result value to be returned from the visit method
68     */
69    protected R defaultAction(DocTree node, P p) {
70        return DEFAULT_VALUE;
71    }
72
73    /**
74     * Invokes the appropriate visit method specific to the type of the node.
75     * @param node the node on which to dispatch
76     * @param p a parameter to be passed to the appropriate visit method
77     * @return the value returns from the appropriate visit method
78     */
79    public final R visit(DocTree node, P p) {
80        return (node == null) ? null : node.accept(this, p);
81    }
82
83    /**
84     * Invokes the appropriate visit method on each of a sequence of nodes.
85     * @param nodes the nodes on which to dispatch
86     * @param p a parameter value to be passed to each appropriate visit method
87     * @return the value return from the last of the visit methods, or null
88     *      if none were called.
89     */
90    public final R visit(Iterable<? extends DocTree> nodes, P p) {
91        R r = null;
92        if (nodes != null) {
93            for (DocTree node : nodes)
94                r = visit(node, p);
95        }
96        return r;
97    }
98
99    /**
100     * {@inheritDoc} This implementation calls {@code defaultAction}.
101     *
102     * @param node {@inheritDoc}
103     * @param p {@inheritDoc}
104     * @return  the result of {@code defaultAction}
105     */
106    @Override
107    public R visitAttribute(AttributeTree node, P p) {
108        return defaultAction(node, p);
109    }
110
111    /**
112     * {@inheritDoc} This implementation calls {@code defaultAction}.
113     *
114     * @param node {@inheritDoc}
115     * @param p {@inheritDoc}
116     * @return  the result of {@code defaultAction}
117     */
118    @Override
119    public R visitAuthor(AuthorTree node, P p) {
120        return defaultAction(node, p);
121    }
122
123    /**
124     * {@inheritDoc} This implementation calls {@code defaultAction}.
125     *
126     * @param node {@inheritDoc}
127     * @param p {@inheritDoc}
128     * @return  the result of {@code defaultAction}
129     */
130    @Override
131    public R visitComment(CommentTree node, P p) {
132        return defaultAction(node, p);
133    }
134
135    /**
136     * {@inheritDoc} This implementation calls {@code defaultAction}.
137     *
138     * @param node {@inheritDoc}
139     * @param p {@inheritDoc}
140     * @return  the result of {@code defaultAction}
141     */
142    @Override
143    public R visitDeprecated(DeprecatedTree node, P p) {
144        return defaultAction(node, p);
145    }
146
147    /**
148     * {@inheritDoc} This implementation calls {@code defaultAction}.
149     *
150     * @param node {@inheritDoc}
151     * @param p {@inheritDoc}
152     * @return  the result of {@code defaultAction}
153     */
154    @Override
155    public R visitDocComment(DocCommentTree node, P p) {
156        return defaultAction(node, p);
157    }
158
159    /**
160     * {@inheritDoc} This implementation calls {@code defaultAction}.
161     *
162     * @param node {@inheritDoc}
163     * @param p {@inheritDoc}
164     * @return  the result of {@code defaultAction}
165     */
166    @Override
167    public R visitDocRoot(DocRootTree node, P p) {
168        return defaultAction(node, p);
169    }
170
171    /**
172     * {@inheritDoc} This implementation calls {@code defaultAction}.
173     *
174     * @param node {@inheritDoc}
175     * @param p {@inheritDoc}
176     * @return  the result of {@code defaultAction}
177     */
178    @Override
179    public R visitEndElement(EndElementTree node, P p) {
180        return defaultAction(node, p);
181    }
182
183    /**
184     * {@inheritDoc} This implementation calls {@code defaultAction}.
185     *
186     * @param node {@inheritDoc}
187     * @param p {@inheritDoc}
188     * @return  the result of {@code defaultAction}
189     */
190    @Override
191    public R visitEntity(EntityTree node, P p) {
192        return defaultAction(node, p);
193    }
194
195    /**
196     * {@inheritDoc} This implementation calls {@code defaultAction}.
197     *
198     * @param node {@inheritDoc}
199     * @param p {@inheritDoc}
200     * @return  the result of {@code defaultAction}
201     */
202    @Override
203    public R visitErroneous(ErroneousTree node, P p) {
204        return defaultAction(node, p);
205    }
206
207    /**
208     * {@inheritDoc} This implementation calls {@code defaultAction}.
209     *
210     * @param node {@inheritDoc}
211     * @param p {@inheritDoc}
212     * @return  the result of {@code defaultAction}
213     */
214    @Override
215    public R visitIdentifier(IdentifierTree node, P p) {
216        return defaultAction(node, p);
217    }
218
219    /**
220     * {@inheritDoc} This implementation calls {@code defaultAction}.
221     *
222     * @param node {@inheritDoc}
223     * @param p {@inheritDoc}
224     * @return  the result of {@code defaultAction}
225     */
226    @Override
227    public R visitIndex(IndexTree node, P p) {
228        return defaultAction(node, p);
229    }
230
231    /**
232     * {@inheritDoc} This implementation calls {@code defaultAction}.
233     *
234     * @param node {@inheritDoc}
235     * @param p {@inheritDoc}
236     * @return  the result of {@code defaultAction}
237     */
238    @Override
239    public R visitInheritDoc(InheritDocTree node, P p) {
240        return defaultAction(node, p);
241    }
242
243    /**
244     * {@inheritDoc} This implementation calls {@code defaultAction}.
245     *
246     * @param node {@inheritDoc}
247     * @param p {@inheritDoc}
248     * @return  the result of {@code defaultAction}
249     */
250    @Override
251    public R visitLink(LinkTree node, P p) {
252        return defaultAction(node, p);
253    }
254
255    /**
256     * {@inheritDoc} This implementation calls {@code defaultAction}.
257     *
258     * @param node {@inheritDoc}
259     * @param p {@inheritDoc}
260     * @return  the result of {@code defaultAction}
261     */
262    @Override
263    public R visitLiteral(LiteralTree node, P p) {
264        return defaultAction(node, p);
265    }
266
267    /**
268     * {@inheritDoc} This implementation calls {@code defaultAction}.
269     *
270     * @param node {@inheritDoc}
271     * @param p {@inheritDoc}
272     * @return  the result of {@code defaultAction}
273     */
274    @Override
275    public R visitParam(ParamTree node, P p) {
276        return defaultAction(node, p);
277    }
278
279    /**
280     * {@inheritDoc} This implementation calls {@code defaultAction}.
281     *
282     * @param node {@inheritDoc}
283     * @param p {@inheritDoc}
284     * @return  the result of {@code defaultAction}
285     */
286    @Override
287    public R visitReference(ReferenceTree node, P p) {
288        return defaultAction(node, p);
289    }
290
291    /**
292     * {@inheritDoc} This implementation calls {@code defaultAction}.
293     *
294     * @param node {@inheritDoc}
295     * @param p {@inheritDoc}
296     * @return  the result of {@code defaultAction}
297     */
298    @Override
299    public R visitReturn(ReturnTree node, P p) {
300        return defaultAction(node, p);
301    }
302
303    /**
304     * {@inheritDoc} This implementation calls {@code defaultAction}.
305     *
306     * @param node {@inheritDoc}
307     * @param p {@inheritDoc}
308     * @return  the result of {@code defaultAction}
309     */
310    @Override
311    public R visitSee(SeeTree node, P p) {
312        return defaultAction(node, p);
313    }
314
315    /**
316     * {@inheritDoc} This implementation calls {@code defaultAction}.
317     *
318     * @param node {@inheritDoc}
319     * @param p {@inheritDoc}
320     * @return  the result of {@code defaultAction}
321     */
322    @Override
323    public R visitSerial(SerialTree node, P p) {
324        return defaultAction(node, p);
325    }
326
327    /**
328     * {@inheritDoc} This implementation calls {@code defaultAction}.
329     *
330     * @param node {@inheritDoc}
331     * @param p {@inheritDoc}
332     * @return  the result of {@code defaultAction}
333     */
334    @Override
335    public R visitSerialData(SerialDataTree node, P p) {
336        return defaultAction(node, p);
337    }
338
339    /**
340     * {@inheritDoc} This implementation calls {@code defaultAction}.
341     *
342     * @param node {@inheritDoc}
343     * @param p {@inheritDoc}
344     * @return  the result of {@code defaultAction}
345     */
346    @Override
347    public R visitSerialField(SerialFieldTree node, P p) {
348        return defaultAction(node, p);
349    }
350
351    /**
352     * {@inheritDoc} This implementation calls {@code defaultAction}.
353     *
354     * @param node {@inheritDoc}
355     * @param p {@inheritDoc}
356     * @return  the result of {@code defaultAction}
357     */
358    @Override
359    public R visitSince(SinceTree node, P p) {
360        return defaultAction(node, p);
361    }
362
363    /**
364     * {@inheritDoc} This implementation calls {@code defaultAction}.
365     *
366     * @param node {@inheritDoc}
367     * @param p {@inheritDoc}
368     * @return  the result of {@code defaultAction}
369     */
370    @Override
371    public R visitStartElement(StartElementTree node, P p) {
372        return defaultAction(node, p);
373    }
374
375    /**
376     * {@inheritDoc} This implementation calls {@code defaultAction}.
377     *
378     * @param node {@inheritDoc}
379     * @param p {@inheritDoc}
380     * @return  the result of {@code defaultAction}
381     */
382    @Override
383    public R visitText(TextTree node, P p) {
384        return defaultAction(node, p);
385    }
386
387    /**
388     * {@inheritDoc} This implementation calls {@code defaultAction}.
389     *
390     * @param node {@inheritDoc}
391     * @param p {@inheritDoc}
392     * @return  the result of {@code defaultAction}
393     */
394    @Override
395    public R visitThrows(ThrowsTree node, P p) {
396        return defaultAction(node, p);
397    }
398
399    /**
400     * {@inheritDoc} This implementation calls {@code defaultAction}.
401     *
402     * @param node {@inheritDoc}
403     * @param p {@inheritDoc}
404     * @return  the result of {@code defaultAction}
405     */
406    @Override
407    public R visitUnknownBlockTag(UnknownBlockTagTree node, P p) {
408        return defaultAction(node, p);
409    }
410
411    /**
412     * {@inheritDoc} This implementation calls {@code defaultAction}.
413     *
414     * @param node {@inheritDoc}
415     * @param p {@inheritDoc}
416     * @return  the result of {@code defaultAction}
417     */
418    @Override
419    public R visitUnknownInlineTag(UnknownInlineTagTree node, P p) {
420        return defaultAction(node, p);
421    }
422
423    /**
424     * {@inheritDoc} This implementation calls {@code defaultAction}.
425     *
426     * @param node {@inheritDoc}
427     * @param p {@inheritDoc}
428     * @return  the result of {@code defaultAction}
429     */
430    @Override
431    public R visitValue(ValueTree node, P p) {
432        return defaultAction(node, p);
433    }
434
435    /**
436     * {@inheritDoc} This implementation calls {@code defaultAction}.
437     *
438     * @param node {@inheritDoc}
439     * @param p {@inheritDoc}
440     * @return  the result of {@code defaultAction}
441     */
442    @Override
443    public R visitVersion(VersionTree node, P p) {
444        return defaultAction(node, p);
445    }
446
447    /**
448     * {@inheritDoc} This implementation calls {@code defaultAction}.
449     *
450     * @param node {@inheritDoc}
451     * @param p {@inheritDoc}
452     * @return  the result of {@code defaultAction}
453     */
454    @Override
455    public R visitOther(DocTree node, P p) {
456        return defaultAction(node, p);
457    }
458
459}
460