| 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.ast.nodeTypes; |
| 23 | |
| 24 | import com.github.javaparser.Position; |
| 25 | import com.github.javaparser.Range; |
| 26 | import com.github.javaparser.ast.Node; |
| 27 | |
| 28 | import java.util.Optional; |
| 29 | |
| 30 | /** |
| 31 | * A node that has a Range, which is every Node. |
| 32 | */ |
| 33 | public interface NodeWithRange<N> { |
| 34 | Optional<Range> getRange(); |
| 35 | |
| 36 | N setRange(Range range); |
| 37 | |
| 38 | /** |
| 39 | * The begin position of this node in the source file. |
| 40 | */ |
| 41 | default Optional<Position> getBegin() { |
| 42 | return getRange().map(r -> r.begin); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * The end position of this node in the source file. |
| 47 | */ |
| 48 | default Optional<Position> getEnd() { |
| 49 | return getRange().map(r -> r.end); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @deprecated use {@link #containsWithinRange(Node)} instead. |
| 54 | */ |
| 55 | @Deprecated |
| 56 | default boolean containsWithin(Node other) { |
| 57 | return containsWithinRange(other); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Checks whether the range of the given {@code Node} is contained within the range of this {@code NodeWithRange}. |
| 62 | * Note that any range contains itself, i.e., for any node {@code n}, we have that |
| 63 | * {@code n.containsWithinRange(n) == true}. |
| 64 | * |
| 65 | * <b>Notice:</b> This method compares two nodes based on their ranges <i>only</i>, but <i>not</i> based on the |
| 66 | * storage unit of the two nodes. Therefore, this method may return {@code true} for a node that is contained in a |
| 67 | * different file than this {@code NodeWithRange}. You may wish to use {@link Node#isAncestorOf(Node)} instead. |
| 68 | * |
| 69 | * @param other the node whose range should be compared with this node's range. |
| 70 | * @return {@code true} if the given node's range is contained within this node's range, and {@code false} |
| 71 | * otherwise. |
| 72 | */ |
| 73 | default boolean containsWithinRange(Node other) { |
| 74 | if (getRange().isPresent() && other.getRange().isPresent()) { |
| 75 | return getRange().get().contains(other.getRange().get()); |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 |
Members