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

com.enonic.xp.config.ConfigurationImpl Maven / Gradle / Ivy

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

import java.util.Map;
import java.util.Objects;

import com.google.common.collect.ImmutableMap;

import com.enonic.xp.convert.Converters;

final class ConfigurationImpl
    implements Configuration
{
    private final ImmutableMap map;

    private ConfigurationImpl( final ImmutableMap map )
    {
        this.map = map;
    }

    @Override
    public String get( final String key )
    {
        return this.map.get( key );
    }

    @Override
    public String getOrDefault( final String key, final String defValue )
    {
        final String value = get( key );
        return value != null ? value : defValue;
    }

    @Override
    public  T get( final String key, final Class type )
    {
        final String value = get( key );
        return value != null ? Converters.convertOrNull( value, type ) : null;
    }

    @Override
    public  T getOrDefault( final String key, final Class type, final T defValue )
    {
        final T value = get( key, type );
        return value != null ? value : defValue;
    }

    @Override
    public boolean exists( final String key )
    {
        return this.map.containsKey( key );
    }

    @Override
    public Configuration subConfig( final String prefix )
    {
        final ImmutableMap.Builder builder = ImmutableMap.builder();
        for ( final Map.Entry entry : this.map.entrySet() )
        {
            final String key = entry.getKey();
            if ( key.startsWith( prefix ) )
            {
                builder.put( key.substring( prefix.length() ), entry.getValue() );
            }
        }

        return new ConfigurationImpl( builder.build() );
    }

    @Override
    public Map asMap()
    {
        return this.map;
    }

    @Override
    public boolean equals( final Object o )
    {
        if ( this == o )
        {
            return true;
        }
        if ( !( o instanceof Configuration ) )
        {
            return false;
        }
        final Configuration that = (Configuration) o;
        return map.equals( that.asMap() );
    }

    @Override
    public int hashCode()
    {
        return Objects.hash( map );
    }

    public static ConfigurationImpl create( final Map map )
    {
        return new ConfigurationImpl( ImmutableMap.copyOf( map ) );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy