Reputation: 23027
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?
Upvotes: 26
Views: 2358
Reputation: 4485
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()
.
Upvotes: 36