SharedCollection.st
author Claus Gittinger <cg@exept.de>
Mon, 08 May 2006 15:21:45 +0200
changeset 1636 a3e5703901c8
parent 1635 a3aaefef0a2c
child 1637 72a61c17925c
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

"{ Package: 'stx:libbasic2' }"

Collection subclass:#SharedCollection
	instanceVariableNames:'accessLock realCollection'
	classVariableNames:''
	poolDictionaries:''
	category:'Collections-Unordered'
!

!SharedCollection class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 2006 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

documentation
"
    Instances of this class provide synchronized access (of multiple processes) 
    to a collection.

    Notice: the message-forwarding is done by catching sbclassResponsibility and
    doesNotUnderstand errors.
    For performance, and for more complex operation-atomicy, more messages might need
    an explicit handling. See the implementation of #at: / #at:put: and #size for examples.
"
!

examples
"
                                        [exBegin]
        |c|

        c := SharedCollection for:(OrderedCollection new).
        c add:1.
        c add:2.
        c add:3.
        c addAll:#(4 5 6).
        c removeFirst.
        c removeLast.
        c inspect.
                                        [exEnd]

                                        [exBegin]
        |c|

        c := SharedCollection for:(Array new:10).
        c at:1 put:5.
        c replaceFrom:2 to:5 with:#(20 30 40 50).
        c inspect.
                                        [exEnd]
"
! !

!SharedCollection class methodsFor:'instance creation'!

for:aCollection
    ^ self new initializeFor:aCollection
! !

!SharedCollection methodsFor:'converting'!

asSharedCollection
    ^ self.
! !

!SharedCollection methodsFor:'initialization'!

initializeFor:aCollection
    accessLock := RecursionLock new.
    realCollection := aCollection.
! !

!SharedCollection methodsFor:'message forwarding'!

at:index
    |rslt|

    accessLock critical:[
        rslt := realCollection at:index
    ].
    ^ rslt
!

at:index put:value
    |rslt|

    accessLock critical:[
        rslt := realCollection at:index put:value
    ].
    ^ rslt
!

doesNotUnderstand:aMessage
    "catches everything not understood by the collection protocol"

    |msg rslt|

    msg := thisContext sender message.
    accessLock critical:[
        rslt := aMessage sendTo:realCollection
    ].
    ^ rslt
!

size
    |rslt|

    accessLock critical:[
        rslt := realCollection size
    ].
    ^ rslt
!

subclassResponsibility
    "catches every required message of the collection protocol"

    |msg rslt|

    msg := thisContext sender message.
    accessLock critical:[
        rslt := msg sendTo:realCollection
    ].
    ^ rslt
! !

!SharedCollection class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/SharedCollection.st,v 1.4 2006-05-08 13:21:45 cg Exp $'
! !