| 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 | |
| 22 | package com.github.javaparser.printer.lexicalpreservation; |
| 23 | |
| 24 | import com.github.javaparser.TokenTypes; |
| 25 | import com.github.javaparser.ast.type.PrimitiveType; |
| 26 | import com.github.javaparser.printer.concretesyntaxmodel.CsmElement; |
| 27 | import com.github.javaparser.printer.concretesyntaxmodel.CsmIndent; |
| 28 | import com.github.javaparser.printer.concretesyntaxmodel.CsmToken; |
| 29 | import com.github.javaparser.printer.concretesyntaxmodel.CsmUnindent; |
| 30 | |
| 31 | public class Kept implements DifferenceElement { |
| 32 | private final CsmElement element; |
| 33 | |
| 34 | Kept(CsmElement element) { |
| 35 | this.element = element; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public String toString() { |
| 40 | return "Kept{" + element + '}'; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | public boolean equals(Object o) { |
| 45 | if (this == o) return true; |
| 46 | if (o == null || getClass() != o.getClass()) return false; |
| 47 | |
| 48 | Kept kept = (Kept) o; |
| 49 | |
| 50 | return element.equals(kept.element); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public int hashCode() { |
| 55 | return element.hashCode(); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public CsmElement getElement() { |
| 60 | return element; |
| 61 | } |
| 62 | |
| 63 | public int getTokenType() { |
| 64 | if (isToken()) { |
| 65 | CsmToken csmToken = (CsmToken) element; |
| 66 | return csmToken.getTokenType(); |
| 67 | } |
| 68 | |
| 69 | throw new IllegalStateException("Kept is not a " + CsmToken.class.getSimpleName()); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public boolean isAdded() { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | public boolean isIndent() { return element instanceof CsmIndent; } |
| 78 | |
| 79 | public boolean isUnindent() { return element instanceof CsmUnindent; } |
| 80 | |
| 81 | public boolean isToken() { return element instanceof CsmToken; } |
| 82 | |
| 83 | public boolean isChild() { return element instanceof LexicalDifferenceCalculator.CsmChild; } |
| 84 | |
| 85 | public boolean isPrimitiveType() { |
| 86 | if (isChild()) { |
| 87 | LexicalDifferenceCalculator.CsmChild csmChild = (LexicalDifferenceCalculator.CsmChild) element; |
| 88 | return csmChild.getChild() instanceof PrimitiveType; |
| 89 | } |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | public boolean isWhiteSpace() { |
| 95 | if(isToken()) { |
| 96 | CsmToken csmToken = (CsmToken) element; |
| 97 | return csmToken.isWhiteSpace(); |
| 98 | } |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | public boolean isWhiteSpaceOrComment() { |
| 104 | if (isToken()) { |
| 105 | CsmToken csmToken = (CsmToken) element; |
| 106 | return TokenTypes.isWhitespaceOrComment(csmToken.getTokenType()); |
| 107 | } |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | public boolean isNewLine() { |
| 113 | if(isToken()) { |
| 114 | CsmToken csmToken = (CsmToken) element; |
| 115 | return csmToken.isNewLine(); |
| 116 | } |
| 117 | |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public boolean isRemoved() { |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 |
Members