enonic / xp

Enonic XP
https://enonic.com
GNU General Public License v3.0
200 stars 34 forks source link

add collecting methods to aggregating classes #10429

Open vbradnitski opened 6 months ago

vbradnitski commented 6 months ago

Every aggregating class (Nodes, Contents, etc. ) is quite often collected from streams in an inconvenient way:

Nodes.from( collection.stream()...collect( Collectors.toList() ) );

There is a way to improve them with an util method:

 public static Collector<Node, Builder, Nodes> collecting()
    {
        return Collector.of( Builder::new, Builder::add, ( left, right ) -> left.addAll( right.build() ), Builder::build );
    }

As a result:

 collection.stream()...collect( Nodes.collecting() );