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

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

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

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

public class NodeBranchEntries
    implements Iterable
{
    private static final NodeBranchEntries EMPTY = new NodeBranchEntries( Collections.emptySet() );

    private final Map branchNodeVersionMap;

    private NodeBranchEntries( final Builder builder )
    {
        this.branchNodeVersionMap = Collections.unmodifiableMap( builder.map );
    }

    private NodeBranchEntries( final Collection entries )
    {
        Map builder = new LinkedHashMap<>();
        for ( NodeBranchEntry entry : entries )
        {
            builder.put( entry.getNodeId(), entry );
        }
        this.branchNodeVersionMap = Collections.unmodifiableMap( builder );
    }

    public static NodeBranchEntries from( final Collection nodeBranchEntries )
    {
        return new NodeBranchEntries( nodeBranchEntries );
    }

    public static NodeBranchEntries empty()
    {
        return EMPTY;
    }

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

    public int getSize()
    {
        return this.branchNodeVersionMap.size();
    }

    public boolean isNotEmpty()
    {
        return !this.branchNodeVersionMap.isEmpty();
    }

    public Collection getSet()
    {
        return this.branchNodeVersionMap.values();
    }

    public Stream stream()
    {
        return this.branchNodeVersionMap.values().stream();
    }

    @Override
    public Iterator iterator()
    {
        return this.branchNodeVersionMap.values().iterator();
    }

    public Set getKeys()
    {
        return branchNodeVersionMap.keySet();
    }

    public NodeBranchEntry get( final NodeId nodeId )
    {
        return branchNodeVersionMap.get( nodeId );
    }

    public static class Builder
    {
        private final Map map = new LinkedHashMap<>();

        public Builder add( final NodeBranchEntry nodeBranchEntry )
        {
            this.map.put( nodeBranchEntry.getNodeId(), nodeBranchEntry );
            return this;
        }

        public Builder addAll( final NodeBranchEntries nodeBranchEntries )
        {
            this.map.putAll( nodeBranchEntries.branchNodeVersionMap );
            return this;
        }

        public NodeBranchEntries build()
        {
            return this.map.isEmpty() ? EMPTY : new NodeBranchEntries( this );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy