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