Skip to content

Commit

Permalink
Add NotFound and SubscriptOutOfBounds to GS64 extensions package
Browse files Browse the repository at this point in the history
  • Loading branch information
gcotelli committed Aug 7, 2023
1 parent eabda81 commit c8f9466
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 0 deletions.
81 changes: 81 additions & 0 deletions source/Buoy-Collections-GS64-Extensions/NotFound.class.st
Original file line number Diff line number Diff line change
@@ -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 ]
]
105 changes: 105 additions & 0 deletions source/Buoy-Collections-GS64-Extensions/SubscriptOutOfBounds.class.st
Original file line number Diff line number Diff line change
@@ -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
]

0 comments on commit c8f9466

Please sign in to comment.