Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Stream.Builder have both add and accept methods?

Tags:

I was using a Stream.Builder and I stumbled upon the fact that this interface has both the methods accept(T t) and add(T t). The only difference is that the former returns void and the latter returns a Stream.Builder.

The documentation even mentions these methods to have the same default implementation:

The default implementation behaves as if:

accept(t) return this; 

Note that they forgot a semicolon, but that's another story.

My question is: why do they have two methods to add something to the stream builder? I think this clutters the API, and I thought they wanted to avoid that.

Is there any compelling reason to do so?

like image 836
MC Emperor Avatar asked Oct 06 '20 11:10

MC Emperor


People also ask

What is Stream in Stream builder?

A stream can have multiple listeners and all those listeners can get the pipeline. puting in all will get equal value. The way you put values on the stream is by using the Stream Controller. A stream builder is a widget that can convert user-defined objects into a stream.

How many stream builders does flutter have?

There are two types of streams in Flutter: single subscription streams and broadcast streams. Single subscription streams are the default. They work well when you're only using a particular stream on one screen.

What is stream builder in Java?

Stream.Builder build() in Java Java 8Object Oriented ProgrammingProgramming. The build() method of the Stream.Builder class builds the stream, transitioning this builder to the built state. The syntax is as follows − Stream<T> build() Following is an example to implement the build() method of the Stream.Builder class −


Video Answer


1 Answers

My guess:

Stream.Builder extends Consumer<T> so it must implement the accept(T) method.

But accept(T) returns void, so I think they added add(T) for convenience: methods in a builder pattern implementation often return this to be able to chain the building and finally call build().

like image 50
Conffusion Avatar answered Oct 18 '22 19:10

Conffusion