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

import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.ResultSetMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
import java.util.OptionalInt;
import java.util.OptionalLong;

import static com.facebook.presto.raptor.util.DatabaseUtil.getOptionalInt;
import static com.facebook.presto.raptor.util.DatabaseUtil.getOptionalLong;
import static com.google.common.base.MoreObjects.toStringHelper;
import static java.util.Objects.requireNonNull;

public final class Table
{
    private final long tableId;
    private final OptionalLong distributionId;
    private final OptionalInt bucketCount;

    public Table(long tableId, OptionalLong distributionId, OptionalInt bucketCount)
    {
        this.tableId = tableId;
        this.distributionId = requireNonNull(distributionId, "distributionId is null");
        this.bucketCount = requireNonNull(bucketCount, "bucketCount is null");
    }

    public long getTableId()
    {
        return tableId;
    }

    public OptionalLong getDistributionId()
    {
        return distributionId;
    }

    public OptionalInt getBucketCount()
    {
        return bucketCount;
    }

    @Override
    public int hashCode()
    {
        return Objects.hash(tableId, bucketCount);
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == this) {
            return true;
        }
        if ((obj == null) || (getClass() != obj.getClass())) {
            return false;
        }
        Table o = (Table) obj;
        return tableId == o.tableId &&
                Objects.equals(bucketCount, o.bucketCount);
    }

    @Override
    public String toString()
    {
        return toStringHelper(this)
                .add("tableId", tableId)
                .add("distributionId", distributionId.isPresent() ? distributionId.getAsLong() : null)
                .add("bucketCount", bucketCount.isPresent() ? bucketCount.getAsInt() : null)
                .omitNullValues()
                .toString();
    }

    public static class TableMapper
            implements ResultSetMapper<Table>
    {
        @Override
        public Table map(int index, ResultSet r, StatementContext ctx)
                throws SQLException
        {
            return new Table(
                    r.getLong("table_id"),
                    getOptionalLong(r, "distribution_id"),
                    getOptionalInt(r, "bucket_count"));
        }
    }
}

@@ -1789,112 +1789,8 @@
  %7D%0A%0A
-    @Override%0A    public int hashCode()%0A    %7B%0A        return Objects.hash(tableId, bucketCount);%0A    %7D%0A%0A
     
@@ -1833,18 +1833,16 @@
 Object o
-bj
 )%0A    %7B%0A
@@ -1857,19 +1857,17 @@
 if (
-obj == this
+this == o
 ) %7B%0A
@@ -1917,20 +1917,17 @@
 if (
-(obj
+o
  == null
 ) %7C%7C
@@ -1926,14 +1926,12 @@
 null
-)
  %7C%7C 
-(
 getC
@@ -1941,18 +1941,16 @@
 s() != o
-bj
 .getClas
@@ -1953,17 +1953,16 @@
 Class())
-)
  %7B%0A     
@@ -2006,17 +2006,21 @@
   Table 
-o
+table
  = (Tabl
@@ -2023,18 +2023,16 @@
 Table) o
-bj
 ;%0A      
@@ -2051,17 +2051,21 @@
 leId == 
-o
+table
 .tableId
@@ -2103,23 +2103,219 @@
 als(
-bucketCount, o.
+distributionId, table.distributionId) &&%0A                Objects.equals(bucketCount, table.bucketCount);%0A    %7D%0A%0A    @Override%0A    public int hashCode()%0A    %7B%0A        return Objects.hash(tableId, distributionId, 
 buck

Include distribution ID in equals and hash code for Table