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

org.commonjava.emb.plexus.InstanceRegistry Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010 Red Hat, Inc.
 * 
 * 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 org.commonjava.emb.plexus;

import java.util.HashMap;
import java.util.Map;

public class InstanceRegistry
{

    private final Map, Object> instances = new HashMap, Object>();

    public InstanceRegistry()
    {
    }

    public InstanceRegistry( final InstanceRegistry... delegates )
    {
        if ( delegates != null && delegates.length > 0 )
        {
            for ( final InstanceRegistry delegate : delegates )
            {
                overrideMerge( delegate );
            }
        }
    }

    public boolean has( final ComponentKey key )
    {
        if ( key == null )
        {
            return false;
        }

        return instances.containsKey( key );
    }

    public  boolean has( final Class role, final String hint )
    {
        return has( new ComponentKey( role, hint ) );
    }

    public  T get( final ComponentKey key )
    {
        return key.castValue( instances.get( key ) );
    }

    public  T get( final Class role, final String hint )
    {
        return get( new ComponentKey( role, hint ) );
    }

    public  InstanceRegistry add( final ComponentKey key, final T instance )
    {
        if ( instance != null )
        {
            instances.put( key, instance );
        }
        return this;
    }

    public  InstanceRegistry add( final Class role, final String hint, final T instance )
    {
        if ( role == null )
        {
            throw new NullPointerException( "Role class is null." );
        }

        if ( instance == null )
        {
            throw new NullPointerException( "Instance is null." );
        }

        if ( !role.isAssignableFrom( instance.getClass() ) )
        {
            throw new IllegalArgumentException( "Instance class: " + instance.getClass().getName()
                            + " is not assignable to role: " + role.getClass() );
        }

        return add( new ComponentKey( role, hint ), instance );
    }

    public  InstanceRegistry add( final Class role, final T instance )
    {
        if ( role == null )
        {
            throw new NullPointerException( "Role class is null." );
        }

        if ( instance == null )
        {
            throw new NullPointerException( "Instance is null." );
        }

        if ( !role.isAssignableFrom( instance.getClass() ) )
        {
            throw new IllegalArgumentException( "Instance class: " + instance.getClass().getName()
                            + " is not assignable to role: " + role.getClass() );
        }

        return add( new ComponentKey( role ), instance );
    }

    @SuppressWarnings( { "unchecked", "rawtypes" } )
    public  InstanceRegistry setVirtualInstance( final ComponentKey key, final T instance )
    {
        final Object virt = instances.get( key );
        if ( virt != null && ( virt instanceof VirtualInstance ) )
        {
            final VirtualInstance vi = (VirtualInstance) virt;
            if ( vi.getVirtualClass().equals( key.getRoleClass() ) && vi.getRawInstance() == null )
            {
                vi.setInstance( instance );
            }
        }

        return this;
    }

    public  InstanceRegistry setVirtualInstance( final Class role, final String hint,
                                                                 final T instance )
    {
        return setVirtualInstance( new ComponentKey( role, hint ), instance );
    }

    public  InstanceRegistry setVirtualInstance( final Class role, final T instance )
    {
        return setVirtualInstance( new ComponentKey( role ), instance );
    }

    public  InstanceRegistry addVirtual( final ComponentKey key, final VirtualInstance virt )
    {
        instances.put( key, virt );
        return this;
    }

    public  InstanceRegistry addVirtual( final String hint, final VirtualInstance instance )
    {
        if ( instance == null )
        {
            throw new NullPointerException( "Instance is null." );
        }

        return addVirtual( new ComponentKey( instance.getVirtualClass(), hint ), instance );
    }

    public  InstanceRegistry addVirtual( final VirtualInstance instance )
    {
        if ( instance == null )
        {
            throw new NullPointerException( "Instance is null." );
        }

        return addVirtual( new ComponentKey( instance.getVirtualClass() ), instance );
    }

    public InstanceRegistry overrideMerge( final InstanceRegistry instanceRegistry )
    {
        if ( !instanceRegistry.instances.isEmpty() )
        {
            instances.putAll( instanceRegistry.instances );
        }

        return this;
    }

    public Map, Object> getInstances()
    {
        return instances;
    }

    @SuppressWarnings( "unchecked" )
    public  InstanceRegistry add( final T instance )
    {
        if ( instance == null )
        {
            throw new NullPointerException( "Instance is null." );
        }

        return add( new ComponentKey( (Class) instance.getClass() ), instance );
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy