You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Torsten Curdt <tc...@vafer.org> on 2004/03/31 16:07:17 UTC

[collections] UnmodifiableBoundedCollection

Quoting from the javadocs

> UnmodifiableBoundedCollection decorates another BoundedCollection  to
> ensure it can't be altered.
> 
> If a BoundedCollection is first wrapped in some other collection 
> decorator, such as synchronized or predicated, the BoundedCollection 
> nature is lost. The factory on this class will attempt to retrieve the 
> bounded nature by examining the package scope variables. 

I wondered why this should be the case so I gave it a try:

         Map sm = MapUtils.synchronizedMap(new HashMap());
         Map m = MapUtils.fixedSizeMap(sm);
         m.put("a","value");

         Map m = MapUtils.fixedSizeMap(new HashMap());
         Map sm = MapUtils.synchronizedMap(m);
         sm.put("a","value");

Both examples throw an exception as exspected.
Can someone explain why the UnmodifiableBoundedCollection
is needed?

cheers
--
Torsten



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [collections] UnmodifiableBoundedCollection

Posted by Torsten Curdt <tc...@vafer.org>.
>>Well, I understand but I used the FixedSizeMap because it also
>>implements BoundedMap. So I assumed that a FixedSizeMap means
>>the boundary is always reached. Did I get this wrong?
> 
> 
> Ah, I'd forgotten about BoundedMap ;-) FixedSizeMap does implement
> BoundedMap, and as the size is fixed, the map is always at the bound.

Puh! ...glad I got it right :)

>>But why is this needed? Both decorators are still being used.
>>...although the object itself does not implement
>>the BoundedMap/Collection interface anymore. So the
>>UnmodifiableBoundedCollection is only to bring the
>>interface back?
>>
>>Is that what you mean?
> 
> 
> UnmodifiableBoundedCollection is used when you need to access the
> BoundedCollection interface, but you only have a Collection. Casting is no
> good as the original bounded collection may have been decorated.
> UnmodifiableBoundedCollection wraps the collection you do have with a thin
> wrapper that tunnels through to the original BoundedCollection.
> 
> And there is no equivalent for BoundedMap at present.

Alright, that clears it up :)

Thanks, Stephen

cheers
--
Torsten



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [collections] UnmodifiableBoundedCollection

Posted by Stephen Colebourne <sc...@btopenworld.com>.
From: "Torsten Curdt" <tc...@vafer.org>
> Stephen Colebourne wrote:
> > This misunderstands the BoundedCollection interface, which is an
interface
> > providing extra information about collections with a maximum limited
size..
>
> Well, I understand but I used the FixedSizeMap because it also
> implements BoundedMap. So I assumed that a FixedSizeMap means
> the boundary is always reached. Did I get this wrong?

Ah, I'd forgotten about BoundedMap ;-) FixedSizeMap does implement
BoundedMap, and as the size is fixed, the map is always at the bound.

> But why is this needed? Both decorators are still being used.
> ...although the object itself does not implement
> the BoundedMap/Collection interface anymore. So the
> UnmodifiableBoundedCollection is only to bring the
> interface back?
>
> Is that what you mean?

UnmodifiableBoundedCollection is used when you need to access the
BoundedCollection interface, but you only have a Collection. Casting is no
good as the original bounded collection may have been decorated.
UnmodifiableBoundedCollection wraps the collection you do have with a thin
wrapper that tunnels through to the original BoundedCollection.

And there is no equivalent for BoundedMap at present.

Stephen


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [collections] UnmodifiableBoundedCollection

Posted by Torsten Curdt <tc...@vafer.org>.
Stephen Colebourne wrote:
> This misunderstands the BoundedCollection interface, which is an interface
> providing extra information about collections with a maximum limited size..

Well, I understand but I used the FixedSizeMap because it also
implements BoundedMap. So I assumed that a FixedSizeMap means
the boundary is always reached. Did I get this wrong?

> // create a buffer that implements the BoundedCollection interface
> BoundedCollection buffer = new BoundedBuffer(16);

// create a BoundedMap
Map m = MapUtils.fixedSizeMap(new HashMap());

> // synchronize it, but this loses the BoundedCollection interface
> Collection sync = SynchronizedCollection.decorate(buffer);

// synchronize it
Map sm = MapUtils.synchronizedMap(m);

> // get back to the bounded collection interface
> BoundedCollection coll = UnmodifiableBoundedCollection.decorateUsing(sync);

But why is this needed? Both decorators are still being used.
...although the object itself does not implement
the BoundedMap/Collection interface anymore. So the
UnmodifiableBoundedCollection is only to bring the
interface back?

Is that what you mean?

cheers
--
Torsten



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Re: [collections] UnmodifiableBoundedCollection

Posted by Stephen Colebourne <sc...@btopenworld.com>.
This misunderstands the BoundedCollection interface, which is an interface
providing extra information about collections with a maximum limited size..

// create a buffer that implements the BoundedCollection interface
BoundedCollection buffer = new BoundedBuffer(16);

// synchronize it, but this loses the BoundedCollection interface
Collection sync = SynchronizedCollection.decorate(buffer);

// get back to the bounded collection interface
BoundedCollection coll = UnmodifiableBoundedCollection.decorateUsing(sync);

Stephen

----- Original Message -----
From: "Torsten Curdt" <tc...@vafer.org>
> Quoting from the javadocs
>
> > UnmodifiableBoundedCollection decorates another BoundedCollection  to
> > ensure it can't be altered.
> >
> > If a BoundedCollection is first wrapped in some other collection
> > decorator, such as synchronized or predicated, the BoundedCollection
> > nature is lost. The factory on this class will attempt to retrieve the
> > bounded nature by examining the package scope variables.
>
> I wondered why this should be the case so I gave it a try:
>
>          Map sm = MapUtils.synchronizedMap(new HashMap());
>          Map m = MapUtils.fixedSizeMap(sm);
>          m.put("a","value");
>
>          Map m = MapUtils.fixedSizeMap(new HashMap());
>          Map sm = MapUtils.synchronizedMap(m);
>          sm.put("a","value");
>
> Both examples throw an exception as exspected.
> Can someone explain why the UnmodifiableBoundedCollection
> is needed?
>
> cheers
> --
> Torsten
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org