Question regarding interface java.util.Sized

Zhong Yu zhong.j.yu at gmail.com
Fri Oct 26 06:51:58 PDT 2012


On Fri, Oct 26, 2012 at 7:25 AM, Andrew Thompson <lordpixel at me.com> wrote:
> I have found myself wanting to write
>
> <C extends Iterable<?> & Sized> doSomething(S input)

Neppert argues that there should be a SizedIterable instead.

> Or variations on that theme in real code from time to time. I've always ended up using Collection but didn't like that. Of course it would be a lot more useful if it also worked with arrays…
>
>
>
>
>
>
> On Oct 26, 2012, at 7:54 AM, Gernot Neppert <mcnepp02 at googlemail.com> wrote:
>
>> Hello all,
>>
>> while browsing the latest lambda-related sourcecode, I came across the
>> new mixin-style interface "java.util.Sized".
>>
>> The JavaDoc says:
>> "Implementing this interface allows an object to indicate that its
>> elements may be efficiently counted."
>>
>> OK, so what do we do with this interface? In what contexts do we want
>> to test for "instanceof Sized" and then query its size()?
>> As far as I can see, "Sized" is implemented by:
>>
>> java.util.Collection
>> java.util.Map
>> java.util.streams.ops.Node
>>
>>
>> I think that interfaces should have both of the following properties:
>> 1. Provide uniform access to an aspect of a class.
>> 2. Provide a common handle to a polymorphic set of instances that can
>> reasonably put to use
>>
>> IMHO, "Sized" fails to have property 2.)
>>
>> It is hard to see what usecase would want to handle a polymporphic set
>> of Collections, Maps and Node, let alone handle them as "Sized"
>> objects, where the only operation you could invoke would be size().
>> This, java.util.Collection and java.util.Map gain nothing from
>> implementing "Sized"
>> If there are cases where it is useful to discriminate between sized an
>> non-sized flavours of a specific class or interface, it makes more
>> sense to provide an interface that extends that baseclass or
>> interface.
>>
>> e.g.
>> interface SizedStream extends Stream {
>>  public int size();
>> }
>>
>> Just my 2 cents...
>>
>


More information about the lambda-dev mailing list