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

org.eclipse.sisu.wire.ElementMerger Maven / Gradle / Ivy

There is a newer version: 3.0.0-alpha-3
Show newest version
/*******************************************************************************
 * Copyright (c) 2010-present Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.sisu.wire;

import java.util.HashSet;
import java.util.Set;

import org.eclipse.sisu.inject.Logs;

import com.google.inject.Binder;
import com.google.inject.Binding;
import com.google.inject.Key;
import com.google.inject.spi.DefaultElementVisitor;
import com.google.inject.spi.Element;
import com.google.inject.spi.ElementVisitor;

/**
 * {@link ElementVisitor} that verifies {@link Binding}s and merges any duplicates.
 */
final class ElementMerger
    extends DefaultElementVisitor
{
    // ----------------------------------------------------------------------
    // Implementation fields
    // ----------------------------------------------------------------------

    private final DependencyVerifier verifier = new DependencyVerifier();

    private final Set> localKeys = new HashSet>();

    private final Binder binder;

    // ----------------------------------------------------------------------
    // Constructors
    // ----------------------------------------------------------------------

    ElementMerger( final Binder binder )
    {
        this.binder = binder;
    }

    // ----------------------------------------------------------------------
    // Public methods
    // ----------------------------------------------------------------------

    @Override
    public  Void visit( final Binding binding )
    {
        final Key key = binding.getKey();
        if ( !localKeys.contains( key ) )
        {
            if ( Boolean.TRUE.equals( binding.acceptTargetVisitor( verifier ) ) )
            {
                localKeys.add( key );
                binding.applyTo( binder );
            }
            else
            {
                Logs.trace( "Discard binding: {}", binding, null );
            }
        }
        return null;
    }

    @Override
    public Void visitOther( final Element element )
    {
        element.applyTo( binder );
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy