From c8f9466256d93dc450592100a347bfe0b4b8951c Mon Sep 17 00:00:00 2001 From: Gabriel Omar Cotelli Date: Mon, 7 Aug 2023 17:03:54 -0300 Subject: [PATCH] Add NotFound and SubscriptOutOfBounds to GS64 extensions package --- .../NotFound.class.st | 81 ++++++++++++++ .../SubscriptOutOfBounds.class.st | 105 ++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 source/Buoy-Collections-GS64-Extensions/NotFound.class.st create mode 100644 source/Buoy-Collections-GS64-Extensions/SubscriptOutOfBounds.class.st diff --git a/source/Buoy-Collections-GS64-Extensions/NotFound.class.st b/source/Buoy-Collections-GS64-Extensions/NotFound.class.st new file mode 100644 index 0000000..20d0368 --- /dev/null +++ b/source/Buoy-Collections-GS64-Extensions/NotFound.class.st @@ -0,0 +1,81 @@ +" +I am NotFound, an exception to indicate that something is not found in a collection. +I am an Error and thus Exception. + +Typically, the thing not found is in my object instance variable. +The collection where this thing was not found is in my inherited signaler instance variable. + +[ NotFound signalFor: 10 in: #(1 2 3) ] on: NotFound do: [ :exception | exception ] +" +Class { + #name : #NotFound, + #superclass : #Error, + #instVars : [ + 'object' + ], + #category : #'Buoy-Collections-GS64-Extensions' +} + +{ #category : #'instance creation' } +NotFound class >> signalFor: anObject [ + "Create and signal a NotFound exception for anObject in the default receiver." + + ^ self new + object: anObject; + signal +] + +{ #category : #'instance creation' } +NotFound class >> signalFor: anObject in: aCollection [ + "Create and signal a NotFound exception for anObject in aCollection." + + ^ self new + object: anObject; + collection: aCollection; + signal +] + +{ #category : #accessing } +NotFound >> collection [ + "Return the collection where something is not found in" + + ^ self signaler +] + +{ #category : #accessing } +NotFound >> collection: aCollection [ + "Set the collection where something is not found in" + + self signaler: aCollection +] + +{ #category : #accessing } +NotFound >> messageText [ + "Overwritten to initialiaze the message text to a standard text if it has not yet been set" + + ^ messageText ifNil: [ messageText := self standardMessageText ] +] + +{ #category : #accessing } +NotFound >> object [ + "Return the object that was not found" + + ^ object +] + +{ #category : #accessing } +NotFound >> object: anObject [ + "Set the object that was not found" + + object := anObject +] + +{ #category : #private } +NotFound >> standardMessageText [ + "Generate a standard textual description" + + ^ String streamContents: [ :stream | + stream print: self object. + stream << ' not found in '. + stream print: self collection class ] +] diff --git a/source/Buoy-Collections-GS64-Extensions/SubscriptOutOfBounds.class.st b/source/Buoy-Collections-GS64-Extensions/SubscriptOutOfBounds.class.st new file mode 100644 index 0000000..1aae707 --- /dev/null +++ b/source/Buoy-Collections-GS64-Extensions/SubscriptOutOfBounds.class.st @@ -0,0 +1,105 @@ +" +I am SubscriptOutOfBounds, an exception indicating that some operation attempted to use a subscript outside allowed bounds. + +Normally, I hold the offending subscript and/or the allowed lowerBound and upperBound (inclusive). + +SubscriptOutOfBounds + signalFor: 10 + lowerBound: 1 + upperBound: 5 + in: (Array new: 5) +" +Class { + #name : #SubscriptOutOfBounds, + #superclass : #Error, + #instVars : [ + 'subscript', + 'lowerBound', + 'upperBound' + ], + #category : #'Buoy-Collections-GS64-Extensions' +} + +{ #category : #signaling } +SubscriptOutOfBounds class >> signalFor: subscript [ + ^ self + signalFor: subscript + lowerBound: nil + upperBound: nil +] + +{ #category : #signaling } +SubscriptOutOfBounds class >> signalFor: subscript lowerBound: lowerBound upperBound: upperBound [ + ^ self + signalFor: subscript + lowerBound: lowerBound + upperBound: upperBound + in: nil +] + +{ #category : #signaling } +SubscriptOutOfBounds class >> signalFor: subscript lowerBound: lowerBound upperBound: upperBound in: object [ + ^ self new + signaler: object; + subscript: subscript; + lowerBound: lowerBound; + upperBound: upperBound; + signal +] + +{ #category : #accessing } +SubscriptOutOfBounds >> lowerBound [ + ^ lowerBound +] + +{ #category : #accessing } +SubscriptOutOfBounds >> lowerBound: anObject [ + lowerBound := anObject +] + +{ #category : #accessing } +SubscriptOutOfBounds >> messageText [ + "Overwritten to initialiaze the message text to a standard text if it has not yet been set" + + ^ messageText ifNil: [ messageText := self standardMessageText ] +] + +{ #category : #printing } +SubscriptOutOfBounds >> standardMessageText [ + "Generate a standard textual description" + + ^ String streamContents: [ :stream | + self subscript + ifNil: [ stream << 'subscript' ] + ifNotNil: [ stream print: self subscript ]. + (self lowerBound notNil and: [ self upperBound notNil ]) ifTrue: [ + stream + << ' is not between '; + print: self lowerBound; + << ' and '; + print: self upperBound ]. + self signaler ifNotNil: [ + stream + nextPutAll: ' in '; + print: self signaler ] ] +] + +{ #category : #accessing } +SubscriptOutOfBounds >> subscript [ + ^ subscript +] + +{ #category : #accessing } +SubscriptOutOfBounds >> subscript: anObject [ + subscript := anObject +] + +{ #category : #accessing } +SubscriptOutOfBounds >> upperBound [ + ^ upperBound +] + +{ #category : #accessing } +SubscriptOutOfBounds >> upperBound: anObject [ + upperBound := anObject +]