OSDN Git Service

modify appendGappedErrorInfo()
authorOlyutorskii <olyutorskii@users.osdn.me>
Sun, 25 Mar 2018 03:20:43 +0000 (12:20 +0900)
committerOlyutorskii <olyutorskii@users.osdn.me>
Sun, 25 Mar 2018 03:20:43 +0000 (12:20 +0900)
src/main/java/jp/osdn/jindolf/parser/content/DecodedContent.java
src/test/java/jp/osdn/jindolf/parser/content/DecodedContentTest.java

index 3370268..9bfcd61 100644 (file)
@@ -98,38 +98,44 @@ public class DecodedContent
      * 必要に応じてエラーリストが生成され
      * 戻り値となる場合がありうる。
      *
-     * @param sourceContent 元の文字列
-     * @param startPos 範囲開始位置
-     * @param endPos 範囲終了位置
-     * @param targetError 追加先エラーリスト。nullでもよい。
+     * @param srcErrList 追加元エラーリスト
+     * @param startCharPos 範囲開始位置
+     * @param endCharPos 範囲終了位置
+     * @param dstErrList 追加先エラーリスト。nullでもよい。
+     *     追加元と異なるリストでなければならない。
      * @param gap ギャップ量
      * @return 引数targetErrorもしくは新規生成されたリストを返す。
+     *     なにもコピーされなければnullを返す。
+     * @throws IllegalArgumentException 追加元リストと追加先リストが
+     *     同一インスタンス
      */
     protected static List<DecodeErrorInfo>
