1 package com.github.javaparser.ast.nodeTypes; 2 3 import com.github.javaparser.Position; 4 import com.github.javaparser.Range; 5 import com.github.javaparser.ast.Node; 6 7 import java.util.Optional; 8 9 /** 10 * A node that has a Range, which is every Node. 11 * 12 */ 13 public interface NodeWithRange<N> { getRange()14 Optional<Range> getRange(); 15 setRange(Range range)16 N setRange(Range range); 17 18 /** 19 * The begin position of this node in the source file. 20 */ getBegin()21 default Optional<Position> getBegin() { 22 return getRange().map(r -> r.begin); 23 } 24 25 /** 26 * The end position of this node in the source file. 27 */ getEnd()28 default Optional<Position> getEnd() { 29 return getRange().map(r -> r.end); 30 } 31 containsWithin(Node other)32 default boolean containsWithin(Node other) { 33 if (getRange().isPresent() && other.getRange().isPresent()) { 34 return getRange().get().contains(other.getRange().get()); 35 } 36 return false; 37 } 38 39 /** 40 * @deprecated use isAfter() on range 41 */ 42 @Deprecated isPositionedAfter(Position position)43 default boolean isPositionedAfter(Position position) { 44 return getRange().map(r -> r.isAfter(position)).orElse(false); 45 } 46 47 /** 48 * @deprecated use isBefore() on range 49 */ 50 @Deprecated isPositionedBefore(Position position)51 default boolean isPositionedBefore(Position position) { 52 return getRange().map(r -> r.isBefore(position)).orElse(false); 53 } 54 } 55