UUID.st
author Claus Gittinger <cg@exept.de>
Sat, 02 May 2020 21:40:13 +0200
changeset 5476 7355a4b11cb6
parent 5218 87cac42cf4f8
permissions -rw-r--r--
#FEATURE by cg class: Socket class added: #newTCPclientToHost:port:domain:domainOrder:withTimeout: changed: #newTCPclientToHost:port:domain:withTimeout:
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 COPYRIGHT (c) 2002 by eXept Software AG
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
              All Rights Reserved
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 This software is furnished under a license and may be used
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 hereby transferred.
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
"
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
"{ Package: 'stx:libbasic2' }"
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
3542
b00b42c1378d class: UUID
Stefan Vogel <sv@exept.de>
parents: 3122
diff changeset
    14
"{ NameSpace: Smalltalk }"
b00b42c1378d class: UUID
Stefan Vogel <sv@exept.de>
parents: 3122
diff changeset
    15
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
ByteArray variableByteSubclass:#UUID
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	instanceVariableNames:''
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
    18
	classVariableNames:'CachedMACAddress Lock SequenceNumber LastTime Increment
3807
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
    19
		NameSpaceToUuidBytes CachedUuidsBySymbol'
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
	poolDictionaries:''
1304
1c58aa764724 category
Claus Gittinger <cg@exept.de>
parents: 1287
diff changeset
    21
	category:'Net-Communication-Support'
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
!
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
!UUID class methodsFor:'documentation'!
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
copyright
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
"
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
 COPYRIGHT (c) 2002 by eXept Software AG
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
              All Rights Reserved
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
 This software is furnished under a license and may be used
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
 only in accordance with the terms of that license and with the
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
 inclusion of the above copyright notice.   This software may not
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
 be provided or otherwise made available to, or used by, any
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
 other person.  No title to or ownership of the software is
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
 hereby transferred.
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
"
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    38
!
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    39
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    40
documentation
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    41
"
1544
6c339f276b91 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1543
diff changeset
    42
    128-bit Universal Unique Ids (UUIDs) as defined by OpenGroup/DCE
6c339f276b91 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1543
diff changeset
    43
        http://www.opengroup.org/onlinepubs/9629399/apdxa.htm.
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    44
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
    45
    See also RFC4122.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
    46
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    47
    A UUID is unique in time and space (at least until about Year 3400).
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    48
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
    49
    Several fields if the UUID are interpreted as integers, so host/network byte
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
    50
    order is relevant. UUIDs are stored in a ByteArray in network byte order (MSB-first),
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
    51
    so they may be exported/imported between different machines.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
    52
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
    53
    You can import UUIDs in host byte order using #fromNativeBytes:
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
    54
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    55
    [author:]
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    56
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    57
    [instance variables:]
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    58
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    59
    [class variables:]
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    60
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    61
    [see also:]
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    62
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    63
"
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    64
! !
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    65
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    66
!UUID class methodsFor:'initialization'!
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    67
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    68
initialize
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    69
    "I want to get informed about image restarts"
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    70
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    71
    Lock isNil ifTrue:[
4505
c1ade3c06219 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4496
diff changeset
    72
        Lock := RecursionLock name:#UUID.
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    73
        LastTime := 0.
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    74
        Increment := 0.
3807
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
    75
        CachedUuidsBySymbol := IdentityDictionary new.
3549
ae696bf10bf5 class: UUID
Claus Gittinger <cg@exept.de>
parents: 3542
diff changeset
    76
    ]
ae696bf10bf5 class: UUID
Claus Gittinger <cg@exept.de>
parents: 3542
diff changeset
    77
!
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
    78
3549
ae696bf10bf5 class: UUID
Claus Gittinger <cg@exept.de>
parents: 3542
diff changeset
    79
nameSpaceToUuidBytes
ae696bf10bf5 class: UUID
Claus Gittinger <cg@exept.de>
parents: 3542
diff changeset
    80
    NameSpaceToUuidBytes isNil ifTrue:[
ae696bf10bf5 class: UUID
Claus Gittinger <cg@exept.de>
parents: 3542
diff changeset
    81
        NameSpaceToUuidBytes := Dictionary 
ae696bf10bf5 class: UUID
Claus Gittinger <cg@exept.de>
parents: 3542
diff changeset
    82
            withKeysAndValues:#(
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
    83
                DNS  #[16r6B 16rA7 16rB8 16r10 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8]            
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
    84
                URL  #[16r6B 16rA7 16rB8 16r11 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8]
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
    85
                "ASN.1 OID in DER or as Text"
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
    86
                OID  #[16r6B 16rA7 16rB8 16r12 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8]
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
    87
                "X.500 DN as DER or as Text"
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
    88
                X500 #[16r6B 16rA7 16rB8 16r14 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8]            
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
    89
            ).
3549
ae696bf10bf5 class: UUID
Claus Gittinger <cg@exept.de>
parents: 3542
diff changeset
    90
    ].
ae696bf10bf5 class: UUID
Claus Gittinger <cg@exept.de>
parents: 3542
diff changeset
    91
    ^ NameSpaceToUuidBytes
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    92
!
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    93
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    94
update:something with:aParameter from:changedObject
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    95
    "flush cached MAC address (may have been restarted on another host)"
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    96
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    97
    (something == #restarted) ifTrue:[
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    98
        CachedMACAddress := nil.
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
    99
        SequenceNumber := nil.
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   100
    ]
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
! !
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
!UUID class methodsFor:'instance creation'!
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
1923
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   105
basicNew
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   106
    ^ super basicNew:16
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   107
!
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   108
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   109
basicNew:size
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   110
    "allow creating with exact size. We need this for XMLStandardDecoder"
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   111
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   112
    size ~~ 16 ifTrue:[
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   113
        ^ self shouldNotImplement.
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   114
    ].
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   115
    ^ super basicNew:size.
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   116
!
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   117
2017
d08521448dc0 decodeFromLiterlArray
fm
parents: 1923
diff changeset
   118
decodeFromLiteralArray:anArray
2018
316b0a396e86 decodeFromLiterlArray
fm
parents: 2017
diff changeset
   119
    anArray size == 2 ifTrue:[
316b0a396e86 decodeFromLiterlArray
fm
parents: 2017
diff changeset
   120
        "/ UUID 'uuid-string'
316b0a396e86 decodeFromLiterlArray
fm
parents: 2017
diff changeset
   121
        ^ self readFrom:(anArray at:2).
316b0a396e86 decodeFromLiterlArray
fm
parents: 2017
diff changeset
   122
    ].
316b0a396e86 decodeFromLiterlArray
fm
parents: 2017
diff changeset
   123
    "/ UUID bytes...
316b0a396e86 decodeFromLiterlArray
fm
parents: 2017
diff changeset
   124
    ^ super decodeFromLiteralArray:anArray "/ self readFrom:(anArray at:2)
