• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007-2010 Júlio Vilmar Gesser.
3  * Copyright (C) 2011, 2013-2016 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 
22 package com.github.javaparser.printer.lexicalpreservation;
23 
24 import com.github.javaparser.GeneratedJavaParserConstants;
25 import com.github.javaparser.Range;
26 import com.github.javaparser.ast.Node;
27 
28 import java.util.Optional;
29 
30 public abstract class TextElement implements TextElementMatcher {
31 
expand()32     abstract String expand();
33 
isToken(int tokenKind)34     abstract boolean isToken(int tokenKind);
35 
isCommentToken()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
match(TextElement textElement)43     public boolean match(TextElement textElement) {
44         return this.equals(textElement);
45     }
46 
isNode(Node node)47     abstract boolean isNode(Node node);
48 
isWhiteSpace()49     public abstract boolean isWhiteSpace();
50 
isSpaceOrTab()51     public abstract boolean isSpaceOrTab();
52 
isNewline()53     public abstract boolean isNewline();
54 
isComment()55     public abstract boolean isComment();
56 
isWhiteSpaceOrComment()57     public final boolean isWhiteSpaceOrComment() {
58         return isWhiteSpace() || isComment();
59     }
60 
61     /**
62      * Is this TextElement representing a child of the given class?
63      */
isChildOfClass(Class<? extends Node> nodeClass)64     public abstract boolean isChildOfClass(Class<? extends Node> nodeClass);
65 
isChild()66     public boolean isChild() {
67         return isChildOfClass(Node.class);
68     }
69 
getRange()70     abstract Optional<Range> getRange();
71 
72     /**
73      * Creates a {@link TextElementMatcher} that matches any TextElement with the same range as this TextElement.<br/>
74      * This can be used to curry another TextElementMatcher.<br/>
75      * e.g. {@code someTextElementMatcher.and(textElement.matchByRange());}
76      *
77      * @return TextElementMatcher that matches any TextElement with the same Range
78      */
matchByRange()79     TextElementMatcher matchByRange() {
80         return (TextElement textElement) -> {
81             Optional<Range> range1 = this.getRange();
82             Optional<Range> range2 = textElement.getRange();
83             if (range1.isPresent() && range2.isPresent()) {
84                 return range1.get().equals(range2.get());
85             }
86 
87             return false;
88         };
89     }
90 }
91