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

com.enonic.xp.aggregation.Aggregations Maven / Gradle / Ivy

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

import java.util.LinkedHashSet;
import java.util.Set;

import com.google.common.collect.ImmutableSet;

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

@PublicApi
public class Aggregations
    extends AbstractImmutableEntitySet
{
    private static final Aggregations EMPTY = new Aggregations( ImmutableSet.of() );

    private Aggregations( final ImmutableSet set )
    {
        super( set );
    }

    public static Aggregations empty()
    {
        return EMPTY;
    }

    @Deprecated
    public static Aggregations from( final ImmutableSet aggregations )
    {
        return fromInternal( aggregations );
    }

    public static Aggregations from( final Iterable aggregations )
    {
        return fromInternal( ImmutableSet.copyOf( aggregations ) );
    }

    public static Aggregations from( final Aggregation... aggregations )
    {
        return fromInternal( ImmutableSet.copyOf( aggregations ) );
    }

    public Aggregation get( final String name )
    {
        return this.stream().filter( ( agg ) -> name.equals( agg.getName() ) ).findFirst().orElse( null );
    }

    private static Aggregations fromInternal( final ImmutableSet set )
    {
        return set.isEmpty() ? EMPTY : new Aggregations( set );
    }

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

    public static class Builder
    {
        private final Set aggregations = new LinkedHashSet<>();

        public Builder add( final Aggregation aggregation )
        {
            this.aggregations.add( aggregation );
            return this;
        }

        public Aggregations build()
        {
            return Aggregations.fromInternal( ImmutableSet.copyOf( aggregations ) );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy