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

com.enonic.xp.attachment.Attachments Maven / Gradle / Ivy

The newest version!
package com.enonic.xp.attachment;

import java.util.Arrays;
import java.util.Collection;

import com.google.common.collect.ImmutableList;

import com.enonic.xp.annotation.PublicApi;
import com.enonic.xp.support.AbstractImmutableEntityList;

@PublicApi
public final class Attachments
    extends AbstractImmutableEntityList
{
    private static final Attachments EMPTY = new Attachments( ImmutableList.of() );

    private Attachments( final ImmutableList list )
    {
        super( list );
    }

    public Attachment byName( final String name )
    {
        return list.stream().filter( a -> a.getName().equals( name ) ).findAny().orElse( null );
    }

    public Attachment byLabel( final String label )
    {
        return list.stream().filter( a -> label.equals( a.getLabel() ) ).findAny().orElse( null );
    }

    public boolean hasByName( final String name )
    {
        return byName( name ) != null;
    }

    public boolean hasByLabel( final String label )
    {
        return byLabel( label ) != null;
    }

    public Attachments add( final Attachment... attachments )
    {
        return add( Arrays.asList( attachments ) );
    }

    public Attachments add( final Iterable attachments )
    {
        final ImmutableList.Builder listBuilder = ImmutableList.builder();
        listBuilder.addAll( this.list );
        listBuilder.addAll( attachments );
        return fromInternal( listBuilder.build() );
    }

    public static Attachments empty()
    {
        return EMPTY;
    }

    public static Attachments from( final Attachment... contents )
    {
        return fromInternal( ImmutableList.copyOf( contents ) );
    }

    public static Attachments from( final Iterable contents )
    {
        return fromInternal( ImmutableList.copyOf( contents ) );
    }

    public static Attachments from( final Collection contents )
    {
        return fromInternal( ImmutableList.copyOf( contents ) );
    }

    private static Attachments fromInternal( final ImmutableList list )
    {
        if ( list.isEmpty() )
        {
            return EMPTY;
        }
        else
        {
            return new Attachments( list );
        }
    }

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

    public static class Builder
    {
        private final ImmutableList.Builder builder = ImmutableList.builder();

        public Builder add( Attachment attachment )
        {
            builder.add( attachment );
            return this;
        }

        @Deprecated
        public Builder addAll( Attachments attachments )
        {
            builder.addAll( attachments );
            return this;
        }

        public Attachments build()
        {
            return fromInternal( builder.build() );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy