Line data Source code
1 : // Copyright (C) 2020 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.acceptance.testsuite.change; 16 : 17 : import java.util.function.Consumer; 18 : import java.util.function.Function; 19 : 20 : /** 21 : * Builder for the end position of a range. Used by {@link TestCommentCreation} and {@link 22 : * TestRobotCommentCreation}. 23 : */ 24 : public class StartAwarePositionBuilder<T> { 25 : private final TestRange.Builder testRangeBuilder; 26 : private final Consumer<TestRange> rangeConsumer; 27 : private final Function<String, T> fileFunction; 28 : 29 : public StartAwarePositionBuilder( 30 : TestRange.Builder testRangeBuilder, 31 : Consumer<TestRange> rangeConsumer, 32 2 : Function<String, T> fileFunction) { 33 2 : this.testRangeBuilder = testRangeBuilder; 34 2 : this.rangeConsumer = rangeConsumer; 35 2 : this.fileFunction = fileFunction; 36 2 : } 37 : 38 : /** Line of the end position of the range. */ 39 : public PositionBuilder<FileBuilder<T>> toLine(int endLine) { 40 2 : return new PositionBuilder<>( 41 : endCharOffset -> { 42 : TestRange.Position end = 43 2 : TestRange.Position.builder().line(endLine).charOffset(endCharOffset).build(); 44 2 : TestRange range = testRangeBuilder.setEnd(end).build(); 45 2 : rangeConsumer.accept(range); 46 2 : return new FileBuilder<>(fileFunction); 47 : }); 48 : } 49 : }