2017
d08521448dc0 decodeFromLiterlArray
fm
parents: 1923
diff changeset
   125
d08521448dc0 decodeFromLiterlArray
fm
parents: 1923
diff changeset
   126
    "
2018
316b0a396e86 decodeFromLiterlArray
fm
parents: 2017
diff changeset
   127
     (UUID decodeFromLiteralArray:#(UUID '5b023ce0-41f1-11dd-b99f-001558137da0'))
316b0a396e86 decodeFromLiterlArray
fm
parents: 2017
diff changeset
   128
       literalArrayEncoding   
2017
d08521448dc0 decodeFromLiterlArray
fm
parents: 1923
diff changeset
   129
    "
d08521448dc0 decodeFromLiterlArray
fm
parents: 1923
diff changeset
   130
!
d08521448dc0 decodeFromLiterlArray
fm
parents: 1923
diff changeset
   131
1382
626693b1fed7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1320
diff changeset
   132
fromBytes:aByteArray
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   133
    "set uuid from aByteArray.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   134
     aByteArray must be 16 bytes in network byte order (MSB-first)"
1382
626693b1fed7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1320
diff changeset
   135
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   136
    |uuid|
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
1923
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   138
    uuid := super basicNew:16.
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
    uuid replaceFrom:1 to:16 with:aByteArray.
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   140
    ^ uuid.
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   142
    "    
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   143
     UUID fromBytes:#[16r01 16r02 16r03 16r04
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   144
                      16r05 16r06 16r07 16r08
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   145
                      16r09 16r10 16r11 16r12
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   146
                      16r13 16r14 16r15 16r16]. 
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   147
    "
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   148
!
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
2950
bf0c61112aa1 class: UUID
Claus Gittinger <cg@exept.de>
parents: 2756
diff changeset
   150
fromBytes:aByteArray MSB:msb
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   151
    "Set the UUID from aByteArray. UUIDS are stored internally as MSB-first. 
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   152
     So, alway set
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   153
        msb = true      if reading from network or persistent storage"
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   155
    |uuid|
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   156
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   157
    uuid := self fromBytes:aByteArray.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   158
    msb ifFalse:[
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   159
        self adjustByteOrder:uuid.
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
    ].
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
    ^ uuid
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
    "
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
     UUID fromBytes:#[16r01 16r02 16r03 16r04
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
                      16r05 16r06 16r07 16r08
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
                      16r09 16r10 16r11 16r12
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
                      16r13 16r14 16r15 16r16] msb:false. 
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
    "
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
    "
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
     UUID fromBytes:#[16r01 16r02 16r03 16r04
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
                      16r05 16r06 16r07 16r08
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
                      16r09 16r10 16r11 16r12
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
                      16r13 16r14 16r15 16r16] msb:true.    
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
    "
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
!
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   178
fromNativeBytes:aByteArray
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   179
    "convert bytes to uuid.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   180
     aByteArray represents a UUID in host byte order 
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   181
     - e.g. an UUID fetched from the Windows OS"
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   182
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   183
    |uuid|
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   184
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   185
    uuid := self fromBytes:aByteArray.
3542
b00b42c1378d class: UUID
Stefan Vogel <sv@exept.de>
parents: 3122
diff changeset
   186
    IsBigEndian ifFalse:[
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   187
        self adjustByteOrder:uuid.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   188
    ].
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   189
    ^ uuid.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   190
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   191
    "    
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   192
     UUID fromNativeBytes:#[16r01 16r02 16r03 16r04
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   193
                              16r05 16r06 16r07 16r08
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   194
                              16r09 16r10 16r11 16r12
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   195
                              16r13 16r14 16r15 16r16]. 
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   196
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   197
    "
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   198
!
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   199
3807
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   200
fromString:aStringOrSymbol
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   201
    "convert aStringOrSymbol to a UUID. 
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   202
     If aStringOrSymbol is a symbol, cache this UUID for faster conversion.
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   203
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   204
     The real conversion is done in #readFrom:onError."
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   205
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   206
    |isSymbol uuid|
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   207
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   208
    isSymbol := aStringOrSymbol isSymbol.
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   209
    isSymbol ifTrue:[
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   210
        uuid := CachedUuidsBySymbol at:aStringOrSymbol ifAbsent:[].
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   211
        uuid notNil ifTrue:[^ uuid].
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   212
    ].
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   213
    uuid := self readFrom:aStringOrSymbol readStream.
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   214
    isSymbol ifTrue:[
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   215
        CachedUuidsBySymbol at:aStringOrSymbol put:uuid.
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   216
    ].
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   217
    ^ uuid.
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   218
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   219
    "
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   220
        UUID fromString:'be1ef7a0-ae1d-11e0-9cb5-02ff7d4f61ea'
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   221
        UUID fromString:#'be1ef7a0-ae1d-11e0-9cb5-02ff7d4f61ea'
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   222
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   223
        Time millisecondsToRun:[
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   224
            100000 timesRepeat:[
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   225
                UUID fromString:'be1ef7a0-ae1d-11e0-9cb5-02ff7d4f61ea'.
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   226
            ]
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   227
        ]
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   228
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   229
        Time millisecondsToRun:[
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   230
            100000 timesRepeat:[
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   231
                UUID fromString:#'be1ef7a0-ae1d-11e0-9cb5-02ff7d4f61ea'.
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   232
            ]
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   233
        ]
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   234
    "
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   235
!
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   236
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   237
genRandomUUID
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   238
    "generate a new random UUID"
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   239
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   240
    ^ (super basicNew:16) setRandomUUIDFromBytes:(RandomGenerator new nextBytes:16).
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   241
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   242
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   243
    "
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   244
      self genRandomUUID
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   245
    "
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   246
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   247
    "
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   248
      |uuids sample|
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   249
      sample := 100000.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   250
      uuids := Set new:sample.  
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   251
      sample timesRepeat:[
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   252
          uuids add:UUID genRandomUUID.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   253
      ].
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   254
      self assert:(uuids size = sample).  
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   255
      uuids
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   256
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   257
      1 to: 100 do:[ : el |
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   258
          Transcript show:el.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   259
          Transcript show:': '.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   260
          Transcript showCR:(UUID genRandomUUID).
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   261
      ].
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   262
    "
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   263
!
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   264
2431
ba791db4a743 added: #genTimestampUUID
Stefan Vogel <sv@exept.de>
parents: 2428
diff changeset
   265
genTimestampUUID
ba791db4a743 added: #genTimestampUUID
Stefan Vogel <sv@exept.de>
parents: 2428
diff changeset
   266
    "generate a new timestamp UUID"
ba791db4a743 added: #genTimestampUUID
Stefan Vogel <sv@exept.de>
parents: 2428
diff changeset
   267
ba791db4a743 added: #genTimestampUUID
Stefan Vogel <sv@exept.de>
parents: 2428
diff changeset
   268
    ^ (super basicNew:16) genTimestampUUID
