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

com.enonic.xp.node.NodeIds Maven / Gradle / Ivy

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

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableSet;

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

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

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

    public static NodeIds empty()
    {
        return EMPTY;
    }

    public static NodeIds from( final NodeId... ids )
    {
        return fromInternal( ImmutableSet.copyOf( ids ) );
    }

    public static NodeIds from( final String... ids )
    {
        return from( Arrays.asList( ids ) );
    }

    public static NodeIds from( final Collection ids )
    {
        return fromInternal( ids.stream().map( NodeId::from ).collect( ImmutableSet.toImmutableSet() ) );
    }

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

    public static NodeIds from( final Iterable ids )
    {
        if ( ids instanceof NodeIds )
        {
            return (NodeIds) ids;
        }
        return fromInternal( ImmutableSet.copyOf( ids ) );
    }

    private static NodeIds fromInternal( final ImmutableSet set )
    {
        if ( set.isEmpty() )
        {
            return EMPTY;
        }
        else
        {
            return new NodeIds( set );
        }
    }

    @Deprecated
    public List getAsStrings()
    {
        return this.set.stream().map( NodeId::toString ).collect( Collectors.toList() );
    }

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

        public Builder add( final NodeId nodeId )
        {
            this.nodeIds.add( nodeId );
            return this;
        }

        public Builder addAll( final NodeIds nodeIds )
        {
            this.nodeIds.addAll( nodeIds.getSet() );
            return this;
        }

        public NodeIds build()
        {
            return fromInternal( nodeIds.build() );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy