com.enonic.xp.schema.xdata.XDatas Maven / Gradle / Ivy
package com.enonic.xp.schema.xdata;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.enonic.xp.annotation.PublicApi;
import com.enonic.xp.schema.BaseSchema;
import com.enonic.xp.support.AbstractImmutableEntityList;
@PublicApi
public final class XDatas
extends AbstractImmutableEntityList
{
private final ImmutableMap map;
private XDatas( final ImmutableList list )
{
super( list );
this.map = list.stream().collect( ImmutableMap.toImmutableMap( XData::getName, Function.identity() ) );
}
public XDatas add( final XData... xDatas )
{
return add( ImmutableList.copyOf( xDatas ) );
}
public XDatas add( final Iterable xDatas )
{
return add( ImmutableList.copyOf( xDatas ) );
}
private XDatas add( final ImmutableList xDatas )
{
final List tmp = new ArrayList<>();
tmp.addAll( this.list );
tmp.addAll( xDatas );
return new XDatas( ImmutableList.copyOf( tmp ) );
}
public Set getNames()
{
return this.list.stream().map( BaseSchema::getName ).collect( ImmutableSet.toImmutableSet() );
}
public XData getXData( final XDataName xDataName )
{
return map.get( xDataName );
}
public XDatas filter( final Predicate filter )
{
return from( this.map.values().stream().filter( filter ).iterator() );
}
public static XDatas empty()
{
return new XDatas( ImmutableList.of() );
}
public static XDatas from( final XData... xDatas )
{
return new XDatas( ImmutableList.copyOf( xDatas ) );
}
public static XDatas from( final Iterable extends XData> xDatas )
{
return new XDatas( ImmutableList.copyOf( xDatas ) );
}
public static XDatas from( final Iterator extends XData> xDatas )
{
return new XDatas( ImmutableList.copyOf( xDatas ) );
}
public static Builder create()
{
return new Builder();
}
public static class Builder
{
private final ImmutableList.Builder builder = ImmutableList.builder();
public Builder add( XData node )
{
builder.add( node );
return this;
}
public Builder addAll( XDatas nodes )
{
builder.addAll( nodes );
return this;
}
public Builder addAll( Collection nodes )
{
builder.addAll( nodes );
return this;
}
public XDatas build()
{
return new XDatas( builder.build() );
}
}
}