SimpleDocTreeVisitor.java revision 3193:3b3bea483542
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 */
41public class SimpleDocTreeVisitor<R,P> implements DocTreeVisitor<R, P> {
42    /**
43     * The default value, returned by the {@link #defaultAction default action}.
44     */
45    protected final R DEFAULT_VALUE;
46
47    /**
48     * Creates a visitor, with a DEFAULT_VALUE of {@code null}.
49     */
50    protected SimpleDocTreeVisitor() {
51        DEFAULT_VALUE = null;
52    }
53
54    /**
55     * Creates a visitor, with a specified DEFAULT_VALUE.
56     * @param defaultValue the default value to be returned by the default action.
57     */
58    protected SimpleDocTreeVisitor(R defaultValue) {
59        DEFAULT_VALUE = defaultValue;
60    }
61
62    /**
63     * The default action, used by all visit methods that are not overridden.
64     * @param node the node being visited
65     * @param p the parameter value passed to the visit method
66     * @return the result value to be returned from the visit method
67     */
68    protected R defaultAction(DocTree node, P p) {
69        return DEFAULT_VALUE;
70    }
71
72    /**
73     * Invokes the appropriate visit method specific to the type of the node.
74     * @param node the node on which to dispatch
75     * @param p a parameter to be passed to the appropriate visit method
76     * @return the value returns from the appropriate visit method
77     */
78    public final R visit(DocTree node, P p) {
79        return (node == null) ? null : node.accept(this, p);
80    }
81
82    /**
83     * Invokes the appropriate visit method on each of a sequence of nodes.
84     * @param nodes the nodes on which to dispatch
85     * @param p a parameter value to be passed to each appropriate visit method
86     * @return the value return from the last of the visit methods, or null
87     *      if none were called.
88     */
89    public final R visit(Iterable<? extends DocTree> nodes, P p) {
90        R r = null;
91        if (nodes != null) {
92            for (DocTree node : nodes)
93                r = visit(node, p);
94        }
95        return r;
96    }
97
98    /**
99     * {@inheritDoc} This implementation calls {@code defaultAction}.
100     *
101     * @param node {@inheritDoc}
102     * @param p {@inheritDoc}
103     * @return  the result of {@code defaultAction}
104     */
105    @Override
106    public R visitAttribute(AttributeTree node, P p) {
107        return defaultAction(node, p);
108    }
109
110    /**
111     * {@inheritDoc} This implementation calls {@code defaultAction}.
112     *
113     * @param node {@inheritDoc}
114     * @param p {@inheritDoc}
115     * @return  the result of {@code defaultAction}
116     */
117    @Override
118    public R visitAuthor(AuthorTree node, P p) {
119        return defaultAction(node, p);
120    }
121
122    /**
123     * {@inheritDoc} This implementation calls {@code defaultAction}.
124     *
125     * @param node {@inheritDoc}
126     * @param p {@inheritDoc}
127     * @return  the result of {@code defaultAction}
128     */
129    @Override
130    public R visitComment(CommentTree node, P p) {
131        return defaultAction(node, p);
132    }
133
134    /**
135     * {@inheritDoc} This implementation calls {@code defaultAction}.
136     *
137     * @param node {@inheritDoc}
138     * @param p {@inheritDoc}
139     * @return  the result of {@code defaultAction}
140     */
141    @Override
142    public R visitDeprecated(DeprecatedTree node, P p) {
143        return defaultAction(node, p);
144    }
145
146    /**
147     * {@inheritDoc} This implementation calls {@code defaultAction}.
148     *
149     * @param node {@inheritDoc}
150     * @param p {@inheritDoc}
151     * @return  the result of {@code defaultAction}
152     */
153    @Override
154    public R visitDocComment(DocCommentTree node, P p) {
155        return defaultAction(node, p);
156    }
157
158    /**
159     * {@inheritDoc} This implementation calls {@code defaultAction}.
160     *
161     * @param node {@inheritDoc}
162     * @param p {@inheritDoc}
163     * @return  the result of {@code defaultAction}
164     */
165    @Override
166    public R visitDocRoot(DocRootTree node, P p) {
167        return defaultAction(node, p);
168    }
169
170    /**
171     * {@inheritDoc} This implementation calls {@code defaultAction}.
172     *
173     * @param node {@inheritDoc}
174     * @param p {@inheritDoc}
175     * @return  the result of {@code defaultAction}
176     */
177    @Override
178    public R visitEndElement(EndElementTree node, P p) {
179        return defaultAction(node, p);
180    }
181
182    /**
183     * {@inheritDoc} This implementation calls {@code defaultAction}.
184     *
185     * @param node {@inheritDoc}
186     * @param p {@inheritDoc}
187     * @return  the result of {@code defaultAction}
188     */
189    @Override
190    public R visitEntity(EntityTree node, P p) {
191        return defaultAction(node, p);
192    }
193
194    /**
195     * {@inheritDoc} This implementation calls {@code defaultAction}.
196     *
197     * @param node {@inheritDoc}
198     * @param p {@inheritDoc}
199     * @return  the result of {@code defaultAction}
200     */
201    @Override
202    public R visitErroneous(ErroneousTree node, P p) {
203        return defaultAction(node, p);
204    }
205
206    /**
207     * {@inheritDoc} This implementation calls {@code defaultAction}.
208     *
209     * @param node {@inheritDoc}
210     * @param p {@inheritDoc}
211     * @return  the result of {@code defaultAction}
212     */
213    @Override
214    public R visitIdentifier(IdentifierTree node, P p) {
215        return defaultAction(node, p);
216    }
217
218    /**
219     * {@inheritDoc} This implementation calls {@code defaultAction}.
220     *
221     * @param node {@inheritDoc}
222     * @param p {@inheritDoc}
223     * @return  the result of {@code defaultAction}
224     */
225    @Override
226    public R visitIndex(IndexTree node, P p) {
227        return defaultAction(node, p);
228    }
229
230    /**
231     * {@inheritDoc} This implementation calls {@code defaultAction}.
232     *
233     * @param node {@inheritDoc}
234     * @param p {@inheritDoc}
235     * @return  the result of {@code defaultAction}
236     */
237    @Override
238    public R visitInheritDoc(InheritDocTree node, P p) {
239        return defaultAction(node, p);
240    }
241
242    /**
243     * {@inheritDoc} This implementation calls {@code defaultAction}.
244     *
245     * @param node {@inheritDoc}
246     * @param p {@inheritDoc}
247     * @return  the result of {@code defaultAction}
248     */
249    @Override
250    public R visitLink(LinkTree node, P p) {
251        return defaultAction(node, p);
252    }
253
254    /**
255     * {@inheritDoc} This implementation calls {@code defaultAction}.
256     *
257     * @param node {@inheritDoc}
258     * @param p {@inheritDoc}
259     * @return  the result of {@code defaultAction}
260     */
261    @Override
262    public R visitLiteral(LiteralTree node, P p) {
263        return defaultAction(node, p);
264    }
265
266    /**
267     * {@inheritDoc} This implementation calls {@code defaultAction}.
268     *
269     * @param node {@inheritDoc}
270     * @param p {@inheritDoc}
271     * @return  the result of {@code defaultAction}
272     */
273    @Override
274    public R visitParam(ParamTree node, P p) {
275        return defaultAction(node, p);
276    }
277
278    /**
279     * {@inheritDoc} This implementation calls {@code defaultAction}.
280     *
281     * @param node {@inheritDoc}
282     * @param p {@inheritDoc}
283     * @return  the result of {@code defaultAction}
284     */
285    @Override
286    public R visitReference(ReferenceTree node, P p) {
287        return defaultAction(node, p);
288    }
289
290    /**
291     * {@inheritDoc} This implementation calls {@code defaultAction}.
292     *
293     * @param node {@inheritDoc}
294     * @param p {@inheritDoc}
295     * @return  the result of {@code defaultAction}
296     */
297    @Override
298    public R visitReturn(ReturnTree node, P p) {
299        return defaultAction(node, p);
300    }
301
302    /**
303     * {@inheritDoc} This implementation calls {@code defaultAction}.
304     *
305     * @param node {@inheritDoc}
306     * @param p {@inheritDoc}
307     * @return  the result of {@code defaultAction}
308     */
309    @Override
310    public R visitSee(SeeTree node, P p) {
311        return defaultAction(node, p);
312    }
313
314    /**
315     * {@inheritDoc} This implementation calls {@code defaultAction}.
316     *
317     * @param node {@inheritDoc}
318     * @param p {@inheritDoc}
319     * @return  the result of {@code defaultAction}
320     */
321    @Override
322    public R visitSerial(SerialTree node, P p) {
323        return defaultAction(node, p);
324    }
325
326    /**
327     * {@inheritDoc} This implementation calls {@code defaultAction}.
328     *
329     * @param node {@inheritDoc}
330     * @param p {@inheritDoc}
331     * @return  the result of {@code defaultAction}
332     */
333    @Override
334    public R visitSerialData(SerialDataTree node, P p) {
335        return defaultAction(node, p);
336    }
337
338    /**
339     * {@inheritDoc} This implementation calls {@code defaultAction}.
340     *
341     * @param node {@inheritDoc}
342     * @param p {@inheritDoc}
343     * @return  the result of {@code defaultAction}
344     */
345    @Override
346    public R visitSerialField(SerialFieldTree node, P p) {
347        return defaultAction(node, p);
348    }
349
350    /**
351     * {@inheritDoc} This implementation calls {@code defaultAction}.
352     *
353     * @param node {@inheritDoc}
354     * @param p {@inheritDoc}
355     * @return  the result of {@code defaultAction}
356     */
357    @Override
358    public R visitSince(SinceTree node, P p) {
359        return defaultAction(node, p);
360    }
361
362    /**
363     * {@inheritDoc} This implementation calls {@code defaultAction}.
364     *
365     * @param node {@inheritDoc}
366     * @param p {@inheritDoc}
367     * @return  the result of {@code defaultAction}
368     */
369    @Override
370    public R visitStartElement(StartElementTree node, P p) {
371        return defaultAction(node, p);
372    }
373
374    /**
375     * {@inheritDoc} This implementation calls {@code defaultAction}.
376     *
377     * @param node {@inheritDoc}
378     * @param p {@inheritDoc}
379     * @return  the result of {@code defaultAction}
380     */
381    @Override
382    public R visitText(TextTree node, P p) {
383        return defaultAction(node, p);
384    }
385
386    /**
387     * {@inheritDoc} This implementation calls {@code defaultAction}.
388     *
389     * @param node {@inheritDoc}
390     * @param p {@inheritDoc}
391     * @return  the result of {@code defaultAction}
392     */
393    @Override
394    public R visitThrows(ThrowsTree node, P p) {
395        return defaultAction(node, p);
396    }
397
398    /**
399     * {@inheritDoc} This implementation calls {@code defaultAction}.
400     *
401     * @param node {@inheritDoc}
402     * @param p {@inheritDoc}
403     * @return  the result of {@code defaultAction}
404     */
405    @Override
406    public R visitUnknownBlockTag(UnknownBlockTagTree node, P p) {
407        return defaultAction(node, p);
408    }
409
410    /**
411     * {@inheritDoc} This implementation calls {@code defaultAction}.
412     *
413     * @param node {@inheritDoc}
414     * @param p {@inheritDoc}
415     * @return  the result of {@code defaultAction}
416     */
417    @Override
418    public R visitUnknownInlineTag(UnknownInlineTagTree node, P p) {
419        return defaultAction(node, p);
420    }
421
422    /**
423     * {@inheritDoc} This implementation calls {@code defaultAction}.
424     *
425     * @param node {@inheritDoc}
426     * @param p {@inheritDoc}
427     * @return  the result of {@code defaultAction}
428     */
429    @Override
430    public R visitValue(ValueTree node, P p) {
431        return defaultAction(node, p);
432    }
433
434    /**
435     * {@inheritDoc} This implementation calls {@code defaultAction}.
436     *
437     * @param node {@inheritDoc}
438     * @param p {@inheritDoc}
439     * @return  the result of {@code defaultAction}
440     */
441    @Override
442    public R visitVersion(VersionTree node, P p) {
443        return defaultAction(node, p);
444    }
445
446    /**
447     * {@inheritDoc} This implementation calls {@code defaultAction}.
448     *
449     * @param node {@inheritDoc}
450     * @param p {@inheritDoc}
451     * @return  the result of {@code defaultAction}
452     */
453    @Override
454    public R visitOther(DocTree node, P p) {
455        return defaultAction(node, p);
456    }
457
458}
459