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

com.enonic.xp.context.LocalScopeImpl Maven / Gradle / Ivy

There is a newer version: 7.14.4
Show newest version
package com.enonic.xp.context;

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

import com.google.common.collect.ImmutableMap;

import com.enonic.xp.session.Session;

final class LocalScopeImpl
    implements LocalScope
{
    private final Map attributes;

    private Session session;

    LocalScopeImpl()
    {
        this.attributes = new HashMap<>();
    }

    LocalScopeImpl( final HashMap attributes )
    {
        this.attributes = attributes;
    }

    @Override
    public Session getSession()
    {
        return this.session;
    }

    @Override
    public void setSession( final Session session )
    {
        this.session = session;
    }

    @Override
    @SuppressWarnings("unchecked")
    public  T getAttribute( final Class type )
    {
        return (T) getAttribute( type.getName() );
    }

    @Override
    public void setAttribute( final String key, final Object value )
    {
        this.attributes.put( key, value );
    }

    @Override
    public  void setAttribute( final T value )
    {
        setAttribute( value.getClass().getName(), value );
    }

    @Override
    public void removeAttribute( final String key )
    {
        this.attributes.remove( key );
    }

    @Override
    public  void removeAttribute( final Class type )
    {
        removeAttribute( type.getName() );
    }

    @Override
    public Object getAttribute( final String key )
    {
        final Object value = this.attributes.get( key );
        if ( value != null )
        {
            return value;
        }

        if ( this.session != null )
        {
            return this.session.getAttribute( key );
        }

        return null;
    }

    @Override
    public Map getAttributes()
    {
        return ImmutableMap.copyOf( this.attributes );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy