SharedCollection.st
author Claus Gittinger <cg@exept.de>
Mon, 11 Feb 2008 15:33:18 +0100
changeset 1929 5271aca614c2
parent 1794 603885d999ca
child 2515 104080163067
permissions -rw-r--r--
+add:

"
 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'!

add:anElement
    |rslt|

    accessLock critical:[
        rslt := realCollection add:anElement
    ].
    ^ rslt
!

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

    "Modified: / 07-12-2006 / 17:38:30 / cg"
!

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 methodsFor:'special'!

species
    "returns non shared collections"

    ^ realCollection species
! !

!SharedCollection class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/SharedCollection.st,v 1.7 2008-02-11 14:33:18 cg Exp $'
! !