-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
GTFilter.class.st
182 lines (149 loc) · 4.29 KB
/
GTFilter.class.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"
I am the root class of the hierarchy of filter objects that can be used by Spotter.
The main entry point is the #value method that performs and returns the list of filtered items.
Subclasses should return the list of filtered elements in sorted order based on the filtering criteria.
"
Class {
#name : #GTFilter,
#superclass : #Object,
#instVars : [
'context',
'filteredItems',
'streamed'
],
#category : #'GT-Spotter-Filters'
}
{ #category : #public }
GTFilter class >> gtFilter [
^ self new
]
{ #category : #public }
GTFilter class >> gtListFilter [
^ self new
]
{ #category : #'private-model' }
GTFilter >> addItem: anItem [
self context streamed add: anItem.
self context addItem: anItem
]
{ #category : #'private-model' }
GTFilter >> addItems: aCollection [
self context streamed addAll: aCollection.
self context addItems: aCollection
]
{ #category : #'private-model' }
GTFilter >> allItems [
" WARNING: #allItems might be a list, block or iterator. we have to evaluate it in order to calculate the remaining/unstreamed items. #allItems are/were designed to be polymorphic to collections and streams. so this should be safe "
| allItems |
allItems := self processor allItemsIn: self context.
^ self context isContinuing
ifTrue: [ allItems value fasterDifferencePreservingOrder: self context streamed ]
ifFalse: [ allItems ]
]
{ #category : #'private-model' }
GTFilter >> allItemsDo: aBlock [
self allItems do: aBlock
]
{ #category : #private }
GTFilter >> applyFilter [
^ self filteredItems
]
{ #category : #private }
GTFilter >> applyFilterInContext: aSpotterContext [
self prepareFilterInContext: aSpotterContext.
^ self applyFilter
]
{ #category : #private }
GTFilter >> applyFilterInContext: aSpotterContext error: anException [
('[Spotter] Exception in filter <', self class name, '>: ', anException asString) logCr.
^ self defaultFilteredItems
]
{ #category : #accessing }
GTFilter >> context [
^ context
]
{ #category : #accessing }
GTFilter >> context: anObject [
context := anObject
]
{ #category : #'accessing-defaults' }
GTFilter >> defaultFilteredItems [
^ OrderedCollection new: 100 " not a limitation, just a reasonable start size "
]
{ #category : #'accessing-defaults' }
GTFilter >> defaultStreamed [
^ true
]
{ #category : #'private-model' }
GTFilter >> exceptionHandler [
self flag: 'Bloc -> overrides doesNotUnderstand discarding the current exception context'.
^ GTCurrentSpotterExceptionHandler value ifNil: [ self context exceptionHandler ]
]
{ #category : #accessing }
GTFilter >> filteredItems [
^ filteredItems
]
{ #category : #accessing }
GTFilter >> filteredItems: aCollection [
filteredItems := aCollection
]
{ #category : #public }
GTFilter >> gtFilter [
^ self
]
{ #category : #public }
GTFilter >> gtListFilter [
^ self
]
{ #category : #testing }
GTFilter >> hasFilteredItems [
^ self filteredItems isEmptyOrNil not
]
{ #category : #initialization }
GTFilter >> initialize [
super initialize.
self filteredItems: self defaultFilteredItems
]
{ #category : #'private-model' }
GTFilter >> itemFilterNameFor: anItem [
"I return the string/text representation of an item used by the filter."
^ self processor itemFilterNameFor: anItem
]
{ #category : #'private-model' }
GTFilter >> itemsLimit [
^ self context itemsLimit
]
{ #category : #private }
GTFilter >> prepareFilterInContext: aSpotterContext [
"I provide a hook for preprocessing the query once before performing a search."
self context: aSpotterContext.
]
{ #category : #'private-model' }
GTFilter >> processor [
^ self context processor
]
{ #category : #'private-model' }
GTFilter >> step [
^ self context step
]
{ #category : #accessing }
GTFilter >> streamed [
^ streamed ifNil: [ streamed := self defaultStreamed ]
]
{ #category : #accessing }
GTFilter >> streamed: anObject [
streamed := anObject
]
{ #category : #public }
GTFilter >> value: aSpotterContext [
"I provide an entry point for performing a search that is
polymorphic with BlockClosure>>value:. I return the list of
filtered items."
^ [ [ self applyFilterInContext: aSpotterContext ]
on: Error
do: [ :exception |
self exceptionHandler
handleException: exception
do: [ self applyFilterInContext: aSpotterContext error: exception ] ] ]
ensure: [ context := nil " release the context after search is completed " ]
]