All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.facebook.presto.functionNamespace.execution.thrift.ContextualSimpleAddressSelectorBinder Maven / Gradle / Ivy

There is a newer version: 0.289
Show newest version
/*
 * 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.functionNamespace.execution.thrift;

import com.facebook.drift.client.address.AddressSelector;
import com.facebook.drift.client.address.SimpleAddressSelector;
import com.facebook.drift.client.address.SimpleAddressSelector.SimpleAddress;
import com.facebook.drift.client.address.SimpleAddressSelectorConfig;
import com.facebook.drift.client.guice.AbstractAnnotatedProvider;
import com.facebook.drift.client.guice.AddressSelectorBinder;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Binder;
import com.google.inject.Injector;

import java.lang.annotation.Annotation;
import java.util.Map;

import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static java.util.Objects.requireNonNull;

public class ContextualSimpleAddressSelectorBinder
        implements AddressSelectorBinder
{
    private final Map addresses;

    public static AddressSelectorBinder contextualSimpleAddressSelector(Map addresses)
    {
        return new ContextualSimpleAddressSelectorBinder(addresses);
    }

    private ContextualSimpleAddressSelectorBinder(Map addresses)
    {
        this.addresses = ImmutableMap.copyOf(requireNonNull(addresses, "addresses is null"));
    }

    @Override
    public void bind(Binder binder, Annotation annotation, String prefix)
    {
        binder.bind(AddressSelector.class)
                .annotatedWith(annotation)
                .toProvider(new SimpleAddressSelectorProvider(annotation, addresses));
    }

    private static class SimpleAddressSelectorProvider
            extends AbstractAnnotatedProvider>
    {
        private final Map addresses;
        protected SimpleAddressSelectorProvider(Annotation annotation, Map addresses)
        {
            super(annotation);
            this.addresses = addresses;
        }

        @Override
        protected AddressSelector get(Injector injector, Annotation annotation)
        {
            return new ContextualAddressSelector(addresses.entrySet().stream()
                    .collect(toImmutableMap(Map.Entry::getKey, entry -> new SimpleAddressSelector(entry.getValue()))));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy