OSDN Git Service

@Override追加
[jindolf/JinParser.git] / src / main / java / jp / sourceforge / jindolf / parser / DecodeErrorInfo.java
1 /*\r
2  * invalid Shift_JIS decoding information\r
3  *\r
4  * License : The MIT License\r
5  * Copyright(c) 2009 olyutorskii\r
6  */\r
7 \r
8 package jp.sourceforge.jindolf.parser;\r
9 \r
10 import java.util.Comparator;\r
11 \r
12 /**\r
13  * 不正な Shift_JIS デコードの情報。\r
14  * 1バイトもしくは2バイトで構成される。\r
15  * 1バイトの場合はおそらくエンコーディングに関するエラー。\r
16  * 2バイトの場合はおそらく文字集合に関するエラー。\r
17  */\r
18 public class DecodeErrorInfo{\r
19 \r
20     /** 出現位置順Comparator。 */\r
21     public static final Comparator<DecodeErrorInfo> POS_COMPARATOR =\r
22             new PosComparator();\r
23 \r
24     private final int charPos;\r
25     private final boolean has2ndFlag;\r
26     private final byte rawByte1st;\r
27     private final byte rawByte2nd;\r
28 \r
29     /**\r
30      * 下請けコンストラクタ。\r
31      * @param charPos デコードエラーで置き換えられた文字列の開始位置\r
32      * @param has2ndFlag 2バイト目が有効ならtrueを渡す。\r
33      * @param rawByte1st デコードエラーを引き起こした最初のバイト値\r
34      * @param rawByte2nd デコードエラーを引き起こした2番目のバイト値\r
35      * @throws IndexOutOfBoundsException charPosが負\r
36      */\r
37     private DecodeErrorInfo(int charPos,\r
38                               boolean has2ndFlag,\r
39                               byte rawByte1st,\r
40                               byte rawByte2nd)\r
41             throws IndexOutOfBoundsException{\r
42         if(charPos < 0) throw new IndexOutOfBoundsException();\r
43 \r
44         this.charPos = charPos;\r
45         this.has2ndFlag = has2ndFlag;\r
46         this.rawByte1st = rawByte1st;\r
47         this.rawByte2nd = rawByte2nd;\r
48 \r
49         return;\r
50     }\r
51 \r
52     /**\r
53      * コンストラクタ。\r
54      * @param charPos デコードエラーで置き換えられた文字列の開始位置\r
55      * @param rawByte1st デコードエラーを引き起こした最初のバイト値\r
56      * @param rawByte2nd デコードエラーを引き起こした2番目のバイト値\r
57      * @throws IndexOutOfBoundsException charPosが負\r
58      */\r
59     public DecodeErrorInfo(int charPos,\r
60                              byte rawByte1st,\r
61                              byte rawByte2nd)\r
62             throws IndexOutOfBoundsException{\r
63         this(charPos, true, rawByte1st, rawByte2nd);\r
64         return;\r
65     }\r
66 \r
67     /**\r
68      * コンストラクタ。\r
69      * @param charPos デコードエラーで置き換えられた文字列の開始位置\r
70      * @param rawByte1st デコードエラーを引き起こしたバイト値\r
71      * @throws IndexOutOfBoundsException charPosが負\r
72      */\r
73     public DecodeErrorInfo(int charPos,\r
74                              byte rawByte1st)\r
75             throws IndexOutOfBoundsException{\r
76         this(charPos, false, rawByte1st, (byte)0x00);\r
77         return;\r
78     }\r
79 \r
80     /**\r
81      * デコードエラーで置き換えられた文字列の開始位置を返す。\r
82      * @return デコードエラーで置き換えられた文字列の開始位置\r
83      */\r
84     public int getCharPosition(){\r
85         return this.charPos;\r
86     }\r
87 \r
88     /**\r
89      * 2バイト目の情報を持つか判定する。\r
90      * @return 2バイト目の情報を持つならtrue\r
91      */\r
92     public boolean has2nd(){\r
93         return this.has2ndFlag;\r
94     }\r
95 \r
96     /**\r
97      * 1バイト目の値を返す。\r
98      * @return 1バイト目の値\r
99      */\r
100     public byte getRawByte1st(){\r
101         return this.rawByte1st;\r
102     }\r
103 \r
104     /**\r
105      * 2バイト目の値を返す。\r
106      * @return 2バイト目の値\r
107      * @throws IllegalStateException 2バイト目の情報を把持していないとき\r
108      */\r
109     public byte getRawByte2nd() throws IllegalStateException{\r
110         if( ! this.has2ndFlag ) throw new IllegalStateException();\r
111         return this.rawByte2nd;\r
112     }\r
113 \r
114     /**\r
115      * 出現位置のみが違う複製オブジェクトを生成する。\r
116      * @param gap 出現位置から引きたい値。正の値なら文字開始位置に向かう。\r
117      * @return 複製オブジェクト\r
118      * @throws IndexOutOfBoundsException 再計算された出現位置が負\r
119      */\r
120     public DecodeErrorInfo createGappedClone(int gap)\r
121             throws IndexOutOfBoundsException{\r
122         DecodeErrorInfo result;\r
123 \r
124         int newPos = this.charPos - gap;\r
125         if(this.has2ndFlag){\r
126             result = new DecodeErrorInfo(newPos,\r
127                                          this.rawByte1st, this.rawByte2nd);\r
128         }else{\r
129             result = new DecodeErrorInfo(newPos, this.rawByte1st);\r
130         }\r
131 \r
132         return result;\r
133     }\r
134 \r
135     /**\r
136      * {@inheritDoc}\r
137      * @return {@inheritDoc}\r
138      */\r
139     @Override\r
140     public String toString(){\r
141         StringBuilder result = new StringBuilder();\r
142 \r
143         result.append("start:").append(this.charPos).append(' ');\r
144 \r
145         String hex;\r
146         hex = Integer.toHexString(this.rawByte1st & 0xff);\r
147         if(hex.length() <= 1) result.append('0');\r
148         result.append(hex);\r
149 \r
150         if(this.has2ndFlag){\r
151             hex = Integer.toHexString(this.rawByte2nd & 0xff);\r
152             result.append(':');\r
153             if(hex.length() <= 1) result.append('0');\r
154             result.append(hex);\r
155         }\r
156 \r
157         return result.toString();\r
158     }\r
159 \r
160     /**\r
161      * 出現位置で順序づける比較子。\r
162      */\r
163     private static class PosComparator\r
164             implements Comparator<DecodeErrorInfo> {\r
165 \r
166         /**\r
167          * コンストラクタ。\r
168          */\r
169         private PosComparator(){\r
170             super();\r
171             return;\r
172         }\r
173 \r
174         /**\r
175          * {@inheritDoc}\r
176          * @param info1 {@inheritDoc}\r
177          * @param info2 {@inheritDoc}\r
178          * @return {@inheritDoc}\r
179          */\r
180         @Override\r
181         public int compare(DecodeErrorInfo info1, DecodeErrorInfo info2){\r
182             int pos1;\r
183             int pos2;\r
184 \r
185             if(info1 == null) pos1 = -1;\r
186             else              pos1 = info1.charPos;\r
187 \r
188             if(info2 == null) pos2 = -1;\r
189             else              pos2 = info2.charPos;\r
190 \r
191             return pos1 - pos2;\r
192         }\r
193 \r
194     }\r
195 \r
196 }\r