compiler/TObjectWithProperties.st
changeset 15 10a95d798b36
child 16 17a2d1d9f205
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/compiler/TObjectWithProperties.st	Wed Sep 23 22:21:44 2015 +0100
@@ -0,0 +1,95 @@
+"{ Package: 'jv:tea/compiler' }"
+
+"{ NameSpace: Smalltalk }"
+
+Object subclass:#TObjectWithProperties
+	instanceVariableNames:'properties'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'Languages-Tea-Compiler-Internals'
+!
+
+!TObjectWithProperties methodsFor:'accessing-properties'!
+
+hasProperty: aKey
+    "Test if the property aKey is present."
+    
+    ^ properties notNil and: [ properties includesKey: aKey ]
+
+    "Created: / 20-08-2015 / 18:25:13 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+propertyAt: aKey
+    "Answer the property value associated with aKey."
+    
+    ^ self propertyAt: aKey ifAbsent: [ self error: 'Property not found' ]
+
+    "Created: / 20-08-2015 / 18:23:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+propertyAt: aKey ifAbsent: aBlock
+    "Answer the property value associated with aKey or, if aKey 
+     isn't found, answer the result of evaluating aBlock."
+    
+    ^ properties isNil
+        ifTrue: [ aBlock value ]
+        ifFalse: [ properties at: aKey ifAbsent: aBlock ]
+
+    "Created: / 20-08-2015 / 18:23:56 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+propertyAt: aKey ifAbsentPut: aBlock
+    "Answer the property associated with aKey or, if aKey isn't found 
+     store the result of evaluating aBlock as new value."
+    
+    ^ self propertyAt: aKey ifAbsent: [ self propertyAt: aKey put: aBlock value ]
+
+    "Created: / 20-08-2015 / 18:24:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+propertyAt: aKey put: anObject
+    "Set the property at aKey to be anObject. If aKey is not found, create a new entry 
+     for aKey and set is value to anObject. Answer anObject."
+
+    ^ (properties ifNil: [ properties := RBSmallDictionary new: 1 ])
+            at: aKey put: anObject
+
+    "Created: / 20-08-2015 / 18:24:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+removeProperty: aKey
+    "Remove the property with aKey. Answer the property or raise 
+     an error if aKey isn't found."
+    
+    ^ self removeProperty: aKey ifAbsent: [ self error: 'Property not found' ].
+
+    "Created: / 20-08-2015 / 18:30:16 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+removeProperty: aKey ifAbsent: aBlock
+    "Remove the property with aKey. Answer the value or, if aKey isn't found, answer the result of evaluating aBlock."
+    
+    | answer |
+    properties isNil ifTrue: [ ^ aBlock value ].
+    answer := properties removeKey: aKey ifAbsent: aBlock.
+    properties isEmpty ifTrue: [ properties := nil ].
+    ^ answer
+
+    "Created: / 20-08-2015 / 18:30:41 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+
+!TObjectWithProperties methodsFor:'attributes access'!
+
+objectAttributes
+    ^ properties
+
+    "Created: / 20-08-2015 / 18:32:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified (format): / 23-09-2015 / 21:11:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+objectAttributes:aCollection
+    properties := aCollection
+
+    "Created: / 23-09-2015 / 21:11:32 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+! !
+