• 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.Range;
25 import com.github.javaparser.ast.Node;
26 import com.github.javaparser.ast.comments.Comment;
27 
28 import java.util.Optional;
29 
30 /**
31  * Represent the position of a child node in the NodeText of its parent.
32  */
33 class ChildTextElement extends TextElement {
34     private final Node child;
35 
ChildTextElement(Node child)36     ChildTextElement(Node child) {
37         this.child = child;
38     }
39 
expand()40     String expand() {
41         return LexicalPreservingPrinter.print(child);
42     }
43 
getChild()44     Node getChild() {
45         return child;
46     }
47 
48     @Override
isToken(int tokenKind)49     boolean isToken(int tokenKind) {
50         return false;
51     }
52 
53     @Override
isNode(Node node)54     boolean isNode(Node node) {
55         return node == child;
56     }
57 
getNodeTextForWrappedNode()58     NodeText getNodeTextForWrappedNode() {
59         return LexicalPreservingPrinter.getOrCreateNodeText(child);
60     }
61 
62     @Override
equals(Object o)63     public boolean equals(Object o) {
64         if (this == o) return true;
65         if (o == null || getClass() != o.getClass()) return false;
66 
67         ChildTextElement that = (ChildTextElement) o;
68 
69         return child.equals(that.child);
70 
71     }
72 
73     @Override
hashCode()74     public int hashCode() {
75         return child.hashCode();
76     }
77 
78     @Override
toString()79     public String toString() {
80         return "ChildTextElement{" + child + '}';
81     }
82 
83     @Override
isWhiteSpace()84     public boolean isWhiteSpace() {
85         return false;
86     }
87 
88     @Override
isSpaceOrTab()89     public boolean isSpaceOrTab() {
90         return false;
91     }
92 
93     @Override
isNewline()94     public boolean isNewline() {
95         return false;
96     }
97 
98     @Override
isComment()99     public boolean isComment() {
100         return child instanceof Comment;
101     }
102 
103     @Override
isChildOfClass(Class<? extends Node> nodeClass)104     public boolean isChildOfClass(Class<? extends Node> nodeClass) {
105         return nodeClass.isInstance(child);
106     }
107 
108     @Override
getRange()109     Optional<Range> getRange() {
110         return child.getRange();
111     }
112 }
113