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;
23 
24 import com.github.javaparser.ast.Node;
25 import com.github.javaparser.ast.comments.CommentsCollection;
26 
27 import java.util.List;
28 import java.util.Optional;
29 import java.util.function.Consumer;
30 
31 import static com.github.javaparser.utils.Utils.EOL;
32 
33 /**
34  * The results given when parsing with an instance of JavaParser.
35  */
36 public class ParseResult<T> {
37     private final T result;
38     private final List<Problem> problems;
39     private final List<JavaToken> tokens;
40     private final CommentsCollection commentsCollection;
41 
42     /**
43      * General constructor.
44      *
45      * @param result the AST, or empty if it wasn't created.
46      * @param problems a list of encountered parsing problems.
47      * @param tokens the complete list of tokens that were parsed, or empty if parsing failed completely.
48      */
ParseResult(T result, List<Problem> problems, List<JavaToken> tokens, CommentsCollection commentsCollection)49     public ParseResult(T result, List<Problem> problems, List<JavaToken> tokens, CommentsCollection commentsCollection) {
50         this.commentsCollection = commentsCollection;
51         this.result = result;
52         this.problems = problems;
53         this.tokens = tokens;
54     }
55 
56     /**
57      * @return if parsing was successful, meaning no errors of any kind were encountered.
58      */
isSuccessful()59     public boolean isSuccessful() {
60         return problems.isEmpty() && result != null;
61     }
62 
63     /**
64      * Calls the consumer with the result if parsing was succesful.
65      */
ifSuccessful(Consumer<T> consumer)66     public void ifSuccessful(Consumer<T> consumer) {
67         if (isSuccessful()) {
68             consumer.accept(result);
69         }
70     }
71 
72     /**
73      * @return the list of encountered parsing problems. Empty when no problems were encountered.
74      */
getProblems()75     public List<Problem> getProblems() {
76         return problems;
77     }
78 
79     /**
80      * @return the <code>i</code>'th encountered parsing problem. May throw <code>IndexOutOfBoundsException</code>.
81      */
getProblem(int i)82     public Problem getProblem(int i) {
83         return getProblems().get(i);
84     }
85 
86     /**
87      * @return the complete list of tokens that were parsed, or empty if parsing failed completely.
88      * @deprecated lists of tokens are now kept in every node.
89      * Calling this method is comparable to calling getResult().get().getTokenRange().get()
90      */
91     @Deprecated
getTokens()92     public Optional<List<JavaToken>> getTokens() {
93         return Optional.ofNullable(tokens);
94     }
95 
96     /**
97      * @return the complete collection of comments encountered while parsing.
98      */
getCommentsCollection()99     public Optional<CommentsCollection> getCommentsCollection() {
100         return Optional.ofNullable(commentsCollection);
101     }
102 
103     /**
104      * @return the AST of the parsed source code, or empty if parsing failed completely.
105      */
getResult()106     public Optional<T> getResult() {
107         return Optional.ofNullable(result);
108     }
109 
110     @Override
toString()111     public String toString() {
112         if (isSuccessful()) {
113             return "Parsing successful";
114         }
115         StringBuilder message = new StringBuilder("Parsing failed:").append(EOL);
116         for (Problem problem : problems) {
117             message.append(problem.toString()).append(EOL);
118         }
119         return message.toString();
120     }
121 
122     /**
123      * A post processor that can be added to ParserConfiguration to add some processing right after parsing.
124      */
125     public interface PostProcessor {
process(ParseResult<? extends Node> result, ParserConfiguration configuration)126         void process(ParseResult<? extends Node> result, ParserConfiguration configuration);
127     }
128 }
129