/*
 * 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.operator;

import com.facebook.presto.spi.block.Block;
import com.google.common.base.Objects;

import java.util.Arrays;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

public class Page
{
    private final Block[] blocks;
    private final int positionCount;
    private final long sizeInBytes;

    public Page(Block... blocks)
    {
        this(determinePositionCount(blocks), blocks);
    }

    public Page(int positionCount, Block... blocks)
    {
        checkNotNull(blocks, "blocks is null");
        this.blocks = Arrays.copyOf(blocks, blocks.length);
        this.positionCount = positionCount;

        long sizeInBytes = 0;
        for (Block block : blocks) {
            sizeInBytes += block.getSizeInBytes();
        }
        this.sizeInBytes = sizeInBytes;
    }

    public int getChannelCount()
    {
        return blocks.length;
    }

    public int getPositionCount()
    {
        return positionCount;
    }

    public long getSizeInBytes()
    {
        return sizeInBytes;
    }

    public Block[] getBlocks()
    {
        return blocks.clone();
    }

    public Block getBlock(int channel)
    {
        return blocks[channel];
    }

    public Block[] getSingleValueBlocks(int position)
    {
        Block[] row = new Block[blocks.length];
        for (int i = 0; i < blocks.length; i++) {
            row[i] = blocks[i].getSingleValueBlock(position);
        }
        return row;
    }

    @Override
    public String toString()
    {
        return Objects.toStringHelper(this)
                .add("positionCount", positionCount)
                .add("channelCount", getChannelCount())
                .addValue("@" + Integer.toHexString(System.identityHashCode(this)))
                .toString();
    }

    private static int determinePositionCount(Block... blocks)
    {
        checkNotNull(blocks, "blocks is null");
        checkArgument(blocks.length != 0, "blocks is empty");

        return blocks[0].getPositionCount();
    }
}

@@ -641,47 +641,8 @@
 ock;
-%0Aimport com.google.common.base.Objects;
 %0A%0Aim
@@ -683,119 +683,36 @@
 tic 
-com.google.common.base.Preconditions.checkArgument;%0Aimport static com.google.common.base.Preconditions.checkNot
+java.util.Objects.requireNon
 Null
@@ -1004,32 +1004,34 @@
   %7B%0A        
-checkNot
+requireNon
 Null(blocks,
@@ -2034,82 +2034,104 @@
     
-return Objects.toStringHelper(this)%0A                .add(%22positionCount%22, 
+StringBuilder builder = new StringBuilder(%22Page%7B%22);%0A        builder.append(%22positions=%22).append(
 posi
@@ -2140,16 +2140,17 @@
 onCount)
+;
 %0A       
@@ -2154,37 +2154,45 @@
     
-        .ad
+builder.appen
 d(%22
+, 
 channel
-Count%22, 
+s=%22).append(
 getC
@@ -2205,16 +2205,17 @@
 Count())
+;
 %0A       
@@ -2219,32 +2219,64 @@
     
-        .addValue(%22@%22 + 
+builder.append('%7D');%0A        builder.append(%22@%22).append(
 Inte
@@ -2322,26 +2322,26 @@
 (this)))
+;
 %0A
- 
         
        .
@@ -2332,23 +2332,30 @@
         
-       
+return builder
 .toStrin
@@ -2447,16 +2447,18 @@
     
-checkNot
+requireNon
 Null
@@ -2497,21 +2497,11 @@
     
-checkArgument
+if 
 (blo
@@ -2515,14 +2515,63 @@
 gth 
-!= 0, 
+== 0) %7B%0A            throw new IllegalArgumentException(
 %22blo
@@ -2585,16 +2585,26 @@
 empty%22);
+%0A        %7D
 %0A%0A      

Remove Page dependencies on Guava