| 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.concretesyntaxmodel; |
| 23 | |
| 24 | import com.github.javaparser.ast.Node; |
| 25 | import com.github.javaparser.ast.NodeList; |
| 26 | import com.github.javaparser.ast.observer.ObservableProperty; |
| 27 | import com.github.javaparser.printer.SourcePrinter; |
| 28 | |
| 29 | import java.util.Arrays; |
| 30 | import java.util.List; |
| 31 | |
| 32 | public class CsmConditional implements CsmElement { |
| 33 | private final Condition condition; |
| 34 | private final List<ObservableProperty> properties; |
| 35 | private final CsmElement thenElement; |
| 36 | private final CsmElement elseElement; |
| 37 | |
| 38 | public Condition getCondition() { |
| 39 | return condition; |
| 40 | } |
| 41 | |
| 42 | public ObservableProperty getProperty() { |
| 43 | if (properties.size() > 1) { |
| 44 | throw new IllegalStateException(); |
| 45 | } |
| 46 | return properties.get(0); |
| 47 | } |
| 48 | |
| 49 | public List<ObservableProperty> getProperties() { |
| 50 | return properties; |
| 51 | } |
| 52 | |
| 53 | public CsmElement getThenElement() { |
| 54 | return thenElement; |
| 55 | } |
| 56 | |
| 57 | public CsmElement getElseElement() { |
| 58 | return elseElement; |
| 59 | } |
| 60 | |
| 61 | public enum Condition { |
| 62 | IS_EMPTY, |
| 63 | IS_NOT_EMPTY, |
| 64 | IS_PRESENT, |
| 65 | FLAG; |
| 66 | |
| 67 | boolean evaluate(Node node, ObservableProperty property){ |
| 68 | if (this == IS_PRESENT) { |
| 69 | return !property.isNullOrNotPresent(node); |
| 70 | } |
| 71 | if (this == FLAG) { |
| 72 | return property.getValueAsBooleanAttribute(node); |
| 73 | } |
| 74 | if (this == IS_EMPTY) { |
| 75 | NodeList<? extends Node> value = property.getValueAsMultipleReference(node); |
| 76 | return value == null || value.isEmpty(); |
| 77 | } |
| 78 | if (this == IS_NOT_EMPTY) { |
| 79 | NodeList<? extends Node> value = property.getValueAsMultipleReference(node); |
| 80 | return value != null && !value.isEmpty(); |
| 81 | } |
| 82 | throw new UnsupportedOperationException(name()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public CsmConditional(ObservableProperty property, Condition condition, CsmElement thenElement, CsmElement elseElement) { |
| 87 | this.properties = Arrays.asList(property); |
| 88 | this.condition = condition; |
| 89 | this.thenElement = thenElement; |
| 90 | this.elseElement = elseElement; |
| 91 | } |
| 92 | |
| 93 | public CsmConditional(List<ObservableProperty> properties, Condition condition, CsmElement thenElement, CsmElement elseElement) { |
| 94 | if (properties.size() < 1) { |
| 95 | throw new IllegalArgumentException(); |
| 96 | } |
| 97 | this.properties = properties; |
| 98 | this.condition = condition; |
| 99 | this.thenElement = thenElement; |
| 100 | this.elseElement = elseElement; |
| 101 | } |
| 102 | |
| 103 | public CsmConditional(ObservableProperty property, Condition condition, CsmElement thenElement) { |
| 104 | this(property, condition, thenElement, new CsmNone()); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void prettyPrint(Node node, SourcePrinter printer) { |
| 109 | boolean test = false; |
| 110 | for (ObservableProperty prop : properties) { |
| 111 | test = test || condition.evaluate(node, prop); |
| 112 | } |
| 113 | if (test) { |
| 114 | thenElement.prettyPrint(node, printer); |
| 115 | } else { |
| 116 | elseElement.prettyPrint(node, printer); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 |
Members