Line data Source code
1 : // Copyright (C) 2019 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.prettify.common; 16 : 17 : import com.google.common.collect.ImmutableList; 18 : import com.google.gerrit.prettify.common.SparseFileContent.Range; 19 : 20 : /** 21 : * A builder for creating immutable {@link SparseFileContent}. Lines can be only be added in 22 : * sequential (increased) order 23 : */ 24 : public class SparseFileContentBuilder { 25 : private final ImmutableList.Builder<Range> ranges; 26 : private final int size; 27 : private int lastRangeBase; 28 : private int lastRangeEnd; 29 : private ImmutableList.Builder<String> lastRangeLines; 30 : 31 15 : public SparseFileContentBuilder(int size) { 32 15 : ranges = new ImmutableList.Builder<>(); 33 15 : startNextRange(0); 34 15 : this.size = size; 35 15 : } 36 : 37 : public void addLine(int lineNumber, String content) { 38 15 : if (lineNumber < 0) { 39 1 : throw new IllegalArgumentException("Line number must be non-negative"); 40 : } 41 : // if (lineNumber >= size) { 42 : // The following 4 tests are failed if you uncomment this condition: 43 : // 44 : // 45 : // diffOfFileWithMultilineRebaseHunkRemovingNewlineAtEndOfFileAndWithCommentReturnsFileContents 46 : // 47 : // diffOfFileWithMultilineRebaseHunkAddingNewlineAtEndOfFileAndWithCommentReturnsFileContents 48 : // 49 : // 50 : // diffOfFileWithMultilineRebaseHunkRemovingNewlineAtEndOfFileAndWithCommentReturnsFileContents 51 : // 52 : // diffOfFileWithMultilineRebaseHunkAddingNewlineAtEndOfFileAndWithCommentReturnsFileContents 53 : // Tests are failed because there are some bug with diff calculation. 54 : // The condition must be uncommented after all these bugs are fixed. 55 : // Also don't forget to remove ignore from for SparseFileContentBuilder 56 : // throw new IllegalArgumentException(String.format("The zero-based line number %d is after 57 : // the end of file. The file size is %d line(s).", lineNumber, size)); 58 : // } 59 15 : if (lineNumber < lastRangeEnd) { 60 1 : throw new IllegalArgumentException( 61 1 : String.format( 62 : "Invalid line number %d. You are trying to add a line before an already added line" 63 : + " %d", 64 1 : lineNumber, lastRangeEnd)); 65 : } 66 15 : if (lineNumber > lastRangeEnd) { 67 7 : finishLastRange(); 68 7 : startNextRange(lineNumber); 69 : } 70 15 : lastRangeLines.add(content); 71 15 : lastRangeEnd++; 72 15 : } 73 : 74 : private void startNextRange(int base) { 75 15 : lastRangeLines = new ImmutableList.Builder<>(); 76 15 : lastRangeBase = lastRangeEnd = base; 77 15 : } 78 : 79 : private void finishLastRange() { 80 15 : if (lastRangeEnd > lastRangeBase) { 81 15 : ranges.add(Range.create(lastRangeBase, lastRangeLines.build())); 82 15 : lastRangeLines = null; 83 : } 84 15 : } 85 : 86 : public SparseFileContent build() { 87 15 : finishLastRange(); 88 15 : return SparseFileContent.create(ranges.build(), size); 89 : } 90 : }