ba791db4a743 added: #genTimestampUUID
Stefan Vogel <sv@exept.de>
parents: 2428
diff changeset
   269
ba791db4a743 added: #genTimestampUUID
Stefan Vogel <sv@exept.de>
parents: 2428
diff changeset
   270
    "
ba791db4a743 added: #genTimestampUUID
Stefan Vogel <sv@exept.de>
parents: 2428
diff changeset
   271
      self genTimestampUUID
ba791db4a743 added: #genTimestampUUID
Stefan Vogel <sv@exept.de>
parents: 2428
diff changeset
   272
    "
ba791db4a743 added: #genTimestampUUID
Stefan Vogel <sv@exept.de>
parents: 2428
diff changeset
   273
!
ba791db4a743 added: #genTimestampUUID
Stefan Vogel <sv@exept.de>
parents: 2428
diff changeset
   274
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   275
genUUID
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   276
    "generate a uuid.
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   277
     If a physical mac address can be retrieved from the OS,
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   278
     a mac-address/timestamp uuid is generated,
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   279
     otherwise a random uuid will be generated."
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   281
    CachedMACAddress isNil ifTrue:[
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   282
        self getValidMACAddress.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   283
    ].
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   284
    CachedMACAddress == false ifTrue:[
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   285
        "no mac address - generate random UUID"
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   286
        ^ self genRandomUUID.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   287
    ].
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   288
    ^ (super basicNew:16) genTimestampUUID.
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   289
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   290
    "
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   291
      self genUUID
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   292
    "
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   293
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   294
    "
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   295
      1 to: 100 do:[ : el |
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   296
          Transcript show:el.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   297
          Transcript show:': '.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   298
          Transcript showCR:(UUID genUUID).
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   299
      ].
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   300
    "
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
!
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   303
genUUID:nameStringOrBytes inNameSpace:namespaceStringOrUUID
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   304
    "generate a namespace UUID (Version 5, hashed by SHA-1).
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   305
     See RFC4122."
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   306
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   307
    ^ (super basicNew:16) genUUID:nameStringOrBytes inNameSpace:namespaceStringOrUUID
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   308
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   309
    "
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   310
        self genUUID:'www.example.org' inNameSpace:'DNS'.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   311
        self genUUID:'http://www.exept.de' inNameSpace:'URL'.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   312
        self genUUID:'1.2.3.4.5' inNameSpace:'OID'.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   313
        self genUUID:(OSI::DERCoder encode:(OSI::ASN1_OID newID:#(1 2 3 4 5))) 
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   314
             inNameSpace:'OID'.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   315
        self genUUID:'c=de, o=eXept Software AG, cn=Development' 
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   316
             inNameSpace:'X500'.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   317
        self genUUID:(OSI::DERCoder encode:(OSI::DistinguishedName fromString:'c=de, o=eXept Software AG, cn=Development') asAsn1Value) 
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   318
             inNameSpace:'X500'.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   319
    "
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   320
!
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   321
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   322
new
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   323
    ^ self genUUID
1923
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   324
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   325
    "
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   326
     self new 
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   327
    "
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
!
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   329
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   330
readFrom:aStringOrStream onError:errorBlock
2954
1f5e5edc610d class: UUID
Stefan Vogel <sv@exept.de>
parents: 2950
diff changeset
   331
    |d offs s uuid t byte hasBrackets|
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   332
1543
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   333
    Error handle:[:ex |
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   334
        ^ errorBlock value.
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   335
    ] do:[
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   336
        s := aStringOrStream readStream.
1923
6380ea5d7385 #new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents: 1898
diff changeset
   337
        uuid := super basicNew:16.
2954
1f5e5edc610d class: UUID
Stefan Vogel <sv@exept.de>
parents: 2950
diff changeset
   338
        hasBrackets := false.
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   339
5218
87cac42cf4f8 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5077
diff changeset
   340
        s skipSeparators == ${ ifTrue:[
2954
1f5e5edc610d class: UUID
Stefan Vogel <sv@exept.de>
parents: 2950
diff changeset
   341
            s next; skipSeparators.
1f5e5edc610d class: UUID
Stefan Vogel <sv@exept.de>
parents: 2950
diff changeset
   342
            hasBrackets := true.
1f5e5edc610d class: UUID
Stefan Vogel <sv@exept.de>
parents: 2950
diff changeset
   343
        ].
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   344
1543
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   345
        t := s next:8.
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   346
        d := Integer readFrom:t radix:16 onError:[^ errorBlock value].
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   347
        uuid unsignedInt32At:1 put:d MSB:true.
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   348
        offs := 5.
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   349
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   350
        s next.         "skip $-"
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   351
1601
b0dfbe332caf WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   352
        1 to:2 do:[:i |
1543
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   353
            t := s next:4.
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   354
            d := Integer readFrom:t radix:16 onError:[^ errorBlock value].
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   355
            uuid unsignedInt16At:offs put:d MSB:true.
1543
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   356
            offs := offs + 2.
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   357
            s next.     "skip $-"
1543
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   358
        ].
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   359
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   360
        1 to:2 do:[:i |
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   361
            t := s next:2.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   362
            d := Integer readFrom:t radix:16 onError:[^ errorBlock value].
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   363
            uuid at:offs put:d.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   364
            offs := offs + 1.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   365
        ].
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   366
        s next.         "skip $-"
1601
b0dfbe332caf WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   367
1543
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   368
        1 to:6 do:[:i |
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   369
            t := s next:2.
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   370
            byte := Integer readFrom:t radix:16 onError:[^ errorBlock value].
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   371
            uuid at:offs put:byte.
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   372
            offs := offs + 1.
929986f0910a fixed readFrom:onError:
Claus Gittinger <cg@exept.de>
parents: 1491
diff changeset
   373
        ].
2954
1f5e5edc610d class: UUID
Stefan Vogel <sv@exept.de>
parents: 2950
diff changeset
   374
        hasBrackets ifTrue:[
5218
87cac42cf4f8 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5077
diff changeset
   375
            s skipSeparators == $} 
2954
1f5e5edc610d class: UUID
Stefan Vogel <sv@exept.de>
parents: 2950
diff changeset
   376
                ifTrue:[s next] 
1f5e5edc610d class: UUID
Stefan Vogel <sv@exept.de>
parents: 2950
diff changeset
   377
                ifFalse:errorBlock
1f5e5edc610d class: UUID
Stefan Vogel <sv@exept.de>
parents: 2950
diff changeset
   378
        ].
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   379
    ].
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   380
    ^ uuid
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   381
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   382
    "
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   383
     UUID readFrom:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77' 
5218
87cac42cf4f8 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5077
diff changeset
   384
     UUID readFrom:'{5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77 }' 
2954
1f5e5edc610d class: UUID
Stefan Vogel <sv@exept.de>
parents: 2950
diff changeset
   385
     UUID readFrom:'{5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77' 
1f5e5edc610d class: UUID
Stefan Vogel <sv@exept.de>
parents: 2950
diff changeset
   386
     UUID fromString:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77 bllll' 
1898
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   387
     UUID fromString:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77' 
1601
b0dfbe332caf WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents: 1577
diff changeset
   388
     UUID fromString:'00000001-0000-0000-C000-000000000046'
1898
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   389
     UUID fromString:'00000001-0000-0000-C000-000000004600'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   390
     UUID fromString:'00000001-0000-0000-C000-000000460000'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   391
     UUID fromString:'00000001-0000-0000-C000-000046000000'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   392
     UUID fromString:'00000001-0000-0000-C000-004600000000'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   393
     UUID fromString:'00000001-0000-0000-C000-460000000000'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   394
     UUID fromString:'00000001-0000-0000-C046-000000000000'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   395
     UUID fromString:'00000001-0000-0046-C000-000000000000'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   396
     UUID fromString:'00000001-0000-4600-C000-000000000000'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   397
     UUID fromString:'00000001-0046-0000-C000-000000000000'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   398
     UUID fromString:'00000001-4600-0000-C000-000000000000'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   399
     UUID fromString:'00000100-4600-0000-C000-000000000000'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   400
     UUID fromString:'00010000-4600-0000-C000-000000000000'
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   401
     UUID fromString:'10000000-4600-0000-C000-000000000000'
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   402
    "
1898
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   403
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   404
    "Modified: / 10-10-2007 / 23:03:47 / cg"
5218
87cac42cf4f8 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 5077
diff changeset
   405
    "Modified: / 28-09-2019 / 15:56:45 / Stefan Vogel"
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
! !
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   408
!UUID class methodsFor:'helpers'!
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   409
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   410
adjustByteOrder:aByteArray
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   411
    "change the byte order of the uuid"
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   412
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   413
    |d|
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   414
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   415
    d := aByteArray unsignedInt32At:1 MSB:false.
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   416
    aByteArray unsignedInt32At:1 put:d MSB:true.
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   417
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   418
    d := aByteArray unsignedInt16At:1+4 MSB:false.
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   419
    aByteArray unsignedInt16At:1+4 put:d MSB:true.
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   420
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   421
    d := aByteArray unsignedInt16At:1+4+2 MSB:false.
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   422
    aByteArray unsignedInt16At:1+4+2 put:d MSB:true.
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   423
!
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   424
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   425
getDtssUtcTime
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   426
    "return the DTSS based time in 100 nsec intervals
1647
124e977b6a8f comment
Stefan Vogel <sv@exept.de>
parents: 1613
diff changeset
   427
     DTSS UTC base time is October 15, 1582 (the start of the Gregorian Calendar)."
124e977b6a8f comment
Stefan Vogel <sv@exept.de>
parents: 1613
diff changeset
   428
124e977b6a8f comment
Stefan Vogel <sv@exept.de>
parents: 1613
diff changeset
   429
     "Unix base time is January 1, 1970.
124e977b6a8f comment
Stefan Vogel <sv@exept.de>
parents: 1613
diff changeset
   430
      The difference between both is: 122192928000000000"
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   431
4298
a66a586d1aea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3999
diff changeset
   432
     "   
1647
124e977b6a8f comment
Stefan Vogel <sv@exept.de>
parents: 1613
diff changeset
   433
        (Timestamp epoch asDate subtractDate:(Date day:15 month:10 year:1582))
124e977b6a8f comment
Stefan Vogel <sv@exept.de>
parents: 1613
diff changeset
   434
        *  (24 * 60 * 60)
4298
a66a586d1aea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3999
diff changeset
   435
        * 1000 
a66a586d1aea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3999
diff changeset
   436
        * 1000 
a66a586d1aea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3999
diff changeset
   437
        * 10        
a66a586d1aea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3999
diff changeset
   438
    "
1613
feef745939ea Use UTC time
Stefan Vogel <sv@exept.de>
parents: 1612
diff changeset
   439
4496
7ba22942a728 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4354
diff changeset
   440
    ^ (OperatingSystem getMicrosecondTime + 12219292800000000) * 10 
7ba22942a728 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4354
diff changeset
   441
    "/ ^ (Timestamp now getMilliseconds + 12219292800000) * 10000   .
4298
a66a586d1aea #DOCUMENTATION by cg
Claus Gittinger <cg@exept.de>
parents: 3999
diff changeset
   442
4496
7ba22942a728 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4354
diff changeset
   443
    "Modified: / 05-08-2017 / 10:17:57 / cg"
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   444
!
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   445
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   446
getValidMACAddress
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   447
    "return the first valid MAC address (i.e. having at least 6 bytes with the first six bytes ~~ 0).
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   448
     As a side effect, initialize anything needed for timestamp UUIDs."
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   449
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   450
    CachedMACAddress isNil ifTrue:[
4634
134765d28a26 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4623
diff changeset
   451
        Lock isNil ifTrue:[
134765d28a26 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4623
diff changeset
   452
            "/ is there an early access possible befor initialization?
134765d28a26 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4623
diff changeset
   453
            self initialize.
134765d28a26 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4623
diff changeset
   454
        ].
2978
4497be5fc993 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2977
diff changeset
   455
        CachedMACAddress := false.      "negative caching: remember the fact, that there is no MAC address" 
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   456
        PrimitiveFailure catch:[
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   457
            CachedMACAddress := OperatingSystem getNetworkMACAddresses 
2978
4497be5fc993 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2977
diff changeset
   458
                                    detect:[:macAddress | macAddress size >= 6 
4497be5fc993 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2977
diff changeset
   459
                                                            and:[(macAddress startsWith:#[0 0 0 0 0 0]) not]]
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   460
                                    ifNone:false.
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   461
        ].
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   462
        "try again when restarted from a snapshot"
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   463
        ObjectMemory addDependent:self.
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   464
    ].
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   465
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   466
    ^ CachedMACAddress
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   467
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   468
    "
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   469
       CachedMACAddress := nil.
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   470
       self getValidMACAddress
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   471
    "
1491
512b21b45a9e noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents: 1467
diff changeset
   472
512b21b45a9e noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents: 1467
diff changeset
   473
    "Modified: / 17-11-2004 / 01:45:53 / cg"
4634
134765d28a26 #BUGFIX by stefan
Stefan Vogel <sv@exept.de>
parents: 4623
diff changeset
   474
    "Modified: / 19-04-2018 / 10:13:18 / stefan"
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   475
! !
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   476
3807
92abd3d375d0 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3769
diff changeset
   477
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   478
!UUID methodsFor:'accessing'!
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   479
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   480
clockSeqHiAndReserved
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   481
    ^ self at:9
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   482
!
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   483
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   484
clockSeqLow
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   485
    ^ self at:10
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   486
!
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   487
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   488
node
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   489
    "answer the node (ethernet) address of this uuid"
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   490
3122
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   491
    self isTimestampUUID ifFalse:[
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   492
        ConversionError raiseWith:self errorString:' - trying to get node address from random UUID'.
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   493
    ].            
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   494
    ^ (ByteArray new:6) replaceFrom:1 to:6 with:self startingAt:11.
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   495
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   496
    "
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   497
       (self allInstances 
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   498
            select:[:e| e isTimestampUUID] 
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   499
            thenCollect:[:e| e node]) asBag
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   500
    "
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   501
!
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   502
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   503
timeHighAndVersion
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   504
    ^ self unsignedInt16At:7 MSB:true
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   505
!
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   506
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   507
timeLow
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   508
    ^ self unsignedInt32At:1 MSB:true
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   509
!
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   510
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   511
timeMid
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   512
    ^ self unsignedInt16At:5 MSB:true
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   513
!
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   514
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   515
timestamp
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   516
    "Get the UTC timestamp, when the UUID has been created.
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   517
     This is only valid for timestampUUIDs"
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   518
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   519
    |low med high dtssUtcTime|
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   520
3121
ca7d3132c33f class: UUID
Stefan Vogel <sv@exept.de>
parents: 2978
diff changeset
   521
    self isTimestampUUID ifFalse:[
ca7d3132c33f class: UUID
Stefan Vogel <sv@exept.de>
parents: 2978
diff changeset
   522
        ConversionError raiseWith:self errorString:' - trying to get timestamp from random UUID'.
ca7d3132c33f class: UUID
Stefan Vogel <sv@exept.de>
parents: 2978
diff changeset
   523
    ].            
ca7d3132c33f class: UUID
Stefan Vogel <sv@exept.de>
parents: 2978
diff changeset
   524
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   525
    low := self unsignedInt32At:1 MSB:true.
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   526
    med := self unsignedInt16At:5 MSB:true.
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   527
    high := (self unsignedInt16At:7 MSB:true) bitAnd:16rFFF.
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   528
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   529
    dtssUtcTime := low + ((med + (high bitShift:16)) bitShift:32).
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   530
3850
5c705a67a0a1 #REFACTORING by stefan
Stefan Vogel <sv@exept.de>
parents: 3807
diff changeset
   531
    ^ UtcTimestamp utcMillisecondsSince1970:(dtssUtcTime//10000) - 12219292800000.
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   532
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   533
    "
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   534
        self genTimestampUUID timestamp
3121
ca7d3132c33f class: UUID
Stefan Vogel <sv@exept.de>
parents: 2978
diff changeset
   535
        self genRandomUUID timestamp
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   536
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   537
        (self allInstances asSet asArray select:[:e| e isTimestampUUID] 
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   538
                                            thenCollect:[:e| e timestamp]) sort
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   539
    "
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   540
!
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   541
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   542
version
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   543
    "the version number of the uuid"
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   544
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   545
    ^ ((self at:7) bitAnd:16rF0) bitShift:-4.
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   546
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   547
    "
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   548
        self genTimestampUUID version
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   549
        self genRandomUUID version
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   550
    "
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   551
! !
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   552
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   553
!UUID methodsFor:'converting'!
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   554
1382
626693b1fed7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1320
diff changeset
   555
asBytes
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   556
    "convert this UUID to a ByteArray in network byte order (MSB-first)"
1382
626693b1fed7 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1320
diff changeset
   557
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   558
    |bytes|
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   559
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   560
    bytes := ByteArray new:16.
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   561
    bytes replaceFrom:1 to:16 with:self.
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   562
    ^ bytes
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   563
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   564
    "
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   565
     |bytes|
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   566
        
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   567
     bytes := #[16r01 16r02 16r03 16r04
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   568
                      16r05 16r06 16r07 16r08
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   569
                      16r09 16r10 16r11 16r12
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   570
                      16r13 16r14 16r15 16r16].
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   571
     (UUID fromBytes:bytes) asBytes ~= bytes ifTrue:[self halt] 
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   572
    "
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   573
!
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   574
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   575
asBytesMSB:msb
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   576
    "convert this UUID to a ByteArray.
1898
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   577
     If msb == false, it is converted into LSB-first byte ordering"
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   578
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   579
    |bytes|
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   580
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   581
    bytes := self asBytes.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   582
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   583
    msb ifFalse:[
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   584
        self class adjustByteOrder:bytes.
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
    ].
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
    ^ bytes
1898
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   587
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   588
    "Modified: / 10-10-2007 / 22:51:09 / cg"
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   589
!
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   590
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   591
asNativeBytes
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   592
    "convert this uuid to a ByteArray in host byte order.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   593
     Use this only to pass the UUID to the OS (Windows)"
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   594
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   595
    |bytes|
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   596
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   597
    bytes := self asBytes.
3542
b00b42c1378d class: UUID
Stefan Vogel <sv@exept.de>
parents: 3122
diff changeset
   598
    IsBigEndian ifFalse:[
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   599
        self class adjustByteOrder:bytes.
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   600
    ].
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   601
    ^ bytes.
2021
21f1faba832a literalArray
fm
parents: 2018
diff changeset
   602
!
21f1faba832a literalArray
fm
parents: 2018
diff changeset
   603
3585
65f8ba24fddc class: UUID
Stefan Vogel <sv@exept.de>
parents: 3549
diff changeset
   604
asString
65f8ba24fddc class: UUID
Stefan Vogel <sv@exept.de>
parents: 3549
diff changeset
   605
    ^ self printString
65f8ba24fddc class: UUID
Stefan Vogel <sv@exept.de>
parents: 3549
diff changeset
   606
65f8ba24fddc class: UUID
Stefan Vogel <sv@exept.de>
parents: 3549
diff changeset
   607
    "
65f8ba24fddc class: UUID
Stefan Vogel <sv@exept.de>
parents: 3549
diff changeset
   608
        self genUUID asString
65f8ba24fddc class: UUID
Stefan Vogel <sv@exept.de>
parents: 3549
diff changeset
   609
    "
65f8ba24fddc class: UUID
Stefan Vogel <sv@exept.de>
parents: 3549
diff changeset
   610
!
65f8ba24fddc class: UUID
Stefan Vogel <sv@exept.de>
parents: 3549
diff changeset
   611
2170
8cd3d9480ab7 new: #asUUID
Stefan Vogel <sv@exept.de>
parents: 2132
diff changeset
   612
asUUID
8cd3d9480ab7 new: #asUUID
Stefan Vogel <sv@exept.de>
parents: 2132
diff changeset
   613
    ^ self
8cd3d9480ab7 new: #asUUID
Stefan Vogel <sv@exept.de>
parents: 2132
diff changeset
   614
!
8cd3d9480ab7 new: #asUUID
Stefan Vogel <sv@exept.de>
parents: 2132
diff changeset
   615
2021
21f1faba832a literalArray
fm
parents: 2018
diff changeset
   616
literalArrayEncoding
21f1faba832a literalArray
fm
parents: 2018
diff changeset
   617
    ^ Array 
21f1faba832a literalArray
fm
parents: 2018
diff changeset
   618
        with:(self class name)
21f1faba832a literalArray
fm
parents: 2018
diff changeset
   619
        with:(self printString).
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   620
! !
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   621
2132
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   622
!UUID methodsFor:'copying'!
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   623
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   624
deepCopy
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   625
    "I am never changed, after I have been created.
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   626
     So there is no need to make a copy"
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   627
2132
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   628
    ^ self
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   629
!
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   630
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   631
deepCopyUsing:aDictionary postCopySelector:postCopySelector
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   632
    "I am never changed, after I have been created.
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   633
     So there is no need to make a copy"
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   634
2132
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   635
    ^ self
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   636
!
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   637
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   638
shallowCopy
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   639
    "I am never changed, after I have been created.
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   640
     So there is no need to make a copy"
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   641
2132
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   642
    ^ self
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   643
!
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   644
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   645
simpleDeepCopy
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   646
    "I am never changed, after I have been created.
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   647
     So there is no need to make a copy"
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   648
2132
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   649
    ^ self
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   650
! !
c580316610e1 copying - a UUID is unique...
ca
parents: 2069
diff changeset
   651
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   652
!UUID methodsFor:'generating uuids'!
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   653
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   654
genTimestampUUID
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   655
    "generate a timestamp (and mac-address) based uuid"
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   656
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   657
    |macBytes utcTime seqNr|
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   658
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   659
    (self at:7) ~~ 0 ifTrue:[
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   660
        "once created, an UUID is immutable"
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   661
        self noModificationError.
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   662
    ].
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   663
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   664
    macBytes := CachedMACAddress.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   665
    macBytes isNil ifTrue:[
2701
d726c35f7084 changed: #genTimestampUUID
Claus Gittinger <cg@exept.de>
parents: 2495
diff changeset
   666
        macBytes := self class getValidMACAddress.
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   667
        macBytes size < 6 ifTrue:[
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   668
            self error:'no mac address - cannot generate UUID'.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   669
        ].
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   670
    ].
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   671
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   672
    "use 60 bit counter of 100ns ticks since 00:00:00 15.oct 1582 (sigh)"
1610
5f79965e186b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1608
diff changeset
   673
    utcTime := self class getDtssUtcTime.
5f79965e186b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1608
diff changeset
   674
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   675
    Lock critical:[
1467
a394ade68816 Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents: 1447
diff changeset
   676
        SequenceNumber isNil ifTrue:[
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   677
            SequenceNumber := RandomGenerator new nextIntegerBetween:0 and:16383.
1467
a394ade68816 Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents: 1447
diff changeset
   678
        ].
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   679
        utcTime > (LastTime+Increment) ifTrue:[
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   680
            Increment := 0.
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   681
        ] ifFalse:[
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   682
            LastTime = utcTime ifTrue:[
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   683
                "clock didn't advance since last call. Simply add a tick"
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   684
                Increment := Increment + 1.
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   685
            ] ifFalse:[
1467
a394ade68816 Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents: 1447
diff changeset
   686
                "clock went backwards - increment SequenceNumber"
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   687
                Increment := 0.
1214
7ae6b9302754 Fix previous change
Stefan Vogel <sv@exept.de>
parents: 1213
diff changeset
   688
                SequenceNumber := SequenceNumber + 1.
7ae6b9302754 Fix previous change
Stefan Vogel <sv@exept.de>
parents: 1213
diff changeset
   689
                SequenceNumber >= 16384 ifTrue:[SequenceNumber := 0].
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   690
            ].
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   691
        ].
1214
7ae6b9302754 Fix previous change
Stefan Vogel <sv@exept.de>
parents: 1213
diff changeset
   692
        
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   693
        LastTime := utcTime.
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   694
        utcTime := utcTime + Increment.
1610
5f79965e186b *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 1608
diff changeset
   695
        seqNr := SequenceNumber.
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   696
    ].
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   697
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   698
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   699
    "time low: long"
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   700
    self unsignedInt32At:1 put:(utcTime bitAnd:16rFFFFFFFF) MSB:true.
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   701
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   702
    "time med: short"
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   703
    utcTime := utcTime bitShift:-32.
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   704
    self unsignedInt16At:5 put:(utcTime bitAnd:16rFFFF) MSB:true.
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   705
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   706
    "time high and version: short
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   707
     multiplex the 4 bit version number in highest 4 bits (version 1 -> time based version)"
3769
dd72d932f033 #REFACTORING
Claus Gittinger <cg@exept.de>
parents: 3641
diff changeset
   708
    self unsignedInt16At:7 put:((utcTime bitShift:-16) bitOr:16r1000) MSB:true.
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   709
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   710
    "2 sequence bytes + reserved bits (this is not a short!!)"
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   711
    self at:9  put:(((seqNr bitShift:-8) bitAnd:16r3F) bitOr:16r80).
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   712
    self at:10 put:(seqNr bitAnd:16rFF).
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   713
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   714
    "48 bits of MAC-Address"
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   715
    self replaceFrom:11 to:16 with:macBytes startingAt:1.
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   716
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   717
    "
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   718
      self genTimestampUUID
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   719
      self genTimestampUUID genTimestampUUID
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   720
    "
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   721
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   722
    "
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   723
      1 to: 100 do:[ : el |
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   724
          Transcript show:el.
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   725
          Transcript show:': '.
4496
7ba22942a728 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4354
diff changeset
   726
          Transcript showCR:(UUID genTimestampUUID).
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   727
      ].
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   728
    "
2701
d726c35f7084 changed: #genTimestampUUID
Claus Gittinger <cg@exept.de>
parents: 2495
diff changeset
   729
d726c35f7084 changed: #genTimestampUUID
Claus Gittinger <cg@exept.de>
parents: 2495
diff changeset
   730
    "Modified: / 26-12-2011 / 13:41:23 / cg"
4496
7ba22942a728 #FEATURE by cg
Claus Gittinger <cg@exept.de>
parents: 4354
diff changeset
   731
    "Modified (comment): / 05-08-2017 / 10:08:44 / cg"
1608
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   732
!
e1cf5d2b6f2f Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents: 1602
diff changeset
   733
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   734
genUUID:nameStringOrBytes inNameSpace:namespaceStringOrUUID
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   735
    "generate a namespace UUID (Version 5, hashed by SHA-1).
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   736
     See RFC4122."
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   737
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   738
    |sha1 namespaceUUID|
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   739
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   740
    (self at:7) ~~ 0 ifTrue:[
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   741
        "once created, an UUID is immutable"
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   742
        self noModificationError.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   743
    ].
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   744
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   745
    namespaceStringOrUUID isUUID ifTrue:[
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   746
        namespaceUUID := namespaceStringOrUUID.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   747
    ] ifFalse:[
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   748
        "this throws an exception, if namespaceStringOrUUID is unknown"
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   749
        namespaceUUID := self class nameSpaceToUuidBytes at:namespaceStringOrUUID.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   750
    ].
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   751
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   752
    sha1 := SHA1Stream new.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   753
    sha1 
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   754
        nextPutAll:namespaceUUID;
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   755
        nextPutAll:nameStringOrBytes.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   756
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   757
    self replaceFrom:1 to:16 with:(sha1 hashValue).
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   758
    "multiplex the 4 bit version number (Version 5 -> SHA1 Namspace UUID) in high bits of byte 7"
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   759
    self at:7 put:(((self at:7) bitAnd:16r0F) bitOr:16r50).
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   760
    self at:9 put:(((self at:9) bitAnd:16r3F) bitOr:16r80).
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   761
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   762
    "
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   763
        self genUUID:'www.example.org' inNameSpace:'DNS'.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   764
        self genUUID:'http://www.exept.de' inNameSpace:'URL'.
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   765
        self genUUID:'some text' inNameSpace:(UUID fromString:'885f7d80-7d60-11e5-a133-101f74535bd0').
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   766
        self genUUID:'1.2.3.4.5' inNameSpace:'OID'.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   767
        self genUUID:(OSI::DERCoder encode:(OSI::ASN1_OID newID:#(1 2 3 4 5))) 
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   768
             inNameSpace:'OID'.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   769
        self genUUID:'c=de, o=eXept Software AG, cn=Development' 
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   770
             inNameSpace:'X500'.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   771
        self genUUID:(OSI::DERCoder encode:(OSI::DistinguishedName fromString:'c=de, o=eXept Software AG, cn=Development') asAsn1Value) 
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   772
             inNameSpace:'X500'.
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   773
    "
3641
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   774
!
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   775
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   776
setRandomUUIDFromBytes:sixteenBytes
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   777
    "answer a randomly generated uuid as defined in RFC4122 section 4.4"
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   778
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   779
    (self at:7) ~~ 0 ifTrue:[
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   780
        "once created, an UUID is immutable"
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   781
        self noModificationError.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   782
    ].
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   783
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   784
    self replaceFrom:1 to:16 with:sixteenBytes.
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   785
    "multiplex the 4 bit version number (Version 4 -> Random UUID) in high bits of byte 7"
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   786
    self at:7 put:(((self at:7) bitAnd:16r0F) bitOr:16r40).
01c3ae2c1edf #FEATURE
Stefan Vogel <sv@exept.de>
parents: 3585
diff changeset
   787
    self at:9 put:(((self at:9) bitAnd:16r3F) bitOr:16r80).
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   788
! !
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   789
2426
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   790
!UUID methodsFor:'hashing'!
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   791
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   792
hash
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   793
   "Generate a 30 bit hash value.
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   794
    For Timestamp-UUIDs:
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   795
        Bytes 1,2,3,4 are the least significant bits of the timestamp,
2428
b283e32a8db5 changed: #hash
Stefan Vogel <sv@exept.de>
parents: 2426
diff changeset
   796
        Bytes 13,14,15,16 are the least significant bytes of the mac address - 
2426
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   797
            but considering these bytes does not generate a better hash to
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   798
            justify the additional computations.
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   799
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   800
    For random UUIDs, every byte is random anyway."
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   801
5077
5ff492ca37d4 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5076
diff changeset
   802
    ^ (self computeXorHashFrom:1 to:4) 
5ff492ca37d4 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 5076
diff changeset
   803
    "/ bitXor:(self computeXorHashFrom:13 to:16)
2426
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   804
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   805
    "
5076
185b8f374d44 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4634
diff changeset
   806
     |allHashes nonConflictsRatio|
185b8f374d44 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4634
diff changeset
   807
185b8f374d44 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4634
diff changeset
   808
     allHashes := UUID allInstances collect:[:each| each hash].
185b8f374d44 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4634
diff changeset
   809
     Transcript showCR:'collisions: %1 (in %2 UUIDs)' with:(allHashes size - allHashes asSet size) with:allHashes size.  
185b8f374d44 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4634
diff changeset
   810
     nonConflictsRatio := (allHashes asSet size / allHashes size) asFloat asFixedPoint:2.
185b8f374d44 #DOCUMENTATION by exept
Claus Gittinger <cg@exept.de>
parents: 4634
diff changeset
   811
     Transcript showCR:'%1%% collision rate' with:(1 asFixedPoint - nonConflictsRatio)*100
2426
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   812
    "
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   813
! !
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   814
3121
ca7d3132c33f class: UUID
Stefan Vogel <sv@exept.de>
parents: 2978
diff changeset
   815
4354
ad0e143f19bb #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4298
diff changeset
   816
!UUID methodsFor:'printing & storing'!
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   817
2495
721d3888cbab Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 2433
diff changeset
   818
displayOn:aGCOrStream
721d3888cbab Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 2433
diff changeset
   819
    "/ what a kludge - Dolphin and Squeak mean: printOn: a stream;
4354
ad0e143f19bb #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4298
diff changeset
   820
    "/ old ST80 means: draw-yourself on a GC.
ad0e143f19bb #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4298
diff changeset
   821
    aGCOrStream isStream ifFalse:[
ad0e143f19bb #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4298
diff changeset
   822
        ^ super displayOn:aGCOrStream.
2495
721d3888cbab Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 2433
diff changeset
   823
    ].
721d3888cbab Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents: 2433
diff changeset
   824
4354
ad0e143f19bb #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4298
diff changeset
   825
    self printOn:aGCOrStream.
2977
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   826
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   827
    "
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   828
        self genUUID displayOn:Transcript
afc4d10019c7 class: UUID
Stefan Vogel <sv@exept.de>
parents: 2954
diff changeset
   829
    "
4354
ad0e143f19bb #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4298
diff changeset
   830
ad0e143f19bb #OTHER by cg
Claus Gittinger <cg@exept.de>
parents: 4298
diff changeset
   831
    "Modified: / 22-02-2017 / 16:58:50 / cg"
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   832
!
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   833
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   834
printOn:aStream
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   835
    1 to:4 do:[:idx|
3999
86895a4f348a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3850
diff changeset
   836
        (self basicAt:idx) printOn:aStream base:-16 size:2 fill:$0.
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   837
    ].
3999
86895a4f348a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3850
diff changeset
   838
    aStream nextPut:$-.
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   839
    5 to:6 do:[:idx|
3999
86895a4f348a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3850
diff changeset
   840
        (self basicAt:idx) printOn:aStream base:-16 size:2 fill:$0.
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   841
    ].
3999
86895a4f348a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3850
diff changeset
   842
    aStream nextPut:$-.
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   843
    7 to:8 do:[:idx|
3999
86895a4f348a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3850
diff changeset
   844
        (self basicAt:idx) printOn:aStream base:-16 size:2 fill:$0.
1612
2877abd0a002 Byte ordering issues
Stefan Vogel <sv@exept.de>
parents: 1611
diff changeset
   845
    ].
3999
86895a4f348a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3850
diff changeset
   846
    aStream nextPut:$-.
86895a4f348a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3850
diff changeset
   847
    9 to:10 do:[:idx|
86895a4f348a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3850
diff changeset
   848
        (self basicAt:idx) printOn:aStream base:-16 size:2 fill:$0.
86895a4f348a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3850
diff changeset
   849
    ].
86895a4f348a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3850
diff changeset
   850
    aStream nextPut:$-.
1602
6ffbb8db78a4 Fix byte ordering.
Stefan Vogel <sv@exept.de>
parents: 1601
diff changeset
   851
    11 to:16 do:[:idx|
3999
86895a4f348a #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 3850
diff changeset
   852
        (self basicAt:idx) printOn:aStream base:-16 size:2 fill:$0.
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   853
    ].
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   854
2741
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   855
    "
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   856
     UUID genUUID printString 
3585
65f8ba24fddc class: UUID
Stefan Vogel <sv@exept.de>
parents: 3549
diff changeset
   857
     UUID genUUID asString 
2741
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   858
    "
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   859
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   860
"/    d := self unsignedLongAt:1 bigEndian:true.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   861
"/    d printOn:tmpStream base:16 size:8 fill:$0.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   862
"/    tmpStream nextPut:$-.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   863
"/
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   864
"/    d := self unsignedShortAt:1+4 bigEndian:true.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   865
"/    d printOn:tmpStream base:16 size:4 fill:$0.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   866
"/    tmpStream nextPut:$-.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   867
"/
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   868
"/    d := self unsignedShortAt:1+4+2 bigEndian:true.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   869
"/    d printOn:tmpStream base:16 size:4 fill:$0.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   870
"/    tmpStream nextPut:$-.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   871
"/
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   872
"/    d := self at:1+4+2+2.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   873
"/    d printOn:tmpStream base:16 size:2 fill:$0.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   874
"/    d := self at:1+4+2+2+1.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   875
"/    d printOn:tmpStream base:16 size:2 fill:$0.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   876
"/    tmpStream nextPut:$-.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   877
"/
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   878
"/    11 to:16 do:[:idx|
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   879
"/        d := self at:idx.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   880
"/        d printOn:tmpStream base:16 size:2 fill:$0.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   881
"/    ].
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   882
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   883
1898
8041791c02c3 comments
Claus Gittinger <cg@exept.de>
parents: 1647
diff changeset
   884
    "Modified: / 10-10-2007 / 23:19:03 / cg"
2069
e84a7576e3ee storeString
sr
parents: 2021
diff changeset
   885
!
e84a7576e3ee storeString
sr
parents: 2021
diff changeset
   886
4623
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   887
printString 
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   888
    "return a printed representation of the receiver for printing.
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   889
     Uses the basic print mechanism of the underlying technology."
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   890
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   891
    |s|
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   892
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   893
    "Speed: we know, that the printed representation of a UUID has exactly 36 chars"
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   894
    s := WriteStream on:(String new:36).
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   895
    self printOn:s.
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   896
    ^ s contents.
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   897
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   898
    "
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   899
        UUID genUUID printString
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   900
    "
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   901
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   902
    "Created: / 20-03-2018 / 16:08:10 / stefan"
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   903
!
29cb9dac3a89 #TUNING by stefan
Stefan Vogel <sv@exept.de>
parents: 4505
diff changeset
   904
2069
e84a7576e3ee storeString
sr
parents: 2021
diff changeset
   905
storeOn:aStream
e84a7576e3ee storeString
sr
parents: 2021
diff changeset
   906
    aStream 
2741
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   907
        nextPut:$(; 
2069
e84a7576e3ee storeString
sr
parents: 2021
diff changeset
   908
        nextPutAll:self class name; 
2741
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   909
        nextPutAll:' fromString:'''.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   910
    self printOn:aStream.
00d17c900df5 comment/format in: #printOn:
Stefan Vogel <sv@exept.de>
parents: 2739
diff changeset
   911
    aStream nextPutAll:''')'.
2069
e84a7576e3ee storeString
sr
parents: 2021
diff changeset
   912
e84a7576e3ee storeString
sr
parents: 2021
diff changeset
   913
    "
e84a7576e3ee storeString
sr
parents: 2021
diff changeset
   914
     Object readFrom:(UUID genUUID storeString) 
e84a7576e3ee storeString
sr
parents: 2021
diff changeset
   915
    "
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   916
! !
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   917
2755
a3017684fcd3 added: #isUUID
Stefan Vogel <sv@exept.de>
parents: 2741
diff changeset
   918
!UUID methodsFor:'testing'!
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   919
3122
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   920
isRandomUUID
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   921
    ^ self version == 4
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   922
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   923
    "
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   924
      self genRandomUUID isRandomUUID
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   925
    "
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   926
!
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   927
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   928
isTimestampUUID
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   929
    ^ self version == 1
3122
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   930
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   931
    "
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   932
      self genTimestampUUID isTimestampUUID
9d0a142eee9f class: UUID
Stefan Vogel <sv@exept.de>
parents: 3121
diff changeset
   933
    "
2755
a3017684fcd3 added: #isUUID
Stefan Vogel <sv@exept.de>
parents: 2741
diff changeset
   934
!
a3017684fcd3 added: #isUUID
Stefan Vogel <sv@exept.de>
parents: 2741
diff changeset
   935
a3017684fcd3 added: #isUUID
Stefan Vogel <sv@exept.de>
parents: 2741
diff changeset
   936
isUUID
a3017684fcd3 added: #isUUID
Stefan Vogel <sv@exept.de>
parents: 2741
diff changeset
   937
    ^ true
2738
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   938
! !
Stefan Vogel <sv@exept.de>
parents: 2701
diff changeset
   939
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   940
!UUID class methodsFor:'documentation'!
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   941
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   942
version
3585
65f8ba24fddc class: UUID
Stefan Vogel <sv@exept.de>
parents: 3549
diff changeset
   943
    ^ '$Header$'
2426
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   944
!
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   945
5348d3922323 added: #hash
Stefan Vogel <sv@exept.de>
parents: 2170
diff changeset
   946
version_CVS
3585
65f8ba24fddc class: UUID
Stefan Vogel <sv@exept.de>
parents: 3549
diff changeset
   947
    ^ '$Header$'
1188
58856b846271 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   948
! !
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   949
2950
bf0c61112aa1 class: UUID
Claus Gittinger <cg@exept.de>
parents: 2756
diff changeset
   950
1213
5a23a433a990 Refactoring
Stefan Vogel <sv@exept.de>
parents: 1188
diff changeset
   951
UUID initialize!