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

import com.facebook.presto.spi.ConnectorColumnHandle;
import com.facebook.presto.spi.ConnectorHandleResolver;
import com.facebook.presto.spi.ConnectorIndexHandle;
import com.facebook.presto.spi.ConnectorSplit;
import com.facebook.presto.spi.ConnectorTableHandle;

import javax.inject.Inject;

import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

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

public class HandleResolver
{
    private final ConcurrentMap<String, ConnectorHandleResolver> handleIdResolvers = new ConcurrentHashMap<>();

    public HandleResolver()
    {
    }

    @Inject
    public HandleResolver(Map<String, ConnectorHandleResolver> handleIdResolvers)
    {
        this.handleIdResolvers.putAll(handleIdResolvers);
    }

    public void addHandleResolver(String id, ConnectorHandleResolver connectorHandleResolver)
    {
        ConnectorHandleResolver existingResolver = handleIdResolvers.putIfAbsent(id, connectorHandleResolver);
        checkState(existingResolver == null, "Id %s is already assigned to resolver %s", id, existingResolver);
    }

    public String getId(ConnectorTableHandle tableHandle)
    {
        for (Entry<String, ConnectorHandleResolver> entry : handleIdResolvers.entrySet()) {
            if (entry.getValue().canHandle(tableHandle)) {
                return entry.getKey();
            }
        }
        throw new IllegalArgumentException("No connector for table handle: " + tableHandle);
    }

    public String getId(ConnectorColumnHandle columnHandle)
    {
        for (Entry<String, ConnectorHandleResolver> entry : handleIdResolvers.entrySet()) {
            if (entry.getValue().canHandle(columnHandle)) {
                return entry.getKey();
            }
        }
        throw new IllegalArgumentException("No connector for column handle: " + columnHandle);
    }

    public String getId(ConnectorSplit split)
    {
        for (Entry<String, ConnectorHandleResolver> entry : handleIdResolvers.entrySet()) {
            if (entry.getValue().canHandle(split)) {
                return entry.getKey();
            }
        }
        throw new IllegalArgumentException("No connector for split: " + split);
    }

    public String getId(ConnectorIndexHandle indexHandle)
    {
        for (Entry<String, ConnectorHandleResolver> entry : handleIdResolvers.entrySet()) {
            if (entry.getValue().canHandle(indexHandle)) {
                return entry.getKey();
            }
        }
        throw new IllegalArgumentException("No connector for index handle: " + indexHandle);
    }

    public Class<? extends ConnectorTableHandle> getTableHandleClass(String id)
    {
        ConnectorHandleResolver connectorHandleResolver = handleIdResolvers.get(id);
        checkArgument(connectorHandleResolver != null, "No handle resolver for %s", id);
        return connectorHandleResolver.getTableHandleClass();
    }

    public Class<? extends ConnectorColumnHandle> getColumnHandleClass(String id)
    {
        ConnectorHandleResolver connectorHandleResolver = handleIdResolvers.get(id);
        checkArgument(connectorHandleResolver != null, "No handle resolver for %s", id);
        return connectorHandleResolver.getColumnHandleClass();
    }

    public Class<? extends ConnectorSplit> getSplitClass(String id)
    {
        ConnectorHandleResolver connectorHandleResolver = handleIdResolvers.get(id);
        checkArgument(connectorHandleResolver != null, "No handle resolver for %s", id);
        return connectorHandleResolver.getSplitClass();
    }

    public Class<? extends ConnectorIndexHandle> getIndexHandleClass(String id)
    {
        ConnectorHandleResolver connectorHandleResolver = handleIdResolvers.get(id);
        checkArgument(connectorHandleResolver != null, "No handle resolver for %s", id);
        return connectorHandleResolver.getIndexHandleClass();
    }
}

@@ -3421,543 +3421,179 @@
     
-ConnectorHandleResolver connectorHandleResolver = handleIdResolvers.get(id);%0A        checkArgument(connectorHandleResolver != null, %22No handle resolver for %25s%22, id);%0A        return connectorHandleResolver.getTableHandleClass();%0A    %7D%0A%0A    public Class%3C? extends ConnectorColumnHandle%3E getColumnHandleClass(String id)%0A    %7B%0A        ConnectorHandleResolver connectorHandleResolver = handleIdResolvers.get(id);%0A        checkArgument(connectorHandleResolver != null, %22No handle resolver for %25s%22, id);%0A        return connectorHandleResolver
+return resolverFor(id).getTableHandleClass();%0A    %7D%0A%0A    public Class%3C? extends ConnectorColumnHandle%3E getColumnHandleClass(String id)%0A    %7B%0A        return resolverFor(id)
 .get
@@ -3706,116 +3706,91 @@
     
-ConnectorHandleResolver connectorHandleResolver = handleIdResolvers.get(id
+return resolverFor(id).getSplitClass(
 );%0A    
+%7D%0A%0A
     
-checkArgument(c
+public Class%3C? extends C
 onnector
 Hand
@@ -3789,113 +3789,103 @@
 ctor
+Index
 Handle
-Resolver != null, %22No handle resolver for %25s%22, id);%0A        return connectorHandleResolver.getSplit
+%3E getIndexHandleClass(String id)%0A    %7B%0A        return resolverFor(id).getIndexHandle
 Clas
@@ -3907,32 +3907,16 @@
  public 
-Class%3C? extends 
 Connecto
@@ -3920,40 +3920,34 @@
 ctor
-Index
 Handle
-%3E getIndexHandleClass
+Resolver resolverFor
 (Str
@@ -3992,32 +3992,17 @@
 esolver 
-connectorHandleR
+r
 esolver 
@@ -4052,32 +4052,17 @@
 rgument(
-connectorHandleR
+r
 esolver 
@@ -4123,53 +4123,16 @@
 urn 
-connectorHandleResolver.getIndexHandleClass()
+resolver
 ;%0A  

Refactor resolver lookup in HandleResolver