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 static com.github.javaparser.Position.pos; 25 26 /** 27 * A range of characters in a source file, from "begin" to "end", including the characters at "begin" and "end". 28 */ 29 public class Range { 30 public final Position begin; 31 public final Position end; 32 Range(Position begin, Position end)33 public Range(Position begin, Position end) { 34 if (begin == null) { 35 throw new IllegalArgumentException("begin can't be null"); 36 } 37 if (end == null) { 38 throw new IllegalArgumentException("end can't be null"); 39 } 40 this.begin = begin; 41 this.end = end; 42 } 43 range(Position begin, Position end)44 public static Range range(Position begin, Position end) { 45 return new Range(begin, end); 46 } 47 range(int beginLine, int beginColumn, int endLine, int endColumn)48 public static Range range(int beginLine, int beginColumn, int endLine, int endColumn) { 49 return new Range(pos(beginLine, beginColumn), pos(endLine, endColumn)); 50 } 51 withBeginColumn(int column)52 public Range withBeginColumn(int column) { 53 return range(begin.withColumn(column), end); 54 } 55 withBeginLine(int line)56 public Range withBeginLine(int line) { 57 return range(begin.withLine(line), end); 58 } 59 withEndColumn(int column)60 public Range withEndColumn(int column) { 61 return range(begin, end.withColumn(column)); 62 } 63 withEndLine(int line)64 public Range withEndLine(int line) { 65 return range(begin, end.withLine(line)); 66 } 67 withBegin(Position begin)68 public Range withBegin(Position begin) { 69 return range(begin, this.end); 70 } 71 withEnd(Position end)72 public Range withEnd(Position end) { 73 return range(this.begin, end); 74 } 75 76 /** 77 * As strictlyContains, but two exactly matching ranges are also considered contained one in each other. 78 */ contains(Range other)79 public boolean contains(Range other) { 80 return (begin.isBefore(other.begin) || begin.equals(other.begin)) && 81 (end.isAfter(other.end) || end.equals(other.end)); 82 } 83 84 /** 85 * Do this strictly contains other? It means that this has to be larger than other and it has to start as other 86 * or before and end as other or after. 87 */ strictlyContains(Range other)88 public boolean strictlyContains(Range other) { 89 return begin.isBefore(other.begin) && end.isAfter(other.end); 90 } 91 isBefore(Position position)92 public boolean isBefore(Position position) { 93 return end.isBefore(position); 94 } 95 isAfter(Position position)96 public boolean isAfter(Position position) { 97 return begin.isAfter(position); 98 } 99 100 @Override equals(Object o)101 public boolean equals(Object o) { 102 if (this == o) return true; 103 if (o == null || getClass() != o.getClass()) return false; 104 105 Range range = (Range) o; 106 107 return begin.equals(range.begin) && end.equals(range.end); 108 109 } 110 111 @Override hashCode()112 public int hashCode() { 113 return 31 * begin.hashCode() + end.hashCode(); 114 } 115 116 @Override toString()117 public String toString() { 118 return begin + "-" + end; 119 } 120 } 121