OSDN Git Service

改行コード指定
[jindolf/JinParser.git] / src / main / java / jp / sourceforge / jindolf / parser / SeqRange.java
1 /*
2  * range of string
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package jp.sourceforge.jindolf.parser;
9
10 import java.util.regex.MatchResult;
11
12 /**
13  * 文字列の範囲を表す。
14  * 範囲は開始位置と終了位置で表される。
15  * 開始位置と終了位置が同じ場合、長さ0の範囲とみなされる。
16  * 開始位置0は文字列の左端を表す。
17  * 開始位置が負の場合、もしくは開始位置より終了位置が小さい場合、
18  * このオブジェクトは無効とみなされる。
19  */
20 public class SeqRange{
21
22     private int startPos;
23     private int endPos;
24
25     /**
26      * コンストラクタ。
27      * 開始位置、終了位置ともに無効状態となる。
28      */
29     public SeqRange(){
30         this(-1, -1);
31         return;
32     }
33
34     /**
35      * コンストラクタ。
36      * @param startPos 開始位置
37      * @param endPos 終了位置
38      */
39     public SeqRange(int startPos, int endPos){
40         super();
41         this.startPos = startPos;
42         this.endPos   = endPos;
43         return;
44     }
45
46     /**
47      * 開始位置を設定する。
48      * @param startPos 開始位置
49      */
50     public void setStartPos(int startPos){
51         this.startPos = startPos;
52         return;
53     }
54
55     /**
56      * 終了位置を設定する。
57      * @param endPos 終了位置
58      */
59     public void setEndPos(int endPos){
60         this.endPos = endPos;
61         return;
62     }
63
64     /**
65      * 開始位置と終了位置を設定する。
66      * @param startPosition 開始位置
67      * @param endPosition 終了位置
68      */
69     public void setRange(int startPosition, int endPosition){
70         this.startPos = startPosition;
71         this.endPos   = endPosition;
72         return;
73     }
74
75     /**
76      * 最後にマッチした前方参照グループの範囲で設定する。
77      * @param result 正規表現マッチ結果
78      * @param groupId グループ番号
79      * @throws IllegalStateException マッチしていない
80      * @throws IndexOutOfBoundsException グループ番号が不正
81      */
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);
87         return;
88     }
89
90     /**
91      * 最後にマッチした範囲全体で設定する。
92      * @param result 正規表現マッチ結果
93      * @throws IllegalStateException マッチしていない
94      */
95     public void setLastMatchedRange(MatchResult result)
96             throws IllegalStateException {
97         this.startPos = result.start();
98         this.endPos   = result.end();
99         return;
100     }
101
102     /**
103      * 開始位置を取得する。
104      * @return 開始位置
105      */
106     public int getStartPos(){
107         return this.startPos;
108     }
109
110     /**
111      * 終了位置を取得する。
112      * @return 終了位置
113      */
114     public int getEndPos(){
115         return this.endPos;
116     }
117
118     /**
119      * 範囲の長さを得る。
120      * 内容が無効な場合、負の値もありえる。
121      * @return 長さ
122      */
123     public int length(){
124         int length = this.endPos - this.startPos;
125         return length;
126     }
127
128     /**
129      * 現在の範囲で与えられた文字列を切り出す。
130      * @param seq 切り出し元文字列
131      * @return 切り出された文字列
132      * @throws IndexOutOfBoundsException 範囲が無効
133      */
134     public CharSequence sliceSequence(CharSequence seq)
135             throws IndexOutOfBoundsException{
136         CharSequence result = seq.subSequence(this.startPos, this.endPos);
137         return result;
138     }
139
140     /**
141      * 範囲指定を無効にする。
142      */
143     public void setInvalid(){
144         this.startPos = -1;
145         this.endPos   = -1;
146         return;
147     }
148
149     /**
150      * 範囲指定が有効か判定する。
151      * @return 有効であればtrue
152      */
153     public boolean isValid(){
154         if     (this.startPos < 0)           return false;
155         else if(this.startPos > this.endPos) return false;
156         return true;
157     }
158
159 }