-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_uint16_iter.go
1394 lines (1196 loc) · 37.9 KB
/
gen_uint16_iter.go
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package iter
import "github.com/pkg/errors"
var _ = errors.New("") // hack to get import // TODO clearly
// Uint16Iterator is an iterator over items type of uint16.
type Uint16Iterator interface {
// HasNext checks if there is the next item
// in the iterator. HasNext should be idempotent.
HasNext() bool
// Next should return next item in the iterator.
// It should be invoked after check HasNext.
Next() uint16
// Err contains first met error while Next.
Err() error
}
type emptyUint16Iterator struct{}
func (emptyUint16Iterator) HasNext() bool { return false }
func (emptyUint16Iterator) Next() (next uint16) { return 0 }
func (emptyUint16Iterator) Err() error { return nil }
// EmptyUint16Iterator is a zero value for Uint16Iterator.
// It is not contains any item to iterate over it.
var EmptyUint16Iterator Uint16Iterator = emptyUint16Iterator{}
// Uint16IterMaker is a maker of Uint16Iterator.
type Uint16IterMaker interface {
// MakeIter should return a new instance of Uint16Iterator to iterate over it.
MakeIter() Uint16Iterator
}
// MakeUint16Iter is a shortcut implementation
// of Uint16Iterator based on a function.
type MakeUint16Iter func() Uint16Iterator
// MakeIter returns a new instance of Uint16Iterator to iterate over it.
func (m MakeUint16Iter) MakeIter() Uint16Iterator { return m() }
// MakeNoUint16Iter is a zero value for Uint16IterMaker.
// It always returns EmptyUint16Iterator and an empty error.
var MakeNoUint16Iter Uint16IterMaker = MakeUint16Iter(
func() Uint16Iterator { return EmptyUint16Iterator })
// Uint16Discard just range over all items and do nothing with each of them.
func Uint16Discard(items Uint16Iterator) error {
if items == nil {
items = EmptyUint16Iterator
}
for items.HasNext() {
_ = items.Next()
}
// no error wrapping since no additional context for the error; just return it.
return items.Err()
}
// Uint16Checker is an object checking an item type of uint16
// for some condition.
type Uint16Checker interface {
// Check should check an item type of uint16 for some condition.
// It is suggested to return EndOfUint16Iterator to stop iteration.
Check(uint16) (bool, error)
}
// Uint16Check is a shortcut implementation
// of Uint16Checker based on a function.
type Uint16Check func(uint16) (bool, error)
// Check checks an item type of uint16 for some condition.
// It returns EndOfUint16Iterator to stop iteration.
func (ch Uint16Check) Check(item uint16) (bool, error) { return ch(item) }
var (
// AlwaysUint16CheckTrue always returns true and empty error.
AlwaysUint16CheckTrue Uint16Checker = Uint16Check(
func(item uint16) (bool, error) { return true, nil })
// AlwaysUint16CheckFalse always returns false and empty error.
AlwaysUint16CheckFalse Uint16Checker = Uint16Check(
func(item uint16) (bool, error) { return false, nil })
)
// NotUint16 do an inversion for checker result.
// It is returns AlwaysUint16CheckTrue if checker is nil.
func NotUint16(checker Uint16Checker) Uint16Checker {
if checker == nil {
return AlwaysUint16CheckTrue
}
return Uint16Check(func(item uint16) (bool, error) {
yes, err := checker.Check(item)
if err != nil {
// No error wrapping since an error context is missing.
return false, err
}
return !yes, nil
})
}
type andUint16 struct {
lhs, rhs Uint16Checker
}
func (a andUint16) Check(item uint16) (bool, error) {
isLHSPassed, err := a.lhs.Check(item)
if err != nil {
return false, wrapIfNotEndOfUint16Iterator(err, "lhs check")
}
if !isLHSPassed {
return false, nil
}
isRHSPassed, err := a.rhs.Check(item)
if err != nil {
return false, wrapIfNotEndOfUint16Iterator(err, "rhs check")
}
return isRHSPassed, nil
}
// AllUint16 combines all the given checkers to one
// checking if all checkers return true.
// It returns true checker if the list of checkers is empty.
func AllUint16(checkers ...Uint16Checker) Uint16Checker {
var all = AlwaysUint16CheckTrue
for i := len(checkers) - 1; i >= 0; i-- {
if checkers[i] == nil {
continue
}
all = andUint16{checkers[i], all}
}
return all
}
type orUint16 struct {
lhs, rhs Uint16Checker
}
func (o orUint16) Check(item uint16) (bool, error) {
isLHSPassed, err := o.lhs.Check(item)
if err != nil {
return false, wrapIfNotEndOfUint16Iterator(err, "lhs check")
}
if isLHSPassed {
return true, nil
}
isRHSPassed, err := o.rhs.Check(item)
if err != nil {
return false, wrapIfNotEndOfUint16Iterator(err, "rhs check")
}
return isRHSPassed, nil
}
// AnyUint16 combines all the given checkers to one.
// checking if any checker return true.
// It returns false if the list of checkers is empty.
func AnyUint16(checkers ...Uint16Checker) Uint16Checker {
var any = AlwaysUint16CheckFalse
for i := len(checkers) - 1; i >= 0; i-- {
if checkers[i] == nil {
continue
}
any = orUint16{checkers[i], any}
}
return any
}
// FilteringUint16Iterator does iteration with
// filtering by previously set checker.
type FilteringUint16Iterator struct {
preparedUint16Item
filter Uint16Checker
}
// HasNext checks if there is the next item
// in the iterator. HasNext is idempotent.
func (it *FilteringUint16Iterator) HasNext() bool {
if it.hasNext {
return true
}
for it.preparedUint16Item.HasNext() {
next := it.base.Next()
isFilterPassed, err := it.filter.Check(next)
if err != nil {
if !isEndOfUint16Iterator(err) {
err = errors.Wrap(err, "filtering iterator: check")
}
it.err = err
return false
}
if !isFilterPassed {
continue
}
it.hasNext = true
it.next = next
return true
}
return false
}
// Uint16Filtering sets filter while iterating over items.
// If filters is empty, so all items will return.
func Uint16Filtering(items Uint16Iterator, filters ...Uint16Checker) Uint16Iterator {
if items == nil {
return EmptyUint16Iterator
}
return &FilteringUint16Iterator{preparedUint16Item{base: items}, AllUint16(filters...)}
}
// DoingUntilUint16Iterator does iteration
// until previously set checker is passed.
type DoingUntilUint16Iterator struct {
preparedUint16Item
until Uint16Checker
}
// HasNext checks if there is the next item
// in the iterator. HasNext is idempotent.
func (it *DoingUntilUint16Iterator) HasNext() bool {
if it.hasNext {
return true
}
for it.preparedUint16Item.HasNext() {
next := it.base.Next()
isUntilPassed, err := it.until.Check(next)
if err != nil {
if !isEndOfUint16Iterator(err) {
err = errors.Wrap(err, "doing until iterator: until")
}
it.err = err
return false
}
if isUntilPassed {
it.err = EndOfUint16Iterator
}
it.hasNext = true
it.next = next
return true
}
return false
}
// Uint16DoingUntil sets until checker while iterating over items.
// If untilList is empty, so all items returned as is.
func Uint16DoingUntil(items Uint16Iterator, untilList ...Uint16Checker) Uint16Iterator {
if items == nil {
return EmptyUint16Iterator
}
var until Uint16Checker
if len(untilList) > 0 {
until = AllUint16(untilList...)
} else {
until = AlwaysUint16CheckFalse
}
return &DoingUntilUint16Iterator{preparedUint16Item{base: items}, until}
}
// Uint16SkipUntil sets until conditions to skip few items.
func Uint16SkipUntil(items Uint16Iterator, untilList ...Uint16Checker) error {
// no error wrapping since no additional context for the error; just return it.
return Uint16Discard(Uint16DoingUntil(items, untilList...))
}
// Uint16EnumChecker is an object checking an item type of uint16
// and its ordering number in for some condition.
type Uint16EnumChecker interface {
// Check checks an item type of uint16 and its ordering number for some condition.
// It is suggested to return EndOfUint16Iterator to stop iteration.
Check(int, uint16) (bool, error)
}
// Uint16EnumCheck is a shortcut implementation
// of Uint16EnumChecker based on a function.
type Uint16EnumCheck func(int, uint16) (bool, error)
// Check checks an item type of uint16 and its ordering number for some condition.
// It returns EndOfUint16Iterator to stop iteration.
func (ch Uint16EnumCheck) Check(n int, item uint16) (bool, error) { return ch(n, item) }
type enumFromUint16Checker struct {
Uint16Checker
}
func (ch enumFromUint16Checker) Check(_ int, item uint16) (bool, error) {
return ch.Uint16Checker.Check(item)
}
// EnumFromUint16Checker adapts checker type of Uint16Checker
// to the interface Uint16EnumChecker.
// If checker is nil it is return based on AlwaysUint16CheckFalse enum checker.
func EnumFromUint16Checker(checker Uint16Checker) Uint16EnumChecker {
if checker == nil {
checker = AlwaysUint16CheckFalse
}
return &enumFromUint16Checker{checker}
}
var (
// AlwaysUint16EnumCheckTrue always returns true and empty error.
AlwaysUint16EnumCheckTrue = EnumFromUint16Checker(
AlwaysUint16CheckTrue)
// AlwaysUint16EnumCheckFalse always returns false and empty error.
AlwaysUint16EnumCheckFalse = EnumFromUint16Checker(
AlwaysUint16CheckFalse)
)
// EnumNotUint16 do an inversion for checker result.
// It is returns AlwaysUint16EnumCheckTrue if checker is nil.
func EnumNotUint16(checker Uint16EnumChecker) Uint16EnumChecker {
if checker == nil {
return AlwaysUint16EnumCheckTrue
}
return Uint16EnumCheck(func(n int, item uint16) (bool, error) {
yes, err := checker.Check(n, item)
if err != nil {
// No error wrapping since an error context is missing.
return false, err
}
return !yes, nil
})
}
type enumAndUint16 struct {
lhs, rhs Uint16EnumChecker
}
func (a enumAndUint16) Check(n int, item uint16) (bool, error) {
isLHSPassed, err := a.lhs.Check(n, item)
if err != nil {
return false, wrapIfNotEndOfUint16Iterator(err, "lhs check")
}
if !isLHSPassed {
return false, nil
}
isRHSPassed, err := a.rhs.Check(n, item)
if err != nil {
return false, wrapIfNotEndOfUint16Iterator(err, "rhs check")
}
return isRHSPassed, nil
}
// EnumAllUint16 combines all the given checkers to one
// checking if all checkers return true.
// It returns true if the list of checkers is empty.
func EnumAllUint16(checkers ...Uint16EnumChecker) Uint16EnumChecker {
var all = AlwaysUint16EnumCheckTrue
for i := len(checkers) - 1; i >= 0; i-- {
if checkers[i] == nil {
continue
}
all = enumAndUint16{checkers[i], all}
}
return all
}
type enumOrUint16 struct {
lhs, rhs Uint16EnumChecker
}
func (o enumOrUint16) Check(n int, item uint16) (bool, error) {
isLHSPassed, err := o.lhs.Check(n, item)
if err != nil {
return false, wrapIfNotEndOfUint16Iterator(err, "lhs check")
}
if isLHSPassed {
return true, nil
}
isRHSPassed, err := o.rhs.Check(n, item)
if err != nil {
return false, wrapIfNotEndOfUint16Iterator(err, "rhs check")
}
return isRHSPassed, nil
}
// EnumAnyUint16 combines all the given checkers to one.
// checking if any checker return true.
// It returns false if the list of checkers is empty.
func EnumAnyUint16(checkers ...Uint16EnumChecker) Uint16EnumChecker {
var any = AlwaysUint16EnumCheckFalse
for i := len(checkers) - 1; i >= 0; i-- {
if checkers[i] == nil {
continue
}
any = enumOrUint16{checkers[i], any}
}
return any
}
// EnumFilteringUint16Iterator does iteration with
// filtering by previously set checker.
type EnumFilteringUint16Iterator struct {
preparedUint16Item
filter Uint16EnumChecker
count int
}
// HasNext checks if there is the next item
// in the iterator. HasNext is idempotent.
func (it *EnumFilteringUint16Iterator) HasNext() bool {
if it.hasNext {
return true
}
for it.preparedUint16Item.HasNext() {
next := it.base.Next()
isFilterPassed, err := it.filter.Check(it.count, next)
if err != nil {
if !isEndOfUint16Iterator(err) {
err = errors.Wrap(err, "enum filtering iterator: check")
}
it.err = err
return false
}
it.count++
if !isFilterPassed {
continue
}
it.hasNext = true
it.next = next
return true
}
return false
}
// Uint16EnumFiltering sets filter while iterating over items and their serial numbers.
// If filters is empty, so all items will return.
func Uint16EnumFiltering(items Uint16Iterator, filters ...Uint16EnumChecker) Uint16Iterator {
if items == nil {
return EmptyUint16Iterator
}
return &EnumFilteringUint16Iterator{preparedUint16Item{base: items}, EnumAllUint16(filters...), 0}
}
// EnumDoingUntilUint16Iterator does iteration
// until previously set checker is passed.
type EnumDoingUntilUint16Iterator struct {
preparedUint16Item
until Uint16EnumChecker
count int
}
// HasNext checks if there is the next item
// in the iterator. HasNext is idempotent.
func (it *EnumDoingUntilUint16Iterator) HasNext() bool {
if it.hasNext {
return true
}
for it.preparedUint16Item.HasNext() {
next := it.base.Next()
isUntilPassed, err := it.until.Check(it.count, next)
if err != nil {
if !isEndOfUint16Iterator(err) {
err = errors.Wrap(err, "doing until iterator: until")
}
it.err = err
return false
}
it.count++
if isUntilPassed {
it.err = EndOfUint16Iterator
}
it.hasNext = true
it.next = next
return true
}
return false
}
// Uint16EnumDoingUntil sets until checker while iterating over items.
// If untilList is empty, so all items returned as is.
func Uint16EnumDoingUntil(items Uint16Iterator, untilList ...Uint16EnumChecker) Uint16Iterator {
if items == nil {
return EmptyUint16Iterator
}
var until Uint16EnumChecker
if len(untilList) > 0 {
until = EnumAllUint16(untilList...)
} else {
until = AlwaysUint16EnumCheckFalse
}
return &EnumDoingUntilUint16Iterator{preparedUint16Item{base: items}, until, 0}
}
// Uint16EnumSkipUntil sets until conditions to skip few items.
func Uint16EnumSkipUntil(items Uint16Iterator, untilList ...Uint16EnumChecker) error {
// no error wrapping since no additional context for the error; just return it.
return Uint16Discard(Uint16EnumDoingUntil(items, untilList...))
}
// Uint16GettingBatch returns the next batch from items.
func Uint16GettingBatch(items Uint16Iterator, batchSize int) Uint16Iterator {
if items == nil {
return EmptyUint16Iterator
}
if batchSize == 0 {
return items
}
return Uint16EnumDoingUntil(items, Uint16EnumCheck(func(n int, item uint16) (bool, error) {
return n == batchSize-1, nil
}))
}
// Uint16Converter is an object converting an item type of uint16.
type Uint16Converter interface {
// Convert should convert an item type of uint16 into another item of uint16.
// It is suggested to return EndOfUint16Iterator to stop iteration.
Convert(uint16) (uint16, error)
}
// Uint16Convert is a shortcut implementation
// of Uint16Converter based on a function.
type Uint16Convert func(uint16) (uint16, error)
// Convert converts an item type of uint16 into another item of uint16.
// It is suggested to return EndOfUint16Iterator to stop iteration.
func (c Uint16Convert) Convert(item uint16) (uint16, error) { return c(item) }
// NoUint16Convert does nothing with item, just returns it as is.
var NoUint16Convert Uint16Converter = Uint16Convert(
func(item uint16) (uint16, error) { return item, nil })
type doubleUint16Converter struct {
lhs, rhs Uint16Converter
}
func (c doubleUint16Converter) Convert(item uint16) (uint16, error) {
item, err := c.lhs.Convert(item)
if err != nil {
return 0, errors.Wrap(err, "convert lhs")
}
item, err = c.rhs.Convert(item)
if err != nil {
return 0, errors.Wrap(err, "convert rhs")
}
return item, nil
}
// Uint16ConverterSeries combines all the given converters to sequenced one
// It returns no converter if the list of converters is empty.
func Uint16ConverterSeries(converters ...Uint16Converter) Uint16Converter {
var series = NoUint16Convert
for i := len(converters) - 1; i >= 0; i-- {
if converters[i] == nil {
continue
}
series = doubleUint16Converter{lhs: converters[i], rhs: series}
}
return series
}
// ConvertingUint16Iterator does iteration with
// converting by previously set converter.
type ConvertingUint16Iterator struct {
preparedUint16Item
converter Uint16Converter
}
// HasNext checks if there is the next item
// in the iterator. HasNext is idempotent.
func (it *ConvertingUint16Iterator) HasNext() bool {
if it.hasNext {
return true
}
if it.preparedUint16Item.HasNext() {
next := it.base.Next()
next, err := it.converter.Convert(next)
if err != nil {
if !isEndOfUint16Iterator(err) {
err = errors.Wrap(err, "converting iterator: check")
}
it.err = err
return false
}
it.hasNext = true
it.next = next
return true
}
return false
}
// Uint16Converting sets converter while iterating over items.
// If converters is empty, so all items will not be affected.
func Uint16Converting(items Uint16Iterator, converters ...Uint16Converter) Uint16Iterator {
if items == nil {
return EmptyUint16Iterator
}
return &ConvertingUint16Iterator{
preparedUint16Item{base: items}, Uint16ConverterSeries(converters...)}
}
// Uint16EnumConverter is an object converting an item type of uint16 and its ordering number.
type Uint16EnumConverter interface {
// Convert should convert an item type of uint16 into another item of uint16.
// It is suggested to return EndOfUint16Iterator to stop iteration.
Convert(n int, val uint16) (uint16, error)
}
// Uint16EnumConvert is a shortcut implementation
// of Uint16EnumConverter based on a function.
type Uint16EnumConvert func(int, uint16) (uint16, error)
// Convert converts an item type of uint16 into another item of uint16.
// It is suggested to return EndOfUint16Iterator to stop iteration.
func (c Uint16EnumConvert) Convert(n int, item uint16) (uint16, error) { return c(n, item) }
// NoUint16EnumConvert does nothing with item, just returns it as is.
var NoUint16EnumConvert Uint16EnumConverter = Uint16EnumConvert(
func(_ int, item uint16) (uint16, error) { return item, nil })
type enumFromUint16Converter struct {
Uint16Converter
}
func (ch enumFromUint16Converter) Convert(_ int, item uint16) (uint16, error) {
return ch.Uint16Converter.Convert(item)
}
// EnumFromUint16Converter adapts checker type of Uint16Converter
// to the interface Uint16EnumConverter.
// If converter is nil it is return based on NoUint16Convert enum checker.
func EnumFromUint16Converter(converter Uint16Converter) Uint16EnumConverter {
if converter == nil {
converter = NoUint16Convert
}
return &enumFromUint16Converter{converter}
}
type doubleUint16EnumConverter struct {
lhs, rhs Uint16EnumConverter
}
func (c doubleUint16EnumConverter) Convert(n int, item uint16) (uint16, error) {
item, err := c.lhs.Convert(n, item)
if err != nil {
return 0, errors.Wrap(err, "convert lhs")
}
item, err = c.rhs.Convert(n, item)
if err != nil {
return 0, errors.Wrap(err, "convert rhs")
}
return item, nil
}
// EnumUint16ConverterSeries combines all the given converters to sequenced one
// It returns no converter if the list of converters is empty.
func EnumUint16ConverterSeries(converters ...Uint16EnumConverter) Uint16EnumConverter {
var series = NoUint16EnumConvert
for i := len(converters) - 1; i >= 0; i-- {
if converters[i] == nil {
continue
}
series = doubleUint16EnumConverter{lhs: converters[i], rhs: series}
}
return series
}
// EnumConvertingUint16Iterator does iteration with
// converting by previously set converter.
type EnumConvertingUint16Iterator struct {
preparedUint16Item
converter Uint16EnumConverter
count int
}
// HasNext checks if there is the next item
// in the iterator. HasNext is idempotent.
func (it *EnumConvertingUint16Iterator) HasNext() bool {
if it.hasNext {
return true
}
if it.preparedUint16Item.HasNext() {
next := it.base.Next()
next, err := it.converter.Convert(it.count, next)
if err != nil {
if !isEndOfUint16Iterator(err) {
err = errors.Wrap(err, "converting iterator: check")
}
it.err = err
return false
}
it.count++
it.hasNext = true
it.next = next
return true
}
return false
}
// Uint16EnumConverting sets converter while iterating over items and their ordering numbers.
// If converters is empty, so all items will not be affected.
func Uint16EnumConverting(items Uint16Iterator, converters ...Uint16EnumConverter) Uint16Iterator {
if items == nil {
return EmptyUint16Iterator
}
return &EnumConvertingUint16Iterator{
preparedUint16Item{base: items}, EnumUint16ConverterSeries(converters...), 0}
}
// Uint16Handler is an object handling an item type of uint16.
type Uint16Handler interface {
// Handle should do something with item of uint16.
// It is suggested to return EndOfUint16Iterator to stop iteration.
Handle(uint16) error
}
// Uint16Handle is a shortcut implementation
// of Uint16Handler based on a function.
type Uint16Handle func(uint16) error
// Handle does something with item of uint16.
// It is suggested to return EndOfUint16Iterator to stop iteration.
func (h Uint16Handle) Handle(item uint16) error { return h(item) }
// Uint16DoNothing does nothing.
var Uint16DoNothing Uint16Handler = Uint16Handle(func(_ uint16) error { return nil })
type doubleUint16Handler struct {
lhs, rhs Uint16Handler
}
func (h doubleUint16Handler) Handle(item uint16) error {
err := h.lhs.Handle(item)
if err != nil {
return errors.Wrap(err, "handle lhs")
}
err = h.rhs.Handle(item)
if err != nil {
return errors.Wrap(err, "handle rhs")
}
return nil
}
// Uint16HandlerSeries combines all the given handlers to sequenced one
// It returns do nothing handler if the list of handlers is empty.
func Uint16HandlerSeries(handlers ...Uint16Handler) Uint16Handler {
var series = Uint16DoNothing
for i := len(handlers) - 1; i >= 0; i-- {
if handlers[i] == nil {
continue
}
series = doubleUint16Handler{lhs: handlers[i], rhs: series}
}
return series
}
// HandlingUint16Iterator does iteration with
// handling by previously set handler.
type HandlingUint16Iterator struct {
preparedUint16Item
handler Uint16Handler
}
// HasNext checks if there is the next item
// in the iterator. HasNext is idempotent.
func (it *HandlingUint16Iterator) HasNext() bool {
if it.hasNext {
return true
}
if it.preparedUint16Item.HasNext() {
next := it.base.Next()
err := it.handler.Handle(next)
if err != nil {
if !isEndOfUint16Iterator(err) {
err = errors.Wrap(err, "handling iterator: check")
}
it.err = err
return false
}
it.hasNext = true
it.next = next
return true
}
return false
}
// Uint16Handling sets handler while iterating over items.
// If handlers is empty, so it will do nothing.
func Uint16Handling(items Uint16Iterator, handlers ...Uint16Handler) Uint16Iterator {
if items == nil {
return EmptyUint16Iterator
}
return &HandlingUint16Iterator{
preparedUint16Item{base: items}, Uint16HandlerSeries(handlers...)}
}
// Uint16Range iterates over items and use handlers to each one.
func Uint16Range(items Uint16Iterator, handlers ...Uint16Handler) error {
return Uint16Discard(Uint16Handling(items, handlers...))
}
// Uint16RangeIterator is an iterator over items.
type Uint16RangeIterator interface {
// Range should iterate over items.
Range(...Uint16Handler) error
}
type sUint16RangeIterator struct {
iter Uint16Iterator
}
// ToUint16RangeIterator constructs an instance implementing Uint16RangeIterator
// based on Uint16Iterator.
func ToUint16RangeIterator(iter Uint16Iterator) Uint16RangeIterator {
if iter == nil {
iter = EmptyUint16Iterator
}
return sUint16RangeIterator{iter: iter}
}
// MakeUint16RangeIterator constructs an instance implementing Uint16RangeIterator
// based on Uint16IterMaker.
func MakeUint16RangeIterator(maker Uint16IterMaker) Uint16RangeIterator {
if maker == nil {
maker = MakeNoUint16Iter
}
return ToUint16RangeIterator(maker.MakeIter())
}
// Range iterates over items.
func (r sUint16RangeIterator) Range(handlers ...Uint16Handler) error {
return Uint16Range(r.iter, handlers...)
}
// Uint16EnumHandler is an object handling an item type of uint16 and its ordered number.
type Uint16EnumHandler interface {
// Handle should do something with item of uint16 and its ordered number.
// It is suggested to return EndOfUint16Iterator to stop iteration.
Handle(int, uint16) error
}
// Uint16EnumHandle is a shortcut implementation
// of Uint16EnumHandler based on a function.
type Uint16EnumHandle func(int, uint16) error
// Handle does something with item of uint16 and its ordered number.
// It is suggested to return EndOfUint16Iterator to stop iteration.
func (h Uint16EnumHandle) Handle(n int, item uint16) error { return h(n, item) }
// Uint16DoEnumNothing does nothing.
var Uint16DoEnumNothing = Uint16EnumHandle(func(_ int, _ uint16) error { return nil })
type doubleUint16EnumHandler struct {
lhs, rhs Uint16EnumHandler
}
func (h doubleUint16EnumHandler) Handle(n int, item uint16) error {
err := h.lhs.Handle(n, item)
if err != nil {
return errors.Wrap(err, "handle lhs")
}
err = h.rhs.Handle(n, item)
if err != nil {
return errors.Wrap(err, "handle rhs")
}
return nil
}
// Uint16EnumHandlerSeries combines all the given handlers to sequenced one
// It returns do nothing handler if the list of handlers is empty.
func Uint16EnumHandlerSeries(handlers ...Uint16EnumHandler) Uint16EnumHandler {
var series Uint16EnumHandler = Uint16DoEnumNothing
for i := len(handlers) - 1; i >= 0; i-- {
if handlers[i] == nil {
continue
}
series = doubleUint16EnumHandler{lhs: handlers[i], rhs: series}
}
return series
}
// EnumHandlingUint16Iterator does iteration with
// handling by previously set handler.
type EnumHandlingUint16Iterator struct {
preparedUint16Item
handler Uint16EnumHandler
count int
}
// HasNext checks if there is the next item
// in the iterator. HasNext is idempotent.
func (it *EnumHandlingUint16Iterator) HasNext() bool {
if it.hasNext {
return true
}
if it.preparedUint16Item.HasNext() {
next := it.base.Next()
err := it.handler.Handle(it.count, next)
if err != nil {
if !isEndOfUint16Iterator(err) {
err = errors.Wrap(err, "enum handling iterator: check")
}
it.err = err
return false
}
it.count++
it.hasNext = true
it.next = next
return true
}
return false
}
// Uint16EnumHandling sets handler while iterating over items with their serial number.
// If handlers is empty, so it will do nothing.
func Uint16EnumHandling(items Uint16Iterator, handlers ...Uint16EnumHandler) Uint16Iterator {
if items == nil {
return EmptyUint16Iterator
}
return &EnumHandlingUint16Iterator{
preparedUint16Item{base: items}, Uint16EnumHandlerSeries(handlers...), 0}
}
// Uint16Enum iterates over items and their ordering numbers and use handlers to each one.
func Uint16Enum(items Uint16Iterator, handlers ...Uint16EnumHandler) error {
return Uint16Discard(Uint16EnumHandling(items, handlers...))
}
// Uint16EnumIterator is an iterator over items and their ordering numbers.
type Uint16EnumIterator interface {
// Enum should iterate over items and their ordering numbers.
Enum(...Uint16EnumHandler) error
}
type sUint16EnumIterator struct {
iter Uint16Iterator
}
// ToUint16EnumIterator constructs an instance implementing Uint16EnumIterator
// based on Uint16Iterator.
func ToUint16EnumIterator(iter Uint16Iterator) Uint16EnumIterator {
if iter == nil {
iter = EmptyUint16Iterator
}
return sUint16EnumIterator{iter: iter}
}
// MakeUint16EnumIterator constructs an instance implementing Uint16EnumIterator
// based on Uint16IterMaker.
func MakeUint16EnumIterator(maker Uint16IterMaker) Uint16EnumIterator {
if maker == nil {
maker = MakeNoUint16Iter
}
return ToUint16EnumIterator(maker.MakeIter())
}
// Enum iterates over items and their ordering numbers.
func (r sUint16EnumIterator) Enum(handlers ...Uint16EnumHandler) error {
return Uint16Enum(r.iter, handlers...)
}
// Range iterates over items.
func (r sUint16EnumIterator) Range(handlers ...Uint16Handler) error {
return Uint16Range(r.iter, handlers...)
}
type doubleUint16Iterator struct {
lhs, rhs Uint16Iterator
inRHS bool
}
func (it *doubleUint16Iterator) HasNext() bool {
if !it.inRHS {
if it.lhs.HasNext() {
return true
}
it.inRHS = true
}
return it.rhs.HasNext()
}
func (it *doubleUint16Iterator) Next() uint16 {
if !it.inRHS {
return it.lhs.Next()
}