JavaParser Source Viewer

Home|JavaParser/com/github/javaparser/TokenRange.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;
23
24import java.util.Iterator;
25import java.util.Optional;
26
27import static com.github.javaparser.utils.Utils.assertNotNull;
28
29/**
30 * The range of tokens covered by this node.
31 */
32public class TokenRange implements Iterable<JavaToken> {
33    public static final TokenRange INVALID = new TokenRange(JavaToken.INVALIDJavaToken.INVALID);
34
35    private final JavaToken begin;
36    private final JavaToken end;
37
38    public TokenRange(JavaToken beginJavaToken end) {
39        this.begin = assertNotNull(begin);
40        this.end = assertNotNull(end);
41    }
42
43    public JavaToken getBegin() {
44        return begin;
45    }
46
47    public JavaToken getEnd() {
48        return end;
49    }
50
51    public Optional<RangetoRange() {
52        if (begin.getRange().isPresent() && end.getRange().isPresent()) {
53            return Optional.of(new Range(begin.getRange().get().beginend.getRange().get().end));
54        }
55        return Optional.empty();
56    }
57
58    public TokenRange withBegin(JavaToken begin) {
59        return new TokenRange(assertNotNull(begin), end);
60    }
61
62    public TokenRange withEnd(JavaToken end) {
63        return new TokenRange(beginassertNotNull(end));
64    }
65
66    @Override
67    public String toString() {
68        StringBuilder result = new StringBuilder();
69        for(JavaToken t: this) {
70            result.append(t.getText());
71        }
72        return result.toString();
73    }
74
75    @Override
76    public Iterator<JavaTokeniterator() {
77        return new Iterator<JavaToken>() {
78            private boolean hasNext = true;
79            private JavaToken current = begin;
80
81            @Override
82            public boolean hasNext() {
83                return hasNext;
84            }
85
86            @Override
87            public JavaToken next() {
88                JavaToken retval = current;
89                if(current == null){
90                    throw new IllegalStateException("Attempting to move past end of range.");
91                }
92                if (current == end) {
93                    hasNext = false;
94                }
95                current = current.getNextToken().orElse(null);
96                if(current == null && hasNext){
97                    throw new IllegalStateException("End token is not linked to begin token.");
98                }
99                return retval;
100            }
101        };
102    }
103}
104
MembersX
TokenRange:getEnd
TokenRange:iterator:Block:current
TokenRange:toString
TokenRange:withEnd
TokenRange:iterator:Block:hasNext
TokenRange:toString:Block:result
TokenRange:iterator:Block:next:Block:retval
TokenRange:end
TokenRange:TokenRange
TokenRange:toRange
TokenRange:withBegin
TokenRange:begin
TokenRange:getBegin
TokenRange:iterator
TokenRange:INVALID
TokenRange:iterator:Block:next
Members
X