gitlab.com/evatix-go/core@v1.3.55/coredata/corepayload/PayloadsCollection.go (about) 1 package corepayload 2 3 import ( 4 "fmt" 5 "math" 6 "strings" 7 "sync" 8 9 "gitlab.com/evatix-go/core/constants" 10 "gitlab.com/evatix-go/core/coredata/corejson" 11 "gitlab.com/evatix-go/core/coredata/corestr" 12 "gitlab.com/evatix-go/core/defaultcapacity" 13 "gitlab.com/evatix-go/core/errcore" 14 ) 15 16 type PayloadsCollection struct { 17 Items []*PayloadWrapper 18 } 19 20 func (it *PayloadsCollection) Add( 21 payloadWrapper PayloadWrapper, 22 ) *PayloadsCollection { 23 it.Items = append( 24 it.Items, 25 &payloadWrapper) 26 27 return it 28 } 29 30 func (it *PayloadsCollection) Adds( 31 payloadWrappers ...PayloadWrapper, 32 ) *PayloadsCollection { 33 if len(payloadWrappers) == 0 { 34 return it 35 } 36 37 for i := 0; i < len(payloadWrappers); i++ { 38 it.Items = append( 39 it.Items, 40 &payloadWrappers[i]) 41 } 42 43 return it 44 } 45 46 func (it *PayloadsCollection) AddsPtr( 47 payloadWrappers ...*PayloadWrapper, 48 ) *PayloadsCollection { 49 if len(payloadWrappers) == 0 { 50 return it 51 } 52 53 it.Items = append( 54 it.Items, 55 payloadWrappers...) 56 57 return it 58 } 59 60 func (it *PayloadsCollection) AddsPtrOptions( 61 isSkipHasIssuedPayloads bool, 62 payloadWrappers ...*PayloadWrapper, 63 ) *PayloadsCollection { 64 if len(payloadWrappers) == 0 { 65 return it 66 } 67 68 for i := 0; i < len(payloadWrappers); i++ { 69 item := payloadWrappers[i] 70 71 if isSkipHasIssuedPayloads && item.HasIssuesOrEmpty() { 72 continue 73 } 74 75 it.Items = append( 76 it.Items, 77 item) 78 } 79 80 return it 81 } 82 83 func (it *PayloadsCollection) AddsOptions( 84 isSkipHasIssuedPayloads bool, 85 payloadWrappers ...PayloadWrapper, 86 ) *PayloadsCollection { 87 if len(payloadWrappers) == 0 { 88 return it 89 } 90 91 for i := 0; i < len(payloadWrappers); i++ { 92 item := payloadWrappers[i] 93 94 if isSkipHasIssuedPayloads && item.HasIssuesOrEmpty() { 95 continue 96 } 97 98 it.Items = append( 99 it.Items, 100 &item) 101 } 102 103 return it 104 } 105 106 func (it *PayloadsCollection) ConcatNew( 107 additionalItems ...PayloadWrapper, 108 ) *PayloadsCollection { 109 cloned := it.Clone() 110 111 return cloned.Adds(additionalItems...) 112 } 113 114 func (it *PayloadsCollection) ConcatNewPtr( 115 additionalItemsPtr ...*PayloadWrapper, 116 ) *PayloadsCollection { 117 cloned := it.Clone() 118 119 return cloned.AddsPtr( 120 additionalItemsPtr...) 121 } 122 123 func (it *PayloadsCollection) AddsIf( 124 isAdd bool, 125 payloadWrappers ...PayloadWrapper, 126 ) *PayloadsCollection { 127 if !isAdd { 128 return it 129 } 130 131 return it.Adds(payloadWrappers...) 132 } 133 134 func (it *PayloadsCollection) InsertAt( 135 index int, 136 item PayloadWrapper, 137 ) *PayloadsCollection { 138 it.Items = append(it.Items[:index+1], it.Items[index:]...) 139 it.Items[index] = &item 140 141 return it 142 } 143 144 func (it *PayloadsCollection) FirstDynamic() interface{} { 145 return it.Items[0] 146 } 147 148 func (it *PayloadsCollection) First() *PayloadWrapper { 149 return it.Items[0] 150 } 151 152 func (it *PayloadsCollection) LastDynamic() interface{} { 153 return it.Items[it.LastIndex()] 154 } 155 156 func (it *PayloadsCollection) Last() *PayloadWrapper { 157 return it.Items[it.LastIndex()] 158 } 159 160 func (it *PayloadsCollection) FirstOrDefaultDynamic() interface{} { 161 return it.FirstOrDefault() 162 } 163 164 func (it *PayloadsCollection) FirstOrDefault() *PayloadWrapper { 165 if it.IsEmpty() { 166 return nil 167 } 168 169 return it.First() 170 } 171 172 func (it *PayloadsCollection) LastOrDefaultDynamic() interface{} { 173 return it.LastOrDefault() 174 } 175 176 func (it *PayloadsCollection) LastOrDefault() *PayloadWrapper { 177 if it.IsEmpty() { 178 return nil 179 } 180 181 return it.Last() 182 } 183 184 func (it *PayloadsCollection) SkipDynamic(skippingItemsCount int) interface{} { 185 return it.Items[skippingItemsCount:] 186 } 187 188 func (it *PayloadsCollection) Skip(skippingItemsCount int) []*PayloadWrapper { 189 return it.Items[skippingItemsCount:] 190 } 191 192 func (it *PayloadsCollection) SkipCollection(skippingItemsCount int) *PayloadsCollection { 193 return &PayloadsCollection{ 194 Items: it.Items[skippingItemsCount:], 195 } 196 } 197 198 func (it *PayloadsCollection) TakeDynamic(takeDynamicItems int) interface{} { 199 return it.Items[:takeDynamicItems] 200 } 201 202 func (it *PayloadsCollection) Take(takeDynamicItems int) []*PayloadWrapper { 203 return it.Items[:takeDynamicItems] 204 } 205 206 func (it *PayloadsCollection) TakeCollection(takeDynamicItems int) *PayloadsCollection { 207 return &PayloadsCollection{ 208 Items: it.Items[:takeDynamicItems], 209 } 210 } 211 212 func (it *PayloadsCollection) LimitCollection(limit int) *PayloadsCollection { 213 return &PayloadsCollection{ 214 Items: it.Items[:limit], 215 } 216 } 217 218 func (it *PayloadsCollection) SafeLimitCollection(limit int) *PayloadsCollection { 219 limit = defaultcapacity. 220 MaxLimit(it.Length(), limit) 221 222 return &PayloadsCollection{ 223 Items: it.Items[:limit], 224 } 225 } 226 227 func (it *PayloadsCollection) LimitDynamic(limit int) interface{} { 228 return it.Take(limit) 229 } 230 231 func (it *PayloadsCollection) Limit(limit int) []*PayloadWrapper { 232 return it.Take(limit) 233 } 234 235 func (it *PayloadsCollection) GetPagesSize( 236 eachPageSize int, 237 ) int { 238 length := it.Length() 239 240 pagesPossibleFloat := float64(length) / float64(eachPageSize) 241 pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat)) 242 243 return pagesPossibleCeiling 244 } 245 246 func (it *PayloadsCollection) GetPagedCollection( 247 eachPageSize int, 248 ) []*PayloadsCollection { 249 length := it.Length() 250 251 if length < eachPageSize { 252 return []*PayloadsCollection{ 253 it, 254 } 255 } 256 257 pagesPossibleFloat := float64(length) / float64(eachPageSize) 258 pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat)) 259 collectionOfCollection := make([]*PayloadsCollection, pagesPossibleCeiling) 260 261 wg := sync.WaitGroup{} 262 addPagedItemsFunc := func(oneBasedPageIndex int) { 263 pagedCollection := it.GetSinglePageCollection( 264 eachPageSize, 265 oneBasedPageIndex, 266 ) 267 268 collectionOfCollection[oneBasedPageIndex-1] = pagedCollection 269 270 wg.Done() 271 } 272 273 wg.Add(pagesPossibleCeiling) 274 for i := 1; i <= pagesPossibleCeiling; i++ { 275 go addPagedItemsFunc(i) 276 } 277 278 wg.Wait() 279 280 return collectionOfCollection 281 } 282 283 // GetSinglePageCollection PageIndex is one based index. Should be above or equal 1 284 func (it *PayloadsCollection) GetSinglePageCollection( 285 eachPageSize int, 286 pageIndex int, 287 ) *PayloadsCollection { 288 length := it.Length() 289 290 if length < eachPageSize { 291 return it 292 } 293 294 /** 295 * eachPageItems = 10 296 * pageIndex = 4 297 * skipItems = 10 * (4 - 1) = 30 298 */ 299 skipItems := eachPageSize * (pageIndex - 1) 300 if skipItems < 0 { 301 errcore. 302 CannotBeNegativeIndexType. 303 HandleUsingPanic( 304 "pageIndex cannot be negative or zero.", 305 pageIndex) 306 } 307 308 endingIndex := skipItems + eachPageSize 309 310 if endingIndex > length { 311 endingIndex = length 312 } 313 314 list := it.Items[skipItems:endingIndex] 315 316 return New. 317 PayloadsCollection. 318 UsingWrappers(list...) 319 } 320 321 func (it *PayloadsCollection) Length() int { 322 if it == nil { 323 return 0 324 } 325 326 return len(it.Items) 327 } 328 329 func (it *PayloadsCollection) Count() int { 330 return it.Length() 331 } 332 333 func (it *PayloadsCollection) IsEmpty() bool { 334 return it.Length() == 0 335 } 336 337 func (it *PayloadsCollection) HasAnyItem() bool { 338 return !it.IsEmpty() 339 } 340 341 func (it *PayloadsCollection) LastIndex() int { 342 return it.Length() - 1 343 } 344 345 func (it *PayloadsCollection) HasIndex(index int) bool { 346 return it.LastIndex() >= index 347 } 348 349 func (it *PayloadsCollection) Strings() []string { 350 list := make([]string, it.Length()) 351 352 for i, item := range it.Items { 353 list[i] = item.String() 354 } 355 356 return list 357 } 358 359 func (it *PayloadsCollection) Filter( 360 filterFunc FilterFunc, 361 ) []*PayloadWrapper { 362 list := make( 363 []*PayloadWrapper, 0, it.Length()) 364 365 for _, item := range it.Items { 366 isTake, isBreak := filterFunc(item) 367 368 if isTake { 369 list = append(list, item) 370 } 371 372 if isBreak { 373 return list 374 } 375 } 376 377 return list 378 } 379 380 func (it *PayloadsCollection) FilterWithLimit( 381 limit int, 382 filterFunc FilterFunc, 383 ) []*PayloadWrapper { 384 length := defaultcapacity.MaxLimit( 385 it.Length(), 386 limit) 387 list := make( 388 []*PayloadWrapper, 389 0, 390 length) 391 392 collectedItems := 0 393 for _, item := range it.Items { 394 isTake, isBreak := filterFunc(item) 395 396 if isTake { 397 list = append(list, item) 398 collectedItems++ 399 } 400 401 if isBreak { 402 return list 403 } 404 405 if collectedItems >= length { 406 return list 407 } 408 } 409 410 return list 411 } 412 413 func (it *PayloadsCollection) FirstByFilter( 414 findByFunc func(payloadWrapper *PayloadWrapper) (isFound bool), 415 ) *PayloadWrapper { 416 items := it.Filter(func(payloadWrapper *PayloadWrapper) (isTake, isBreak bool) { 417 isTake = findByFunc(payloadWrapper) 418 419 return isTake, isTake 420 }) 421 422 if len(items) > 0 { 423 return items[0] 424 } 425 426 return nil 427 } 428 429 func (it *PayloadsCollection) FirstById( 430 id string, 431 ) *PayloadWrapper { 432 return it.FirstByFilter(func(payloadWrapper *PayloadWrapper) (isFound bool) { 433 return payloadWrapper.IsIdentifier(id) 434 }) 435 } 436 437 func (it *PayloadsCollection) FirstByCategory( 438 category string, 439 ) *PayloadWrapper { 440 return it.FirstByFilter(func(payloadWrapper *PayloadWrapper) (isFound bool) { 441 return payloadWrapper.IsCategory(category) 442 }) 443 } 444 445 func (it *PayloadsCollection) FirstByTaskType( 446 taskType string, 447 ) *PayloadWrapper { 448 return it.FirstByFilter(func(payloadWrapper *PayloadWrapper) (isFound bool) { 449 return payloadWrapper.IsTaskTypeName(taskType) 450 }) 451 } 452 453 func (it *PayloadsCollection) FirstByEntityType( 454 entityType string, 455 ) *PayloadWrapper { 456 return it.FirstByFilter(func(payloadWrapper *PayloadWrapper) (isFound bool) { 457 return payloadWrapper.IsEntityType(entityType) 458 }) 459 } 460 461 func (it *PayloadsCollection) FilterCollection( 462 filterFunc FilterFunc, 463 ) *PayloadsCollection { 464 list := it.Filter(filterFunc) 465 466 collection := New.PayloadsCollection.UsingWrappers( 467 list...) 468 469 return collection 470 } 471 472 func (it *PayloadsCollection) SkipFilterCollection( 473 skipFilterFunc SkipFilterFunc, 474 ) *PayloadsCollection { 475 list := make( 476 []*PayloadWrapper, 477 0, 478 it.Length()) 479 480 for _, item := range it.Items { 481 isSkip, isBreak := skipFilterFunc(item) 482 483 if !isSkip { 484 list = append(list, item) 485 } 486 487 if isBreak { 488 break 489 } 490 } 491 492 return New. 493 PayloadsCollection. 494 UsingWrappers(list...) 495 } 496 497 func (it *PayloadsCollection) FilterCollectionByIds( 498 ids ...string, 499 ) *PayloadsCollection { 500 idsHashmap := corestr. 501 New. 502 Hashset. 503 Strings(ids) 504 505 return it.FilterCollection(func(payloadWrapper *PayloadWrapper) (isTake, isBreak bool) { 506 return idsHashmap.Has(payloadWrapper.Identifier), false 507 }) 508 } 509 510 func (it *PayloadsCollection) FilterNameCollection( 511 name string, 512 ) *PayloadsCollection { 513 return it.FilterCollection(func(payloadWrapper *PayloadWrapper) (isTake, isBreak bool) { 514 return payloadWrapper.Name == name, false 515 }) 516 } 517 518 func (it *PayloadsCollection) FilterCategoryCollection( 519 categoryName string, 520 ) *PayloadsCollection { 521 return it.FilterCollection(func(payloadWrapper *PayloadWrapper) (isTake, isBreak bool) { 522 return payloadWrapper.CategoryName == categoryName, false 523 }) 524 } 525 526 func (it *PayloadsCollection) FilterEntityTypeCollection( 527 entityTypeName string, 528 ) *PayloadsCollection { 529 return it.FilterCollection(func(payloadWrapper *PayloadWrapper) (isTake, isBreak bool) { 530 return payloadWrapper.EntityType == entityTypeName, false 531 }) 532 } 533 534 func (it *PayloadsCollection) FilterTaskTypeCollection( 535 taskType string, 536 ) *PayloadsCollection { 537 return it.FilterCollection(func(payloadWrapper *PayloadWrapper) (isTake, isBreak bool) { 538 return payloadWrapper.TaskTypeName == taskType, false 539 }) 540 } 541 542 func (it *PayloadsCollection) StringsUsingFmt(formatter Formatter) []string { 543 list := make([]string, it.Length()) 544 545 for i := range it.Items { 546 list[i] = formatter(it.Items[i]) 547 } 548 549 return list 550 } 551 552 func (it *PayloadsCollection) JoinUsingFmt(formatter Formatter, joiner string) string { 553 lines := it.StringsUsingFmt(formatter) 554 555 return strings.Join(lines, joiner) 556 } 557 558 func (it *PayloadsCollection) Reverse() *PayloadsCollection { 559 length := it.Length() 560 561 if length <= 1 { 562 return it 563 } 564 565 if length == 2 { 566 it.Items[0], it.Items[1] = it.Items[1], it.Items[0] 567 568 return it 569 } 570 571 mid := length / 2 572 lastIndex := length - 1 573 574 for i := 0; i < mid; i++ { 575 it.Items[i], it.Items[lastIndex-i] = 576 it.Items[lastIndex-i], it.Items[i] 577 } 578 579 return it 580 } 581 582 func (it *PayloadsCollection) JsonStrings() []string { 583 list := make([]string, it.Length()) 584 585 for i, item := range it.Items { 586 list[i] = item.JsonString() 587 } 588 589 return list 590 } 591 592 func (it *PayloadsCollection) JoinJsonStrings(joiner string) string { 593 return strings.Join(it.JsonStrings(), joiner) 594 } 595 596 func (it *PayloadsCollection) Join(joiner string) string { 597 return strings.Join(it.Strings(), joiner) 598 } 599 600 func (it *PayloadsCollection) JoinCsv() string { 601 return strings.Join(it.CsvStrings(), constants.Comma) 602 } 603 604 func (it *PayloadsCollection) JoinCsvLine() string { 605 return strings.Join(it.CsvStrings(), constants.CommaUnixNewLine) 606 } 607 608 func (it *PayloadsCollection) IsEqual(another *PayloadsCollection) bool { 609 if it == nil && another == nil { 610 return true 611 } 612 613 if it == nil || another == nil { 614 return false 615 } 616 617 if it.Length() != another.Length() { 618 return false 619 } 620 621 return it.IsEqualItems(another.Items...) 622 } 623 624 func (it *PayloadsCollection) IsEqualItems(lines ...*PayloadWrapper) bool { 625 if it == nil && lines == nil { 626 return true 627 } 628 629 if it == nil || lines == nil { 630 return false 631 } 632 633 if it.Length() != len(lines) { 634 return false 635 } 636 637 for i, item := range it.Items { 638 anotherItem := lines[i] 639 640 if !item.IsEqual(anotherItem) { 641 return false 642 } 643 } 644 645 return true 646 } 647 648 func (it *PayloadsCollection) JsonString() string { 649 if it.IsEmpty() { 650 return constants.EmptyString 651 } 652 653 return it.JsonPtr().JsonString() 654 } 655 656 func (it *PayloadsCollection) String() string { 657 if it.IsEmpty() { 658 return constants.EmptyString 659 } 660 661 return it.JsonPtr().JsonString() 662 } 663 664 func (it *PayloadsCollection) PrettyJsonString() string { 665 if it.IsEmpty() { 666 return constants.EmptyString 667 } 668 669 return it.JsonPtr().PrettyJsonString() 670 } 671 672 func (it *PayloadsCollection) CsvStrings() []string { 673 if it.IsEmpty() { 674 return []string{} 675 } 676 677 newSlice := make([]string, it.Length()) 678 679 for i, item := range it.Items { 680 newSlice[i] = fmt.Sprintf( 681 constants.SprintDoubleQuoteFormat, 682 item.String()) 683 } 684 685 return newSlice 686 } 687 688 func (it PayloadsCollection) Json() corejson.Result { 689 return corejson.New(it) 690 } 691 692 func (it PayloadsCollection) JsonPtr() *corejson.Result { 693 return corejson.NewPtr(it) 694 } 695 696 func (it *PayloadsCollection) ParseInjectUsingJson( 697 jsonResult *corejson.Result, 698 ) (*PayloadsCollection, error) { 699 err := jsonResult.Unmarshal(it) 700 701 if err != nil { 702 return Empty.PayloadsCollection(), err 703 } 704 705 return it, nil 706 } 707 708 // ParseInjectUsingJsonMust Panic if error 709 func (it *PayloadsCollection) ParseInjectUsingJsonMust( 710 jsonResult *corejson.Result, 711 ) *PayloadsCollection { 712 hashSet, err := it. 713 ParseInjectUsingJson(jsonResult) 714 715 if err != nil { 716 panic(err) 717 } 718 719 return hashSet 720 } 721 722 func (it *PayloadsCollection) AsJsonContractsBinder() corejson.JsonContractsBinder { 723 return it 724 } 725 726 func (it *PayloadsCollection) AsJsoner() corejson.Jsoner { 727 return it 728 } 729 730 func (it *PayloadsCollection) JsonParseSelfInject( 731 jsonResult *corejson.Result, 732 ) error { 733 _, err := it.ParseInjectUsingJson( 734 jsonResult, 735 ) 736 737 return err 738 } 739 740 func (it *PayloadsCollection) AsJsonParseSelfInjector() corejson.JsonParseSelfInjector { 741 return it 742 } 743 744 func (it *PayloadsCollection) Clear() *PayloadsCollection { 745 if it == nil { 746 return it 747 } 748 749 tempItems := it.Items 750 clearFunc := func() { 751 for _, item := range tempItems { 752 item.Dispose() 753 } 754 } 755 756 go clearFunc() 757 758 it.Items = []*PayloadWrapper{} 759 760 return it 761 } 762 763 func (it *PayloadsCollection) Dispose() { 764 if it == nil { 765 return 766 } 767 768 it.Clear() 769 it.Items = nil 770 } 771 772 func (it PayloadsCollection) Clone() PayloadsCollection { 773 list := New.PayloadsCollection.UsingCap(it.Length()) 774 775 return *list.AddsPtr(it.Items...) 776 } 777 778 func (it *PayloadsCollection) ClonePtr() *PayloadsCollection { 779 if it == nil { 780 return nil 781 } 782 783 list := New.PayloadsCollection.UsingCap(it.Length()) 784 785 return list.AddsPtr(it.Items...) 786 }