-            appendGappedErrorInfo(DecodedContent sourceContent,
-                                  int startPos, int endPos,
-                                  List<DecodeErrorInfo> targetError,
+            appendGappedErrorInfo(List<DecodeErrorInfo> srcErrList,
+                                  int startCharPos, int endCharPos,
+                                  List<DecodeErrorInfo> dstErrList,
                                   int gap){
-        if(startPos >= endPos) return targetError;
+        if(srcErrList == dstErrList) throw new IllegalArgumentException();
 
-        List<DecodeErrorInfo> errList = sourceContent.decodeError;
-        int errSize = errList.size();
-        assert errSize > 0;
+        if(startCharPos >= endCharPos) return dstErrList;
+
+        int errSize = srcErrList.size();
 
         int startErrorIdx;
         int endErrorIdx;
 
-        startErrorIdx = DecodeErrorInfo.searchErrorIndex(errList, startPos);
+        startErrorIdx =
+                DecodeErrorInfo.searchErrorIndex(srcErrList, startCharPos);
         if(startErrorIdx >= errSize){
             return null;
         }
 
-        int lastCharPos = endPos - 1;
-        endErrorIdx = DecodeErrorInfo.searchErrorIndex(errList, lastCharPos);
+        int lastCharPos = endCharPos - 1;
+        endErrorIdx =
+                DecodeErrorInfo.searchErrorIndex(srcErrList, lastCharPos);
         if(endErrorIdx >= errSize){
             endErrorIdx = errSize - 1;
         }else{
-            DecodeErrorInfo lastErrorInfo = errList.get(endErrorIdx);
+            DecodeErrorInfo lastErrorInfo = srcErrList.get(endErrorIdx);
             boolean isLastErrorInfoOnLastPos =
                     lastErrorInfo.getCharPosition() == lastCharPos;
             if( ! isLastErrorInfoOnLastPos){
@@ -144,10 +150,10 @@ public class DecodedContent
         }
 
         List<DecodeErrorInfo> result;
-        if(targetError == null) result = createErrorList();
-        else                    result = targetError;
+        if(dstErrList == null) result = createErrorList();
+        else                   result = dstErrList;
 
-        copyGappedErrorInfo(errList,
+        copyGappedErrorInfo(srcErrList,
                             startErrorIdx, endErrorIdx,
                             result, gap);
         return result;
@@ -440,7 +446,12 @@ public class DecodedContent
         int oldLength = this.rawContent.length();
 
         this.rawContent.append(source.rawContent, startPos, endPos);
-        if( ! source.hasDecodeError() ) return this;
+        List<DecodeErrorInfo> sourceErrorList;
+        if(source.hasDecodeError()){
+            sourceErrorList = source.decodeError;
+        }else{
+            return this;
+        }
 
         List<DecodeErrorInfo> targetErrorList;
         if(source != this) targetErrorList = this.decodeError;
@@ -448,7 +459,7 @@ public class DecodedContent
 
         int gap = startPos - oldLength;
 
-        targetErrorList = appendGappedErrorInfo(source,
+        targetErrorList = appendGappedErrorInfo(sourceErrorList,
                                                 startPos, endPos,
                                                 targetErrorList,
                                                 gap);
index b05a27e..73c88a4 100644 (file)
@@ -5,6 +5,7 @@
 
 package jp.osdn.jindolf.parser.content;
 
+import java.util.ArrayList;
 import java.util.List;
 import org.junit.After;
 import org.junit.AfterClass;
@@ -789,19 +790,100 @@ public class DecodedContentTest {
     public void testAppendGappedErrorInfo(){
         System.out.println("appendGappedErrorInfo");
 
-        DecodedContent sourceContent;
-        sourceContent = new DecodedContent();
+        List<DecodeErrorInfo> srcErrList = new ArrayList<>();
         for(int pos = 0; pos <= 50; pos += 10){
-            sourceContent.append("123456789");
-            sourceContent.addDecodeError((byte)0x00);
+            DecodeErrorInfo info = new DecodeErrorInfo(pos, (byte)0x00);
+            srcErrList.add(info);
         }
 
         List<DecodeErrorInfo> result;
-        result = DecodedContent.appendGappedErrorInfo(sourceContent, 15, 35, null, -100);
+        List<DecodeErrorInfo> target;
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 15, 35, null, -100);
         assertNotNull(result);
         assertEquals(2, result.size());
-        assertEquals(119, result.get(0).getCharPosition());
-        assertEquals(129, result.get(1).getCharPosition());
+        assertEquals(120, result.get(0).getCharPosition());
+        assertEquals(130, result.get(1).getCharPosition());
+
+        target = new ArrayList<>();
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 15, 35, target, -100);
+        assertSame(target, result);
+        assertEquals(2, result.size());
+        assertEquals(120, result.get(0).getCharPosition());
+        assertEquals(130, result.get(1).getCharPosition());
+
+        try{
+            DecodedContent.appendGappedErrorInfo(srcErrList, 15, 35, srcErrList, -100);
+            fail();
+        }catch(IllegalArgumentException e){
+            // GOOD
+        }
+
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 10, 40, null, -100);
+        assertNotNull(result);
+        assertEquals(3, result.size());
+        assertEquals(110, result.get(0).getCharPosition());
+        assertEquals(120, result.get(1).getCharPosition());
+        assertEquals(130, result.get(2).getCharPosition());
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 10, 41, null, -100);
+        assertNotNull(result);
+        assertEquals(4, result.size());
+        assertEquals(110, result.get(0).getCharPosition());
+        assertEquals(120, result.get(1).getCharPosition());
+        assertEquals(130, result.get(2).getCharPosition());
+        assertEquals(140, result.get(3).getCharPosition());
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 11, 40, null, -100);
+        assertNotNull(result);
+        assertEquals(2, result.size());
+        assertEquals(120, result.get(0).getCharPosition());
+        assertEquals(130, result.get(1).getCharPosition());
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 9, 40, null, -100);
+        assertNotNull(result);
+        assertEquals(3, result.size());
+        assertEquals(110, result.get(0).getCharPosition());
+        assertEquals(120, result.get(1).getCharPosition());
+        assertEquals(130, result.get(2).getCharPosition());
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 10, 50, null, -100);
+        assertNotNull(result);
+        assertEquals(4, result.size());
+        assertEquals(110, result.get(0).getCharPosition());
+        assertEquals(120, result.get(1).getCharPosition());
+        assertEquals(130, result.get(2).getCharPosition());
+        assertEquals(140, result.get(3).getCharPosition());
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 10, 51, null, -100);
+        assertNotNull(result);
+        assertEquals(5, result.size());
+        assertEquals(110, result.get(0).getCharPosition());
+        assertEquals(120, result.get(1).getCharPosition());
+        assertEquals(130, result.get(2).getCharPosition());
+        assertEquals(140, result.get(3).getCharPosition());
+        assertEquals(150, result.get(4).getCharPosition());
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 0, 0, null, -100);
+        assertNull(result);
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 0, 1, null, -100);
+        assertNotNull(result);
+        assertEquals(1, result.size());
+        assertEquals(100, result.get(0).getCharPosition());
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 10, 10, null, -100);
+        assertNull(result);
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 15, 15, null, -100);
+        assertNull(result);
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 15, 16, null, -100);
+        assertNull(result);
+
+        result = DecodedContent.appendGappedErrorInfo(srcErrList, 60, 70, null, -100);
+        assertNull(result);
 
         return;
     }