JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/printer/lexicalpreservation/TextElement.java
1/*
2 * Copyright (C) 2007-2010 JĂșlio Vilmar Gesser.
3 * Copyright (C) 2011, 2013-2020 The JavaParser Team.
4 *
5 * This file is part of JavaParser.
6 *
7 * JavaParser can be used either under the terms of
8 * a) the GNU Lesser General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 * b) the terms of the Apache License
12 *
13 * You should have received a copy of both licenses in LICENCE.LGPL and
14 * LICENCE.APACHE. Please refer to those files for details.
15 *
16 * JavaParser is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 */
21
22package com.github.javaparser.printer.lexicalpreservation;
23
24import com.github.javaparser.GeneratedJavaParserConstants;
25import com.github.javaparser.Range;
26import com.github.javaparser.ast.Node;
27
28import java.util.Optional;
29
30public abstract class TextElement implements TextElementMatcher {
31
32    abstract String expand();
33
34    abstract boolean isToken(int tokenKind);
35
36    final boolean isCommentToken() {
37        return isToken(GeneratedJavaParserConstants.JAVADOC_COMMENT)
38                || isToken(GeneratedJavaParserConstants.SINGLE_LINE_COMMENT)
39                || isToken(GeneratedJavaParserConstants.MULTI_LINE_COMMENT);
40    }
41
42    @Override
43    public boolean match(TextElement textElement) {
44        return this.equals(textElement);
45    }
46
47    abstract boolean isNode(Node node);
48
49    public abstract boolean isLiteral();
50
51    public abstract boolean isWhiteSpace();
52
53    public abstract boolean isSpaceOrTab();
54
55    public abstract boolean isNewline();
56
57    public abstract boolean isComment();
58
59    public abstract boolean isSeparator();
60
61    public abstract boolean isIdentifier();
62
63    public abstract boolean isPrimitive();
64
65    public final boolean isWhiteSpaceOrComment() {
66        return isWhiteSpace() || isComment();
67    }
68
69    /**
70     * Is this TextElement representing a child of the given class?
71     */
72    public abstract boolean isChildOfClass(Class<? extends NodenodeClass);
73
74    public boolean isChild() {
75        return isChildOfClass(Node.class);
76    }
77
78    abstract Optional<RangegetRange();
79
80    /**
81     * Creates a {@link TextElementMatcher} that matches any TextElement with the same range as this TextElement.<br>
82     * This can be used to curry another TextElementMatcher.<br>
83     * e.g. {@code someTextElementMatcher.and(textElement.matchByRange());}
84     *
85     * @return TextElementMatcher that matches any TextElement with the same Range
86     */
87    TextElementMatcher matchByRange() {
88        return (TextElement textElement) ->
89                getRange()
90                        .flatMap(r1 -> textElement.getRange()
91                                .map(r1::equals))
92                        // We're missing range information. This may happen when a node is manually instantiated. Don't be too harsh on that:
93                        .orElse(true);
94    }
95}
96
MembersX
TextElement:isCommentToken
TextElement:isNode
TextElement:isPrimitive
TextElement:isChild
TextElement:matchByRange
TextElement:isSpaceOrTab
TextElement:isWhiteSpace
TextElement:isSeparator
TextElement:isLiteral
TextElement:isToken
TextElement:isNewline
TextElement:isIdentifier
TextElement:expand
TextElement:getRange
TextElement:isComment
TextElement:isWhiteSpaceOrComment
TextElement:isChildOfClass
TextElement:match
Members
X