4 * License : The MIT License
5 * Copyright(c) 2009 olyutorskii
8 package jp.sourceforge.jindolf.parser;
10 import java.util.regex.MatchResult;
15 * 開始位置と終了位置が同じ場合、長さ0の範囲とみなされる。
17 * 開始位置が負の場合、もしくは開始位置より終了位置が小さい場合、
20 public class SeqRange{
27 * 開始位置、終了位置ともに無効状態となる。
36 * @param startPos 開始位置
39 public SeqRange(int startPos, int endPos){
41 this.startPos = startPos;
48 * @param startPos 開始位置
50 public void setStartPos(int startPos){
51 this.startPos = startPos;
59 public void setEndPos(int endPos){
66 * @param startPosition 開始位置
67 * @param endPosition 終了位置
69 public void setRange(int startPosition, int endPosition){
70 this.startPos = startPosition;
71 this.endPos = endPosition;
76 * 最後にマッチした前方参照グループの範囲で設定する。
77 * @param result 正規表現マッチ結果
78 * @param groupId グループ番号
79 * @throws IllegalStateException マッチしていない
80 * @throws IndexOutOfBoundsException グループ番号が不正
82 public void setLastMatchedGroupRange(MatchResult result, int groupId)
83 throws IllegalStateException,
84 IndexOutOfBoundsException {
85 this.startPos = result.start(groupId);
86 this.endPos = result.end(groupId);
92 * @param result 正規表現マッチ結果
93 * @throws IllegalStateException マッチしていない
95 public void setLastMatchedRange(MatchResult result)
96 throws IllegalStateException {
97 this.startPos = result.start();
98 this.endPos = result.end();
106 public int getStartPos(){
107 return this.startPos;
114 public int getEndPos(){
124 int length = this.endPos - this.startPos;
129 * 現在の範囲で与えられた文字列を切り出す。
130 * @param seq 切り出し元文字列
132 * @throws IndexOutOfBoundsException 範囲が無効
134 public CharSequence sliceSequence(CharSequence seq)
135 throws IndexOutOfBoundsException{
136 CharSequence result = seq.subSequence(this.startPos, this.endPos);
143 public void setInvalid(){
153 public boolean isValid(){
154 if (this.startPos < 0) return false;
155 else if(this.startPos > this.endPos) return false;