1 28 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify; 29 30 public class Span { 31 32 private int _startLine; 33 private int _startColumn; 34 private int _endLine; 35 private int _endColumn; 36 37 public Span() {} 38 39 public Span(int startLine, int startColumn, int endLine, int endColumn) { 40 setStart(startLine, startColumn); 41 setEnd(endLine, endColumn); 42 } 43 44 public Span( Span span ) { 45 this( span.getStartLine(), span.getStartColumn(), 46 span.getEndLine(), span.getEndColumn() ); 47 } 48 49 public void setStart(int startLine, int startColumn) { 50 _startLine = startLine; 51 _startColumn = startColumn; 52 } 53 54 public void setEnd(int endLine, int endColumn) { 55 _endLine = endLine; 56 _endColumn = endColumn; 57 } 58 59 public int getStartLine() { 60 return _startLine; 61 } 62 63 public int getStartColumn() { 64 return _startColumn; 65 } 66 67 public int getEndLine() { 68 return _endLine; 69 } 70 71 public int getEndColumn() { 72 return _endColumn; 73 } 74 75 public boolean contains( Span span ) { 76 return ( contains(span.getStartLine(), span.getStartColumn()) 77 && contains( span.getEndLine(), span.getEndColumn()) ); 78 } 79 80 public boolean contains(int line, int column) { 81 boolean afterStart = false; 82 boolean beforeEnd = false; 83 84 if ( getStartLine() < line ) { 85 afterStart = true; 86 } 87 else if ( getStartLine() == line && getStartColumn() <= column ) { 88 afterStart = true; 89 } 90 91 if ( getEndLine() > line ) { 92 beforeEnd = true; 93 } 94 else if ( getEndLine() == line && getEndColumn() >= column ) { 95 beforeEnd = true; 96 } 97 98 return ( afterStart && beforeEnd ); 99 } 100 101 protected boolean startsBefore( Span span ) { 102 boolean result = false; 103 if ( getStartLine() < span.getStartLine() ) { 104 result = true; 105 } 106 else if ( getStartLine() == span.getStartLine() 107 && getStartColumn() <= span.getStartColumn() ) { 108 result = true; 109 } 110 111 return result; 112 } 113 114 protected boolean endsAfter( Span span ) { 115 boolean result = false; 116 if ( getEndLine() > span.getEndLine() ) { 117 result = true; 118 } 119 else if ( getEndLine() == span.getEndLine() 120 && getEndColumn() >= span.getEndColumn() ) { 121 result = true; 122 } 123 124 return result; 125 } 126 127 public void compose( Span span ) { 128 if ( span.startsBefore( this ) ) { 129 setStart( span.getStartLine(), span.getStartColumn() ); 130 } 131 if ( span.endsAfter( this ) ) { 132 setEnd( span.getEndLine(), span.getEndColumn() ); 133 } 134 } 135 136 public boolean equals(Object o) { 137 boolean result = false; 138 if ( o instanceof Span ) { 139 Span span = (Span)o; 140 result = ( span.getStartLine() == getStartLine() 141 && span.getStartColumn() == getStartColumn() 142 && span.getEndLine() == getEndLine() 143 && span.getEndColumn() == getEndColumn() ); 144 } 145 return result; 146 } 147 148 public String toString() { 149 return "[" + getStartLine() + "," + getStartColumn() 150 + ":" + getEndLine() + "," + getEndColumn() + "]"; 151 } 152 153 } 154 | Popular Tags |