/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.facebook.presto;

import com.facebook.presto.spi.Page;
import com.facebook.presto.spi.block.Block;
import com.facebook.presto.spi.block.BlockBuilder;
import com.facebook.presto.spi.block.BlockBuilderStatus;
import com.facebook.presto.spi.block.VariableWidthBlockBuilder;
import com.facebook.presto.spi.type.StandardTypes;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.type.ArrayType;
import com.google.common.collect.ImmutableList;
import io.airlift.slice.Slice;
import io.airlift.slice.Slices;

import java.util.List;
import java.util.Map;

import static com.facebook.presto.type.TypeJsonUtils.appendToBlockBuilder;
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.requireNonNull;

public class RowPageBuilder
{
    private final List<Type> types;

    public static RowPageBuilder rowPageBuilder(Type... types)
    {
        return rowPageBuilder(ImmutableList.copyOf(types));
    }

    public static RowPageBuilder rowPageBuilder(Iterable<Type> types)
    {
        return new RowPageBuilder(types);
    }

    private final List<BlockBuilder> builders;
    private long rowCount;

    RowPageBuilder(Iterable<Type> types)
    {
        this.types = ImmutableList.copyOf(requireNonNull(types, "types is null"));
        ImmutableList.Builder<BlockBuilder> builders = ImmutableList.builder();
        for (Type type : types) {
            builders.add(type.createBlockBuilder(new BlockBuilderStatus(), 1));
        }
        this.builders = builders.build();
        checkArgument(!this.builders.isEmpty(), "At least one value info is required");
    }

    public boolean isEmpty()
    {
        return rowCount == 0;
    }

    public RowPageBuilder row(Object... values)
    {
        checkArgument(values.length == builders.size(), "Expected %s values, but got %s", builders.size(), values.length);

        for (int channel = 0; channel < values.length; channel++) {
            append(channel, values[channel]);
        }
        rowCount++;
        return this;
    }

    public Page build()
    {
        Block[] blocks = new Block[builders.size()];
        for (int i = 0; i < blocks.length; i++) {
            blocks[i] = builders.get(i).build();
        }
        return new Page(blocks);
    }

    private void append(int channel, Object value)
    {
        BlockBuilder builder = builders.get(channel);

        Type type = types.get(channel);
        Class<?> javaType = type.getJavaType();
        if (value == null) {
            builder.appendNull();
        }
        else if (type.getTypeSignature().getBase().equals(StandardTypes.ARRAY) && value instanceof Iterable<?>) {
            BlockBuilder subBlockBuilder = ((ArrayType) type).getElementType().createBlockBuilder(new BlockBuilderStatus(), 100);
            for (Object subElement : (Iterable<?>) value) {
                appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);
            }
            type.writeObject(builder, subBlockBuilder);
        }
        else if (type.getTypeSignature().getBase().equals(StandardTypes.ROW) && value instanceof Iterable<?>) {
            BlockBuilder subBlockBuilder = new VariableWidthBlockBuilder(new BlockBuilderStatus());
            int field = 0;
            for (Object subElement : (Iterable<?>) value) {
                appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);
                field++;
            }
            type.writeObject(builder, subBlockBuilder);
        }
        else if (type.getTypeSignature().getBase().equals(StandardTypes.MAP) && value instanceof Map<?, ?>) {
            BlockBuilder subBlockBuilder = new VariableWidthBlockBuilder(new BlockBuilderStatus());
            for (Map.Entry<?, ?> entry : ((Map<?, ?>) value).entrySet()) {
                appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
                appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
            }
            type.writeObject(builder, subBlockBuilder);
        }
        else if (javaType == boolean.class) {
            type.writeBoolean(builder, (Boolean) value);
        }
        else if (javaType == long.class) {
            type.writeLong(builder, ((Number) value).longValue());
        }
        else if (javaType == double.class) {
            type.writeDouble(builder, (Double) value);
        }
        else if (javaType == Slice.class) {
            Slice slice = value instanceof Slice ? (Slice) value : Slices.utf8Slice((String) value);
            type.writeSlice(builder, slice, 0, slice.length());
        }
        else {
            type.writeObject(builder, value);
        }
    }
}

@@ -809,286 +809,65 @@
 spi.
-block.VariableWidthBlockBuilder;%0Aimport com.facebook.presto.spi.type.StandardTypes;%0Aimport com.facebook.presto.spi.type.Type;%0Aimport com.facebook.presto.type.ArrayType;%0Aimport com.google.common.collect.ImmutableList;%0Aimport io.airlift.slice.Slice;%0Aimport io.airlift.slice.Slices
+type.Type;%0Aimport com.google.common.collect.ImmutableList
 ;%0A%0Ai
@@ -891,30 +891,8 @@
 ist;
-%0Aimport java.util.Map;
 %0A%0Aim
@@ -2655,355 +2655,23 @@
 ect 
-value)%0A    %7B%0A        BlockBuilder builder = builders.get(channel);%0A%0A        Type type = types.get(channel);%0A        Class%3C?%3E javaType = type.getJavaType();%0A        if (value == null) %7B%0A            builder.appendNull();%0A        %7D%0A        else if (type.getTypeSignature().getBase().equals(StandardTypes.ARRAY) && value instanceof Iterable%3C?%3E) %7B%0A    
+element)%0A    %7B%0A
     
@@ -2679,36 +2679,33 @@
    BlockBuilder 
-subB
+b
 lockBuilder = ((
@@ -2706,2050 +2706,129 @@
 r = 
-((ArrayType) type).getElementType().createBlockBuilder(new BlockBuilderStatus(), 100);%0A            for (Object subElement : (Iterable%3C?%3E) value) %7B%0A                appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);%0A            %7D%0A            type.writeObject(builder, subBlockBuilder);%0A        %7D%0A        else if (type.getTypeSignature().getBase().equals(StandardTypes.ROW) && value instanceof Iterable%3C?%3E) %7B%0A            BlockBuilder subBlockBuilder = new VariableWidthBlockBuilder(new BlockBuilderStatus());%0A            int field = 0;%0A            for (Object subElement : (Iterable%3C?%3E) value) %7B%0A                appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);%0A                field++;%0A            %7D%0A            type.writeObject(builder, subBlockBuilder);%0A        %7D%0A        else if (
+builders.get(channel);%0A        Type type = 
 type
+s
 .get
-TypeSignature().getBase().equals(StandardTypes.MAP) && value instanceof Map%3C?, ?%3E) %7B%0A            BlockBuilder subBlockBuilder = new VariableWidthBlockBuilder(new BlockBuilderStatus());%0A            for (Map.Entry%3C?, ?%3E entry : ((Map%3C?, ?%3E) value).entrySet()) %7B%0A                appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);%0A                appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);%0A            %7D%0A            type.writeObject(builder, subBlockBuilder);%0A        %7D%0A        else if (javaType == boolean.class) %7B%0A            type.writeBoolean(builder, (Boolean) value);%0A        %7D%0A        else if (javaType == long.class) %7B%0A            type.writeLong(builder, ((Number) value).longValue());%0A        %7D%0A        else if (javaType == double.class) %7B%0A            type.writeDouble(builder, (Double) value);%0A        %7D%0A        else if (javaType == Slice.class) %7B%0A            Slice slice = value instanceof Slice ? (Slice) value : Slices.utf8Slice((String) value);%0A            type.writeSlice(builder, slice, 0, slice.length());%0A        %7D%0A        else %7B%0A            type.writeObject(builder, value);%0A        %7D
+(channel);%0A        appendToBlockBuilder(type, element, blockBuilder);
 %0A   

Remove duplicate code from RowPageBuilder test class