-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NotFound and SubscriptOutOfBounds to GS64 extensions package
- Loading branch information
Showing
2 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
105
source/Buoy-Collections-GS64-Extensions/SubscriptOutOfBounds.class.st
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] |