1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *    http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.apache.arrow.vector.complex;
19 
20 import org.apache.arrow.util.Preconditions;
21 import org.apache.arrow.vector.UInt4Vector;
22 
23 /**
24  * A helper class that is used to track and populate empty values in repeated value vectors.
25  */
26 public class EmptyValuePopulator {
27   private final UInt4Vector offsets;
28 
EmptyValuePopulator(UInt4Vector offsets)29   public EmptyValuePopulator(UInt4Vector offsets) {
30     this.offsets = Preconditions.checkNotNull(offsets, "offsets cannot be null");
31   }
32 
33   /**
34    * Marks all values since the last set as empty. The last set value is obtained from underlying offsets vector.
35    *
36    * @param lastIndex the last index (inclusive) in the offsets vector until which empty population takes place
37    * @throws java.lang.IndexOutOfBoundsException if lastIndex is negative or greater than offsets capacity.
38    */
populate(int lastIndex)39   public void populate(int lastIndex) {
40     if (lastIndex < 0) {
41       throw new IndexOutOfBoundsException("index cannot be negative");
42     }
43     final int lastSet = Math.max(offsets.getValueCount() - 1, 0);
44     final int previousEnd = offsets.get(lastSet); //0 ? 0 : accessor.get(lastSet);
45     for (int i = lastSet; i < lastIndex; i++) {
46       offsets.setSafe(i + 1, previousEnd);
47     }
48     offsets.setValueCount(lastIndex + 1);
49   }
50 
51 }
52