JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/printer/concretesyntaxmodel/CsmConditional.java
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
22package com.github.javaparser.printer.concretesyntaxmodel;
23
24import com.github.javaparser.ast.Node;
25import com.github.javaparser.ast.NodeList;
26import com.github.javaparser.ast.observer.ObservableProperty;
27import com.github.javaparser.printer.SourcePrinter;
28
29import java.util.Arrays;
30import java.util.List;
31
32public class CsmConditional implements CsmElement {
33    private final Condition condition;
34    private final List<ObservablePropertyproperties;
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<ObservablePropertygetProperties() {
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 nodeObservableProperty 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 Nodevalue = property.getValueAsMultipleReference(node);
76                return value == null || value.isEmpty();
77            }
78            if (this == IS_NOT_EMPTY) {
79                NodeList<? extends Nodevalue = property.getValueAsMultipleReference(node);
80                return value != null && !value.isEmpty();
81            }
82            throw new UnsupportedOperationException(name());
83        }
84    }
85
86    public CsmConditional(ObservableProperty propertyCondition conditionCsmElement thenElementCsmElement 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<ObservablePropertypropertiesCondition conditionCsmElement thenElementCsmElement 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 propertyCondition conditionCsmElement thenElement) {
104        this(propertyconditionthenElement, new CsmNone());
105    }
106
107    @Override
108    public void prettyPrint(Node nodeSourcePrinter printer) {
109        boolean test = false;
110        for (ObservableProperty prop : properties) {
111            test = test || condition.evaluate(nodeprop);
112        }
113        if (test) {
114            thenElement.prettyPrint(nodeprinter);
115        } else {
116            elseElement.prettyPrint(nodeprinter);
117        }
118    }
119}
120
MembersX
CsmConditional:condition
CsmConditional:properties
CsmConditional:thenElement
CsmConditional:Condition:evaluate:Block:Block:value
CsmConditional:CsmConditional
CsmConditional:Condition:IS_PRESENT
CsmConditional:getElseElement
CsmConditional:Condition:IS_EMPTY
CsmConditional:Condition:FLAG
CsmConditional:prettyPrint
CsmConditional:Condition:IS_NOT_EMPTY
CsmConditional:Condition:evaluate
CsmConditional:elseElement
CsmConditional:getProperty
CsmConditional:prettyPrint:Block:test
CsmConditional:getCondition
CsmConditional:getProperties
CsmConditional:getThenElement
Members
X