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

com.enonic.xp.page.PageTemplates Maven / Gradle / Ivy

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

import java.util.Collection;
import java.util.function.Function;
import java.util.function.Predicate;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import com.enonic.xp.annotation.PublicApi;
import com.enonic.xp.content.ContentName;
import com.enonic.xp.content.Contents;
import com.enonic.xp.support.AbstractImmutableEntityList;

@PublicApi
public final class PageTemplates
    extends AbstractImmutableEntityList
{
    private final ImmutableMap templatesByName;

    private PageTemplates( final ImmutableList list )
    {
        super( list );
        this.templatesByName = list.stream().collect( ImmutableMap.toImmutableMap( PageTemplate::getName, Function.identity() ) );
    }

    public PageTemplate getTemplate( final ContentName name )
    {
        return this.templatesByName.get( name );
    }

    public PageTemplate getTemplate( final PageTemplateKey key )
    {
        for ( PageTemplate pageTemplate : this.templatesByName.values() )
        {
            if ( pageTemplate.getKey().equals( key ) )
            {
                return pageTemplate;
            }
        }
        return null;
    }

    public PageTemplates filter( final Predicate predicate )
    {
        return PageTemplates.from( this.stream().filter( predicate ).toArray( PageTemplate[]::new ) );
    }

    public Contents toContents()
    {
        final Contents.Builder builder = Contents.create();
        for ( final PageTemplate pageTemplate : this )
        {
            builder.add( pageTemplate );
        }
        return builder.build();
    }

    public static PageTemplates empty()
    {
        return new PageTemplates( ImmutableList.of() );
    }

    public static PageTemplates from( final PageTemplate... templates )
    {
        return new PageTemplates( ImmutableList.copyOf( templates ) );
    }

    public static PageTemplates from( final Iterable templates )
    {
        return new PageTemplates( ImmutableList.copyOf( templates ) );
    }

    public static PageTemplates from( final Collection templates )
    {
        return new PageTemplates( ImmutableList.copyOf( templates ) );
    }

    public static Builder create()
    {
        return new Builder();
    }

    public static class Builder
    {
        private final ImmutableList.Builder list = new ImmutableList.Builder<>();

        public Builder add( PageTemplate template )
        {
            this.list.add( template );
            return this;
        }

        public Builder addAll( Iterable templates )
        {
            for ( final PageTemplate template : templates )
            {
                this.list.add( template );
            }
            return this;
        }

        public PageTemplates build()
        {
            return new PageTemplates( this.list.build() );
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy