aboutsummaryrefslogtreecommitdiff
path: root/javaparser-core/src/main/java/com/github/javaparser/ast/nodeTypes/NodeWithRange.java
blob: d1e2fb7677530631b462b1b0e8c62162a8b41c99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.github.javaparser.ast.nodeTypes;

import com.github.javaparser.Position;
import com.github.javaparser.Range;
import com.github.javaparser.ast.Node;

import java.util.Optional;

/**
 * A node that has a Range, which is every Node.
 * 
 */
public interface NodeWithRange<N> {
    Optional<Range> getRange();

    N setRange(Range range);

    /**
     * The begin position of this node in the source file.
     */
    default Optional<Position> getBegin() {
        return getRange().map(r -> r.begin);
    }

    /**
     * The end position of this node in the source file.
     */
    default Optional<Position> getEnd() {
        return getRange().map(r -> r.end);
    }

    default boolean containsWithin(Node other) {
        if (getRange().isPresent() && other.getRange().isPresent()) {
            return getRange().get().contains(other.getRange().get());
        }
        return false;
    }

    /**
     * @deprecated use isAfter() on range
     */
    @Deprecated
    default boolean isPositionedAfter(Position position) {
        return getRange().map(r -> r.isAfter(position)).orElse(false);
    }

    /**
     * @deprecated use isBefore() on range
     */
    @Deprecated
    default boolean isPositionedBefore(Position position) {
        return getRange().map(r -> r.isBefore(position)).orElse(false);
    }
}