gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/Collection.go (about) 1 package corestr 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "math" 8 "sort" 9 "strings" 10 "sync" 11 12 "gitlab.com/evatix-go/core/constants" 13 "gitlab.com/evatix-go/core/coredata/corejson" 14 "gitlab.com/evatix-go/core/coredata/stringslice" 15 "gitlab.com/evatix-go/core/coreindexes" 16 "gitlab.com/evatix-go/core/defaultcapacity" 17 "gitlab.com/evatix-go/core/errcore" 18 "gitlab.com/evatix-go/core/internal/strutilinternal" 19 "gitlab.com/evatix-go/core/simplewrap" 20 ) 21 22 type Collection struct { 23 items []string 24 sync.Mutex 25 } 26 27 func (it *Collection) JsonString() string { 28 return it.JsonPtr().JsonString() 29 } 30 31 func (it *Collection) JsonStringMust() string { 32 return it.JsonPtr().JsonString() 33 } 34 35 func (it *Collection) HasAnyItem() bool { 36 return it.Length() > 0 37 } 38 39 func (it *Collection) LastIndex() int { 40 return it.Length() - 1 41 } 42 43 func (it *Collection) HasIndex(index int) bool { 44 return it.LastIndex() >= index 45 } 46 47 func (it *Collection) ListStringsPtr() []string { 48 return it.items 49 } 50 51 func (it *Collection) ListStrings() []string { 52 return it.items 53 } 54 55 func (it *Collection) StringJSON() string { 56 return it.JsonPtr().JsonString() 57 } 58 59 func (it *Collection) RemoveAt(index int) (isSuccess bool) { 60 length := it.Length() 61 if length-1 > index { 62 return false 63 } 64 65 // a = append(a[:i], a[i+1:]...) 66 // https://github.com/golang/go/wiki/SliceTricks 67 items := it.items 68 it.items = append(items[:index], items[index+1:]...) 69 70 return true 71 } 72 73 func (it *Collection) Count() int { 74 return it.Length() 75 } 76 77 func (it *Collection) Capacity() int { 78 if it.items == nil { 79 return 0 80 } 81 82 return cap(it.items) 83 } 84 85 func (it *Collection) Length() int { 86 if it == nil || it.items == nil { 87 return 0 88 } 89 90 return len(it.items) 91 } 92 93 func (it *Collection) LengthLock() int { 94 it.Lock() 95 defer it.Unlock() 96 97 if it == nil || it.items == nil { 98 return 0 99 } 100 101 return len(it.items) 102 } 103 104 func (it *Collection) IsEqualsPtr( 105 anotherCollection *Collection, 106 ) bool { 107 return it.IsEqualsWithSensitivePtr( 108 anotherCollection, 109 true) 110 } 111 112 func (it *Collection) IsEqualsWithSensitivePtr( 113 anotherCollection *Collection, 114 isCaseSensitive bool, 115 ) bool { 116 if anotherCollection == nil && it == nil { 117 return true 118 } 119 120 if anotherCollection == nil || it == nil { 121 return false 122 } 123 124 if it == anotherCollection { 125 return true 126 } 127 128 if it.IsEmpty() && anotherCollection.IsEmpty() { 129 return true 130 } 131 132 if it.IsEmpty() || anotherCollection.IsEmpty() { 133 return false 134 } 135 136 if it.Length() != anotherCollection.Length() { 137 return false 138 } 139 140 leftItems := it.items 141 rightItems := anotherCollection.items 142 143 if isCaseSensitive { 144 for i, leftVal := range leftItems { 145 if leftVal != rightItems[i] { 146 return false 147 } 148 } 149 150 return true 151 } 152 153 for i, leftVal := range leftItems { 154 if !strings.EqualFold(leftVal, rightItems[i]) { 155 return false 156 } 157 } 158 159 return true 160 } 161 162 func (it *Collection) IsEmptyLock() bool { 163 it.Lock() 164 defer it.Unlock() 165 166 return it == nil || 167 len(it.items) == 0 168 } 169 170 func (it *Collection) IsEmpty() bool { 171 return it == nil || 172 len(it.items) == 0 173 } 174 175 func (it *Collection) HasItems() bool { 176 return it != nil && 177 len(it.items) > 0 178 } 179 180 func (it *Collection) AddLock(str string) *Collection { 181 it.Lock() 182 defer it.Unlock() 183 184 it.items = append( 185 it.items, 186 str) 187 188 return it 189 } 190 191 func (it *Collection) AddNonEmpty(str string) *Collection { 192 if str == "" { 193 return it 194 } 195 196 it.items = append( 197 it.items, 198 str) 199 200 return it 201 } 202 203 func (it *Collection) AddNonEmptyWhitespace(str string) *Collection { 204 if strutilinternal.IsEmptyOrWhitespace(str) { 205 return it 206 } 207 208 it.items = append( 209 it.items, 210 str) 211 212 return it 213 } 214 215 func (it *Collection) Add(str string) *Collection { 216 it.items = append( 217 it.items, 218 str) 219 220 return it 221 } 222 223 func (it *Collection) AddError(err error) *Collection { 224 if err == nil { 225 return it 226 } 227 228 it.items = append( 229 it.items, 230 err.Error()) 231 232 return it 233 } 234 235 func (it *Collection) AsDefaultError() error { 236 return it.AsError(constants.NewLineUnix) 237 } 238 239 func (it *Collection) AsError(sep string) error { 240 if it.Length() == 0 { 241 return nil 242 } 243 244 toStr := it.Join(sep) 245 246 return errors.New(toStr) 247 } 248 249 func (it *Collection) AddIf( 250 isAdd bool, 251 addingString string, 252 ) *Collection { 253 if !isAdd { 254 return it 255 } 256 257 it.items = append( 258 it.items, 259 addingString) 260 261 return it 262 } 263 264 func (it *Collection) EachItemSplitBy(splitBy string) []string { 265 slice := make([]string, 0, it.Length()*constants.Capacity3) 266 267 for _, item := range it.items { 268 splitItems := strings.Split(item, splitBy) 269 slice = append(slice, splitItems...) 270 } 271 272 return slice 273 } 274 275 func (it *Collection) ConcatNew( 276 predictiveLengthAdd int, 277 addingStrings ...string, 278 ) *Collection { 279 length := len(addingStrings) 280 281 if length == 0 { 282 return New.Collection.StringsOptions( 283 true, 284 it.items, 285 ) 286 } 287 288 finalLength := it.Length() + length 289 capacity := defaultcapacity.PredictiveFiftyPercentIncrement( 290 finalLength, 291 predictiveLengthAdd) 292 293 return New.Collection.Cap(capacity). 294 Adds(it.items...). 295 AddStringsPtr(&addingStrings) 296 } 297 298 func (it *Collection) ToError(sep string) error { 299 return errcore.SliceError(sep, &it.items) 300 } 301 302 func (it *Collection) ToDefaultError() error { 303 return errcore.SliceError( 304 constants.NewLineUnix, &it.items) 305 } 306 307 func (it *Collection) AddIfMany( 308 isAdd bool, 309 addingStrings ...string, 310 ) *Collection { 311 if !isAdd { 312 return it 313 } 314 315 it.items = append( 316 it.items, 317 addingStrings...) 318 319 return it 320 } 321 322 func (it *Collection) AddFunc(f func() string) *Collection { 323 it.items = append( 324 it.items, 325 f()) 326 327 return it 328 } 329 330 func (it *Collection) AddFuncErr( 331 funcReturnsStringError func() (result string, err error), 332 errHandler func(errInput error), 333 ) *Collection { 334 r, err := funcReturnsStringError() 335 336 if err != nil { 337 errHandler(err) 338 339 return it 340 } 341 342 it.items = append( 343 it.items, 344 r) 345 346 return it 347 } 348 349 func (it *Collection) AddsLock(items ...string) *Collection { 350 it.Lock() 351 defer it.Unlock() 352 353 it.items = append( 354 it.items, 355 items...) 356 357 return it 358 } 359 360 func (it *Collection) Adds(items ...string) *Collection { 361 it.items = append( 362 it.items, 363 items...) 364 365 return it 366 } 367 368 func (it *Collection) AddCollection(collectionIn *Collection) *Collection { 369 return it.AddStringsPtr(&collectionIn.items) 370 } 371 372 // AddCollections skip on nil 373 func (it *Collection) AddCollections(collectionsIn ...*Collection) *Collection { 374 for _, collectionIn := range collectionsIn { 375 if collectionIn == nil || collectionIn.items == nil { 376 continue 377 } 378 379 it.AddStringsPtr(&collectionIn.items) 380 } 381 382 return it 383 } 384 385 // AddPointerCollections skip on nil 386 func (it *Collection) AddPointerCollections(collectionsIn *[]*Collection) *Collection { 387 for _, collectionIn := range *collectionsIn { 388 if collectionIn == nil || collectionIn.items == nil { 389 continue 390 } 391 392 it.AddStringsPtr(&collectionIn.items) 393 } 394 395 return it 396 } 397 398 func (it *Collection) AddPointerCollectionsLock(collectionsIn *[]*Collection) *Collection { 399 it.Lock() 400 defer it.Unlock() 401 402 return it.AddPointerCollections(collectionsIn) 403 } 404 405 func (it *Collection) AddHashmapsValues( 406 hashmaps ...*Hashmap, 407 ) *Collection { 408 if hashmaps == nil { 409 return it 410 } 411 412 for _, hashmap := range hashmaps { 413 if hashmap == nil || hashmap.IsEmpty() { 414 continue 415 } 416 417 for _, v := range hashmap.items { 418 it.items = append( 419 it.items, 420 v) 421 } 422 } 423 424 return it 425 } 426 427 func (it *Collection) AddHashmapsKeys( 428 hashmaps ...*Hashmap, 429 ) *Collection { 430 if hashmaps == nil { 431 return it 432 } 433 434 it.resizeForHashmaps( 435 &hashmaps, 436 constants.One) 437 438 for _, hashmap := range hashmaps { 439 if hashmap == nil || hashmap.IsEmpty() { 440 continue 441 } 442 443 for k := range hashmap.items { 444 it.items = append( 445 it.items, 446 k) 447 } 448 } 449 450 return it 451 } 452 453 func (it *Collection) isResizeRequired( 454 length int, 455 ) bool { 456 if length < constants.ArbitraryCapacity200 { 457 return false 458 } 459 460 windowLength := it.Capacity() - it.Length() 461 if windowLength >= length { 462 return false 463 } 464 465 return true 466 } 467 468 func (it *Collection) resizeForHashmaps( 469 hashmaps *[]*Hashmap, 470 multiplier int, 471 ) *Collection { 472 if hashmaps == nil { 473 return it 474 } 475 476 length := 0 477 478 for _, hashmap := range *hashmaps { 479 if hashmap == nil || hashmap.IsEmpty() { 480 continue 481 } 482 483 length += hashmap.Length() 484 } 485 486 if !it.isResizeRequired(length) { 487 return it 488 } 489 490 finalLength := 491 length*multiplier + 492 length/2 493 494 return it.AddCapacity(finalLength) 495 } 496 497 func (it *Collection) resizeForCollections( 498 collections *[]*Collection, 499 multiplier int, 500 ) *Collection { 501 if collections == nil { 502 return it 503 } 504 505 length := 0 506 507 for _, hashmap := range *collections { 508 if hashmap == nil || hashmap.IsEmpty() { 509 continue 510 } 511 512 length += hashmap.Length() 513 } 514 515 if !it.isResizeRequired(length) { 516 return it 517 } 518 519 finalLength := 520 length*multiplier + 521 length/2 522 523 return it.AddCapacity(finalLength) 524 } 525 526 func (it *Collection) resizeForItems( 527 items *[]string, 528 multiplier int, 529 ) *Collection { 530 if items == nil { 531 return it 532 } 533 534 length := len(*items) 535 if !it.isResizeRequired(length) { 536 return it 537 } 538 539 finalLength := 540 length*multiplier + 541 length/2 542 543 return it.AddCapacity(finalLength) 544 } 545 546 func (it *Collection) resizeForPointerItems( 547 items *[]*string, 548 multiplier int, 549 ) *Collection { 550 if items == nil { 551 return it 552 } 553 554 length := len(*items) 555 if !it.isResizeRequired(length) { 556 return it 557 } 558 559 finalLength := 560 length*multiplier + 561 length/2 562 563 return it.AddCapacity(finalLength) 564 } 565 566 func (it *Collection) resizeForAnys( 567 items *[]interface{}, 568 multiplier int, 569 ) *Collection { 570 if items == nil { 571 return it 572 } 573 574 length := len(*items) 575 if !it.isResizeRequired(length) { 576 return it 577 } 578 579 finalLength := 580 length*multiplier + 581 length/2 582 583 return it.AddCapacity(finalLength) 584 } 585 586 func (it *Collection) AddHashmapsKeysValues( 587 hashmaps ...*Hashmap, 588 ) *Collection { 589 if hashmaps == nil { 590 return it 591 } 592 593 it.resizeForHashmaps( 594 &hashmaps, 595 constants.ArbitraryCapacity2) 596 597 for _, hashmap := range hashmaps { 598 if hashmap == nil || hashmap.IsEmpty() { 599 continue 600 } 601 602 for k, v := range hashmap.items { 603 it.items = append( 604 it.items, 605 k) 606 it.items = append( 607 it.items, 608 v) 609 } 610 } 611 612 return it 613 } 614 615 func (it *Collection) AddHashmapsKeysValuesUsingFilter( 616 filter IsKeyValueFilter, 617 hashmaps ...*Hashmap, 618 ) *Collection { 619 if hashmaps == nil { 620 return it 621 } 622 623 it.resizeForHashmaps( 624 &hashmaps, 625 constants.One) 626 627 for _, hashmap := range hashmaps { 628 if hashmap == nil || hashmap.IsEmpty() { 629 continue 630 } 631 632 for k, v := range hashmap.items { 633 result, isAcceptable, isBreak := filter(KeyValuePair{ 634 Key: k, 635 Value: v, 636 }) 637 638 if isAcceptable { 639 it.items = append( 640 it.items, 641 result) 642 } 643 644 if isBreak { 645 return it 646 } 647 } 648 } 649 650 return it 651 } 652 653 func (it *Collection) AddPtr(str *string) *Collection { 654 it.items = append( 655 it.items, 656 *str) 657 658 return it 659 } 660 661 func (it *Collection) AddPtrLock(str *string) *Collection { 662 it.Lock() 663 defer it.Unlock() 664 665 it.items = append( 666 it.items, 667 *str) 668 669 return it 670 } 671 672 func (it *Collection) AddWithWgLock( 673 str string, 674 group *sync.WaitGroup, 675 ) *Collection { 676 it.Lock() 677 defer it.Unlock() 678 679 it.items = append( 680 it.items, 681 str) 682 683 group.Done() 684 685 return it 686 } 687 688 func (it *Collection) AddsPtrLock(itemsPtr ...*string) *Collection { 689 it.Lock() 690 defer it.Unlock() 691 692 for _, str := range itemsPtr { 693 it.items = append( 694 it.items, 695 *str) 696 } 697 698 return it 699 } 700 701 func (it *Collection) AddStringsPtrWgLock( 702 str *[]string, 703 group *sync.WaitGroup, 704 ) *Collection { 705 it.Lock() 706 defer it.Unlock() 707 708 it.items = append( 709 it.items, 710 *str...) 711 712 group.Done() 713 714 return it 715 } 716 717 // AddPointerStringsPtrLock skip on nil 718 func (it *Collection) AddPointerStringsPtrLock( 719 pointerStringItems *[]*string, 720 ) *Collection { 721 it.Lock() 722 defer it.Unlock() 723 724 return it. 725 AddPointerStringsPtr(pointerStringItems) 726 } 727 728 // AddPointerStringsPtr skip on nil 729 func (it *Collection) AddPointerStringsPtr( 730 pointerStringItems *[]*string, 731 ) *Collection { 732 for i := range *pointerStringItems { 733 newPtr := (*pointerStringItems)[i] 734 735 if newPtr == nil { 736 continue 737 } 738 739 it.items = append( 740 it.items, 741 *(*pointerStringItems)[i]) 742 } 743 744 return it 745 } 746 747 func (it *Collection) IndexAt( 748 index int, 749 ) string { 750 return (it.items)[index] 751 } 752 753 func (it *Collection) SafePointerIndexAt( 754 index int, 755 ) *string { 756 length := it.Length() 757 if length-1 < index { 758 return nil 759 } 760 761 return &(it.items)[index] 762 } 763 764 func (it *Collection) SafePointerIndexAtUsingLength( 765 length, index int, 766 ) *string { 767 if length-1 < index { 768 return nil 769 } 770 771 return &(it.items)[index] 772 } 773 774 func (it *Collection) SafeIndexAtUsingLength( 775 defaultString string, length, index int, 776 ) string { 777 if length-1 < index { 778 return defaultString 779 } 780 781 return (it.items)[index] 782 } 783 784 func (it *Collection) First() string { 785 return (it.items)[0] 786 } 787 788 func (it *Collection) Single() string { 789 length := it.Length() 790 if length != 1 { 791 errcore.LengthShouldBeEqualToType.HandleUsingPanic( 792 "1", 793 length) 794 } 795 796 return (it.items)[0] 797 } 798 799 func (it *Collection) Last() string { 800 length := it.Length() 801 802 return (it.items)[length-1] 803 } 804 805 func (it *Collection) LastOrDefault() string { 806 length := it.Length() 807 808 if length == 0 { 809 return constants.EmptyString 810 } 811 812 return (it.items)[length-1] 813 } 814 815 func (it *Collection) FirstOrDefault() string { 816 if it.IsEmpty() { 817 return constants.EmptyString 818 } 819 820 return (it.items)[0] 821 } 822 823 // Take use One based index 824 func (it *Collection) Take( 825 take int, 826 ) *Collection { 827 length := it.Length() 828 829 if length <= take { 830 return it 831 } 832 833 if take == 0 { 834 return Empty.Collection() 835 } 836 837 list := (it.items)[:take] 838 839 return New.Collection.StringsOptions( 840 false, 841 list, 842 ) 843 } 844 845 // Skip use One based index 846 func (it *Collection) Skip( 847 skip int, 848 ) *Collection { 849 length := it.Length() 850 851 if length < skip { 852 errcore. 853 LengthShouldBeEqualToType. 854 HandleUsingPanic( 855 "Length is lower than skip value. Skip:", 856 skip) 857 } 858 859 if skip == 0 { 860 return it 861 } 862 863 list := (it.items)[skip:] 864 865 return New.Collection.StringsOptions( 866 false, 867 list, 868 ) 869 } 870 871 func (it *Collection) Reverse() *Collection { 872 length := it.Length() 873 874 if length <= constants.Capacity1 { 875 return it 876 } 877 878 if length == constants.Capacity2 { 879 it.items[0], it.items[1] = it.items[1], it.items[0] 880 881 return it 882 } 883 884 mid := length / 2 885 lastIndex := length - 1 886 887 for i := 0; i < mid; i++ { 888 it.items[i], it.items[lastIndex-i] = 889 it.items[lastIndex-i], it.items[i] 890 } 891 892 return it 893 } 894 895 func (it *Collection) GetPagesSize( 896 eachPageSize int, 897 ) int { 898 length := it.Length() 899 900 pagesPossibleFloat := float64(length) / float64(eachPageSize) 901 pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat)) 902 903 return pagesPossibleCeiling 904 } 905 906 func (it *Collection) GetPagedCollection( 907 eachPageSize int, 908 ) *CollectionsOfCollection { 909 length := it.Length() 910 911 if length < eachPageSize { 912 return New.CollectionsOfCollection.Strings( 913 it.items) 914 } 915 916 pagesPossibleFloat := float64(length) / float64(eachPageSize) 917 pagesPossibleCeiling := int(math.Ceil(pagesPossibleFloat)) 918 collectionOfCollection := New.CollectionsOfCollection.Cap( 919 pagesPossibleCeiling) 920 921 wg := sync.WaitGroup{} 922 addPagedItemsFunc := func(oneBasedPageIndex int) { 923 pagedCollection := it.GetSinglePageCollection( 924 eachPageSize, 925 oneBasedPageIndex, 926 ) 927 928 collectionOfCollection.items[oneBasedPageIndex-1] = pagedCollection 929 930 wg.Done() 931 } 932 933 wg.Add(pagesPossibleCeiling) 934 for i := 1; i <= pagesPossibleCeiling; i++ { 935 go addPagedItemsFunc(i) 936 } 937 938 wg.Wait() 939 940 return collectionOfCollection 941 } 942 943 // GetSinglePageCollection PageIndex is one based index. Should be above or equal 1 944 func (it *Collection) GetSinglePageCollection( 945 eachPageSize int, 946 pageIndex int, 947 ) *Collection { 948 length := it.Length() 949 950 if length < eachPageSize { 951 return it 952 } 953 954 /** 955 * eachPageItems = 10 956 * pageIndex = 4 957 * skipItems = 10 * (4 - 1) = 30 958 */ 959 skipItems := eachPageSize * (pageIndex - 1) 960 if skipItems < 0 { 961 errcore. 962 CannotBeNegativeIndexType. 963 HandleUsingPanic( 964 "pageIndex cannot be negative or zero.", 965 pageIndex) 966 } 967 968 endingIndex := skipItems + eachPageSize 969 970 if endingIndex > length { 971 endingIndex = length 972 } 973 974 list := (it.items)[skipItems:endingIndex] 975 976 return New.Collection.StringsOptions( 977 false, 978 list, 979 ) 980 } 981 982 func (it *Collection) AddStringsPtr( 983 stringItems *[]string, 984 ) *Collection { 985 if stringItems == nil { 986 return it 987 } 988 989 it.resizeForItems( 990 stringItems, 991 constants.One) 992 993 it.items = append( 994 it.items, 995 *stringItems...) 996 997 return it 998 } 999 1000 func (it *Collection) AddStringsPtrLock( 1001 stringItems *[]string, 1002 ) *Collection { 1003 it.Lock() 1004 defer it.Unlock() 1005 1006 it.AddStringsPtr(stringItems) 1007 1008 return it 1009 } 1010 1011 func (it *Collection) AddStringsPtrAsync( 1012 wg *sync.WaitGroup, 1013 stringItems *[]string, 1014 ) *Collection { 1015 if stringItems == nil { 1016 return it 1017 } 1018 1019 go func() { 1020 it.Lock() 1021 1022 it.AddStringsPtr(stringItems) 1023 1024 it.Unlock() 1025 1026 wg.Done() 1027 }() 1028 1029 return it 1030 } 1031 1032 func (it *Collection) InsertItemsAt( 1033 index int, stringItems *[]string, 1034 ) *Collection { 1035 length := it.Length() 1036 isAtFirst := length == 0 1037 isAtLast := length-1 == index 1038 isAppendItems := isAtFirst || isAtLast 1039 1040 if isAppendItems { 1041 return it.AddStringsPtr(stringItems) 1042 } 1043 1044 // https://bit.ly/3pIDfRY 1045 it.items = 1046 append( 1047 (it.items)[:index], 1048 *stringItems...) 1049 1050 it.items = append( 1051 it.items, 1052 (it.items)[index:]...) 1053 1054 return it 1055 } 1056 1057 func (it *Collection) ChainRemoveAt( 1058 index int, 1059 ) *Collection { 1060 it.items = append( 1061 (it.items)[:index], 1062 (it.items)[index+1:]...) 1063 1064 return it 1065 } 1066 1067 // RemoveItemsIndexes creates a new collection without the indexes mentioned. 1068 // 1069 // it is better to filter out than remove. 1070 func (it *Collection) RemoveItemsIndexes( 1071 isIgnoreRemoveError bool, 1072 indexes ...int, 1073 ) *Collection { 1074 if isIgnoreRemoveError && indexes == nil { 1075 return it 1076 } 1077 1078 return it. 1079 RemoveItemsIndexesPtr(isIgnoreRemoveError, indexes) 1080 } 1081 1082 // RemoveItemsIndexesPtr creates a new collection without the indexes mentioned. 1083 // 1084 // it is better to filter out than remove. 1085 func (it *Collection) RemoveItemsIndexesPtr( 1086 isIgnoreRemoveError bool, 1087 indexes []int, 1088 ) *Collection { 1089 if indexes == nil { 1090 return it 1091 } 1092 1093 length := it.Length() 1094 indexesLength := len(indexes) 1095 hasPossibleError := length == 0 && indexesLength > 0 1096 1097 if hasPossibleError && !isIgnoreRemoveError { 1098 panic(errcore.CannotRemoveIndexesFromEmptyCollectionType) 1099 } 1100 1101 if !isIgnoreRemoveError { 1102 errcore.PanicOnIndexOutOfRange(length, indexes) 1103 } 1104 1105 if hasPossibleError { 1106 return it 1107 } 1108 1109 newList := make([]string, 0, it.Capacity()) 1110 for i, s := range it.items { //nolint:wsl 1111 if coreindexes.HasIndex(indexes, i) { 1112 continue 1113 } 1114 1115 newList = append(newList, s) 1116 } 1117 1118 it.items = newList 1119 1120 return it 1121 } 1122 1123 func (it *Collection) AppendCollectionPtr( 1124 anotherCollection *Collection, 1125 ) *Collection { 1126 it.resizeForItems( 1127 &anotherCollection.items, 1128 constants.One) 1129 1130 it.items = append( 1131 it.items, 1132 anotherCollection.items...) 1133 1134 return it 1135 } 1136 1137 func (it *Collection) AppendCollectionsPtr( 1138 anotherCollectionsPtr ...*Collection, 1139 ) *Collection { 1140 if anotherCollectionsPtr == nil { 1141 return it 1142 } 1143 1144 return it.AppendPointersCollectionsPtr( 1145 &anotherCollectionsPtr) 1146 } 1147 1148 func (it *Collection) AppendPointersCollectionsPtr( 1149 anotherCollectionsPtr *[]*Collection, 1150 ) *Collection { 1151 if anotherCollectionsPtr == nil { 1152 return it 1153 } 1154 1155 it.resizeForCollections( 1156 anotherCollectionsPtr, 1157 constants.One) 1158 1159 capacitiesIncrease := 0 1160 for _, currentCollection := range *anotherCollectionsPtr { 1161 1162 if currentCollection == nil || currentCollection.IsEmpty() { 1163 continue 1164 } 1165 1166 capacitiesIncrease += currentCollection.Length() 1167 } 1168 1169 it.AddCapacity(capacitiesIncrease) 1170 1171 for _, currentCollection := range *anotherCollectionsPtr { 1172 if currentCollection == nil || currentCollection.IsEmpty() { 1173 continue 1174 } 1175 1176 it.items = append( 1177 it.items, 1178 currentCollection.items...) 1179 } 1180 1181 return it 1182 } 1183 1184 func (it *Collection) AppendCollectionsPtrAsync( 1185 wg *sync.WaitGroup, 1186 anotherCollectionsPtr ...*Collection, 1187 ) *Collection { 1188 if anotherCollectionsPtr == nil { 1189 return it 1190 } 1191 1192 go func() { 1193 it.AppendPointersCollectionsPtr( 1194 &anotherCollectionsPtr) 1195 1196 wg.Done() 1197 }() 1198 1199 return it 1200 } 1201 1202 // AppendAnysAsync Continue on nil 1203 func (it *Collection) AppendAnysAsync( 1204 wg *sync.WaitGroup, 1205 anys ...interface{}, 1206 ) *Collection { 1207 if anys == nil { 1208 return it 1209 } 1210 1211 go func() { 1212 it.Lock() 1213 it.resizeForAnys( 1214 &anys, 1215 constants.One) 1216 it.Unlock() 1217 1218 it.AppendAnysLock(&anys) 1219 1220 wg.Done() 1221 }() 1222 1223 return it 1224 } 1225 1226 // AppendAnysLock Continue on nil 1227 func (it *Collection) AppendAnysLock( 1228 anys *[]interface{}, 1229 ) *Collection { 1230 if anys == nil { 1231 return it 1232 } 1233 1234 it.resizeForAnys( 1235 anys, 1236 constants.One) 1237 1238 for _, any := range *anys { 1239 if any == nil { 1240 continue 1241 } 1242 1243 anyStr := fmt.Sprintf(constants.SprintValueFormat, any) 1244 1245 it.Lock() 1246 it.items = append( 1247 it.items, 1248 anyStr) 1249 it.Unlock() 1250 } 1251 1252 return it 1253 } 1254 1255 // AppendAnys Continue on nil 1256 func (it *Collection) AppendAnys( 1257 anys ...interface{}, 1258 ) *Collection { 1259 if anys == nil { 1260 return it 1261 } 1262 1263 it.resizeForAnys( 1264 &anys, 1265 constants.One) 1266 1267 for _, any := range anys { 1268 if any == nil { 1269 continue 1270 } 1271 1272 anyStr := fmt.Sprintf( 1273 constants.SprintValueFormat, 1274 any, 1275 ) 1276 1277 it.items = append( 1278 it.items, 1279 anyStr) 1280 } 1281 1282 return it 1283 } 1284 1285 // AppendAnysUsingFilter Skip on nil 1286 func (it *Collection) AppendAnysUsingFilter( 1287 filter IsStringFilter, 1288 anys ...interface{}, 1289 ) *Collection { 1290 if anys == nil { 1291 return it 1292 } 1293 1294 it.resizeForAnys( 1295 &anys, 1296 constants.One) 1297 1298 for i, any := range anys { 1299 if any == nil { 1300 continue 1301 } 1302 1303 anyStr := fmt.Sprintf( 1304 constants.SprintValueFormat, 1305 any) 1306 1307 result, isKeep, isBreak := filter(anyStr, i) 1308 1309 if !isKeep { 1310 continue 1311 } 1312 1313 it.items = append( 1314 it.items, 1315 result) 1316 1317 if isBreak { 1318 return it 1319 } 1320 } 1321 1322 return it 1323 } 1324 1325 // AppendAnysUsingFilterLock Skip on nil 1326 func (it *Collection) AppendAnysUsingFilterLock( 1327 filter IsStringFilter, 1328 anys ...interface{}, 1329 ) *Collection { 1330 if anys == nil { 1331 return it 1332 } 1333 1334 it.resizeForAnys( 1335 &anys, 1336 constants.One) 1337 1338 for i, any := range anys { 1339 if any == nil { 1340 continue 1341 } 1342 1343 anyStr := fmt.Sprintf(constants.SprintValueFormat, any) 1344 result, isKeep, isBreak := filter(anyStr, i) 1345 1346 if !isKeep { 1347 continue 1348 } 1349 1350 it.Lock() 1351 it.items = append( 1352 it.items, 1353 result) 1354 it.Unlock() 1355 1356 if isBreak { 1357 return it 1358 } 1359 } 1360 1361 return it 1362 } 1363 1364 // AppendNonEmptyAnys Continue on nil 1365 func (it *Collection) AppendNonEmptyAnys( 1366 anys ...interface{}, 1367 ) *Collection { 1368 if anys == nil { 1369 return it 1370 } 1371 1372 it.resizeForAnys( 1373 &anys, 1374 constants.One) 1375 1376 for _, any := range anys { 1377 if any == nil { 1378 continue 1379 } 1380 1381 anyStr := fmt.Sprintf(constants.SprintValueFormat, any) 1382 if anyStr == "" { 1383 continue 1384 } 1385 1386 it.items = append( 1387 it.items, 1388 anyStr) 1389 } 1390 1391 return it 1392 } 1393 1394 // AddsPtr Skip on nil 1395 func (it *Collection) AddsPtr(itemsPtr ...*string) *Collection { 1396 if itemsPtr == nil { 1397 return it 1398 } 1399 1400 for _, str := range itemsPtr { 1401 if str == nil { 1402 continue 1403 } 1404 1405 it.items = append( 1406 it.items, 1407 *str) 1408 } 1409 1410 return it 1411 } 1412 1413 // AddsPtrAsync Skip on nil 1414 func (it *Collection) AddsPtrAsync( 1415 wg *sync.WaitGroup, 1416 itemsPtr ...*string, 1417 ) *Collection { 1418 if itemsPtr == nil { 1419 return it 1420 } 1421 1422 go func() { 1423 it.Lock() 1424 it.resizeForPointerItems( 1425 &itemsPtr, 1426 constants.One) 1427 1428 it.Unlock() 1429 1430 for _, str := range itemsPtr { 1431 if str == nil { 1432 continue 1433 } 1434 1435 it.Lock() 1436 1437 it.items = append( 1438 it.items, 1439 *str) 1440 1441 it.Unlock() 1442 } 1443 1444 wg.Done() 1445 }() 1446 1447 return it 1448 } 1449 1450 func (it *Collection) AddsNonEmptyPtr(itemsPtr ...*string) *Collection { 1451 if itemsPtr == nil { 1452 return it 1453 } 1454 1455 for _, str := range itemsPtr { 1456 if str == nil || *str == "" { 1457 continue 1458 } 1459 1460 it.items = append( 1461 it.items, 1462 *str) 1463 } 1464 1465 return it 1466 } 1467 1468 func (it *Collection) AddsNonEmptyPtrLock( 1469 itemsPtr ...*string, 1470 ) *Collection { 1471 if itemsPtr == nil { 1472 return it 1473 } 1474 1475 for _, str := range itemsPtr { 1476 if str == nil || *str == "" { 1477 continue 1478 } 1479 1480 it.Lock() 1481 it.items = append( 1482 it.items, 1483 *str) 1484 it.Unlock() 1485 } 1486 1487 return it 1488 } 1489 1490 func (it *Collection) UniqueBoolMapLock() *map[string]bool { 1491 it.Lock() 1492 defer it.Unlock() 1493 1494 return it.UniqueBoolMap() 1495 } 1496 1497 func (it *Collection) UniqueBoolMap() *map[string]bool { 1498 respectiveMap := make( 1499 map[string]bool, 1500 it.Length()) 1501 1502 for _, item := range it.items { 1503 respectiveMap[item] = true 1504 } 1505 1506 return &respectiveMap 1507 } 1508 1509 func (it *Collection) UniqueListPtr() *[]string { 1510 boolMap := it.UniqueBoolMap() 1511 list := make([]string, len(*boolMap)) 1512 1513 i := 0 1514 for str := range *boolMap { 1515 list[i] = str 1516 i++ 1517 } 1518 1519 return &list 1520 } 1521 1522 func (it *Collection) UniqueListPtrLock() *[]string { 1523 it.Lock() 1524 defer it.Unlock() 1525 1526 return it.UniqueListPtr() 1527 } 1528 1529 func (it *Collection) UniqueListLock() []string { 1530 it.Lock() 1531 defer it.Unlock() 1532 1533 return it.UniqueList() 1534 } 1535 1536 func (it *Collection) UniqueList() []string { 1537 return *it.UniqueListPtr() 1538 } 1539 1540 func (it *Collection) List() []string { 1541 return it.items 1542 } 1543 1544 // Filter must return a slice 1545 func (it *Collection) Filter(filter IsStringFilter) []string { 1546 if it.IsEmpty() { 1547 return []string{} 1548 } 1549 1550 list := make([]string, 0, it.Length()) 1551 1552 for i, element := range it.items { 1553 result, isKeep, isBreak := filter(element, i) 1554 1555 if isKeep { 1556 list = append(list, result) 1557 } 1558 1559 if isBreak { 1560 return list 1561 } 1562 } 1563 1564 return list 1565 } 1566 1567 // FilterLock must return a slice 1568 func (it *Collection) FilterLock(filter IsStringFilter) []string { 1569 elements := it.ListCopyPtrLock() 1570 length := len(elements) 1571 1572 if length == 0 { 1573 return elements 1574 } 1575 1576 list := make([]string, 0, length) 1577 1578 for i, element := range elements { 1579 result, isKeep, isBreak := filter(element, i) 1580 1581 if isKeep { 1582 list = append(list, result) 1583 } 1584 1585 if isBreak { 1586 return list 1587 } 1588 } 1589 1590 return list 1591 } 1592 1593 // FilteredCollection must return a items 1594 func (it *Collection) FilteredCollection(filter IsStringFilter) *Collection { 1595 return New.Collection.Strings(it.Filter(filter)) 1596 } 1597 1598 // FilteredCollectionLock must return a items 1599 func (it *Collection) FilteredCollectionLock(filter IsStringFilter) *Collection { 1600 return New.Collection.Strings(it.FilterLock(filter)) 1601 } 1602 1603 // FilterPtrLock must return a slice 1604 func (it *Collection) FilterPtrLock( 1605 filterPtr IsStringPointerFilter, 1606 ) *[]*string { 1607 elements := it.ListCopyPtrLock() 1608 length := len(elements) 1609 1610 if length == 0 { 1611 return &([]*string{}) 1612 } 1613 1614 list := make([]*string, 0, length) 1615 1616 for i := range elements { 1617 copyTo := elements[i] 1618 result, isKeep, isBreak := 1619 filterPtr(©To, i) 1620 1621 if isKeep { 1622 list = append(list, result) 1623 } 1624 1625 if isBreak { 1626 return &list 1627 } 1628 } 1629 1630 return &list 1631 } 1632 1633 // FilterPtr must return a slice 1634 func (it *Collection) FilterPtr(filterPtr IsStringPointerFilter) *[]*string { 1635 if it.IsEmpty() { 1636 return &([]*string{}) 1637 } 1638 1639 list := make([]*string, 0, it.Length()) 1640 1641 for i := range it.items { 1642 result, isKeep, isBreak := filterPtr( 1643 &it.items[i], i) 1644 1645 if isKeep { 1646 list = append(list, result) 1647 } 1648 1649 if isBreak { 1650 return &list 1651 } 1652 } 1653 1654 return &list 1655 } 1656 1657 // NonEmptyListPtr must return a slice 1658 func (it *Collection) NonEmptyListPtr() *[]string { 1659 if it.IsEmpty() { 1660 return &([]string{}) 1661 } 1662 1663 list := make([]string, 0, it.Length()) 1664 1665 for _, element := range it.items { 1666 if element == "" { 1667 continue 1668 } 1669 1670 list = append(list, element) 1671 } 1672 1673 return &list 1674 } 1675 1676 func (it *Collection) HashsetAsIs() *Hashset { 1677 return New.Hashset.Strings( 1678 it.items) 1679 } 1680 1681 func (it *Collection) HashsetWithDoubleLength() *Hashset { 1682 return New.Hashset.StringsOption( 1683 it.Length()*2, 1684 false, 1685 it.items...) 1686 } 1687 1688 func (it *Collection) HashsetLock() *Hashset { 1689 return New.Hashset.Strings( 1690 it.ListCopyPtrLock()) 1691 } 1692 1693 func (it *Collection) NonEmptyItems() []string { 1694 return stringslice.NonEmptySlice(it.items) 1695 } 1696 1697 func (it *Collection) NonEmptyItemsPtr() *[]string { 1698 return stringslice.NonEmptySlicePtr(&it.items) 1699 } 1700 1701 func (it *Collection) NonEmptyItemsOrNonWhitespacePtr() *[]string { 1702 return stringslice.NonWhitespaceSlicePtr(&it.items) 1703 } 1704 1705 // Items direct return pointer 1706 func (it *Collection) Items() []string { 1707 return it.items 1708 } 1709 1710 // ListPtr direct return pointer 1711 func (it *Collection) ListPtr() *[]string { 1712 return &it.items 1713 } 1714 1715 // ListCopyPtrLock returns a copy of the items 1716 // 1717 // must return a slice 1718 func (it *Collection) ListCopyPtrLock() []string { 1719 it.Lock() 1720 defer it.Unlock() 1721 1722 if it.IsEmpty() { 1723 return []string{} 1724 } 1725 1726 return it.items 1727 } 1728 1729 func (it *Collection) HasLock(str string) bool { 1730 it.Lock() 1731 defer it.Unlock() 1732 1733 return it.Has(str) 1734 } 1735 1736 func (it *Collection) Has(str string) bool { 1737 if it.IsEmpty() { 1738 return false 1739 } 1740 1741 for _, element := range it.items { 1742 if element == str { 1743 return true 1744 } 1745 } 1746 1747 return false 1748 } 1749 1750 func (it *Collection) HasPtr(str *string) bool { 1751 if str == nil || it.IsEmpty() { 1752 return false 1753 } 1754 1755 for _, element := range it.items { 1756 if element == *str { 1757 return true 1758 } 1759 } 1760 1761 return false 1762 } 1763 1764 func (it *Collection) HasAll(items ...string) bool { 1765 if it.IsEmpty() { 1766 return false 1767 } 1768 1769 for _, element := range items { 1770 if !it.IsContainsPtr(&element) { 1771 return false 1772 } 1773 } 1774 1775 return true 1776 } 1777 1778 // SortedListAsc Creates new doesn't modify current collection 1779 func (it *Collection) SortedListAsc() *[]string { 1780 if it.IsEmpty() { 1781 return &[]string{} 1782 } 1783 1784 list := &(it.items) 1785 sort.Strings(*list) 1786 1787 return list 1788 } 1789 1790 // SortedAsc mutates current collection 1791 func (it *Collection) SortedAsc() *Collection { 1792 if it.IsEmpty() { 1793 return it 1794 } 1795 1796 sort.Strings(it.items) 1797 1798 return it 1799 } 1800 1801 // SortedAscLock mutates current collection 1802 func (it *Collection) SortedAscLock() *Collection { 1803 if it.IsEmptyLock() { 1804 return it 1805 } 1806 1807 it.Lock() 1808 defer it.Unlock() 1809 1810 sort.Strings(it.items) 1811 1812 return it 1813 } 1814 1815 // SortedListDsc Creates new one. 1816 func (it *Collection) SortedListDsc() *[]string { 1817 list := it.SortedListAsc() 1818 stringslice.InPlaceReverse( 1819 &it.items) 1820 1821 return list 1822 } 1823 1824 func (it *Collection) HasUsingSensitivity(str string, isCaseSensitive bool) bool { 1825 if isCaseSensitive { 1826 return it.Has(str) 1827 } 1828 1829 for _, element := range it.items { 1830 if strings.EqualFold(element, str) { 1831 return true 1832 } 1833 } 1834 1835 return false 1836 } 1837 1838 func (it *Collection) IsContainsPtr(item *string) bool { 1839 if item == nil || it.IsEmpty() { 1840 return false 1841 } 1842 1843 for _, element := range it.items { 1844 if element == *item { 1845 return true 1846 } 1847 } 1848 1849 return false 1850 } 1851 1852 // GetHashsetPlusHasAll nil will return false. 1853 func (it *Collection) GetHashsetPlusHasAll(items []string) (*Hashset, bool) { 1854 hashset := it.HashsetAsIs() 1855 1856 if items == nil || it.IsEmpty() { 1857 return hashset, false 1858 } 1859 1860 return hashset, hashset.HasAllStrings(items) 1861 } 1862 1863 // IsContainsAllPtr nil will return false. 1864 func (it *Collection) IsContainsAllPtr(items *[]string) bool { 1865 if items == nil { 1866 return false 1867 } 1868 1869 if it.IsEmpty() { 1870 return false 1871 } 1872 1873 for _, item := range *items { 1874 if !it.IsContainsPtr(&item) { 1875 return false 1876 } 1877 } 1878 1879 return true 1880 } 1881 1882 // IsContainsAll nil will return false. 1883 func (it *Collection) IsContainsAll(items ...string) bool { 1884 if items == nil { 1885 return false 1886 } 1887 1888 return it.IsContainsAllPtr(&items) 1889 } 1890 1891 // IsContainsAllLock nil will return false. 1892 func (it *Collection) IsContainsAllLock(items ...string) bool { 1893 it.Lock() 1894 defer it.Unlock() 1895 1896 if items == nil { 1897 return false 1898 } 1899 1900 return it.IsContainsAllPtr(&items) 1901 } 1902 1903 func (it *Collection) New( 1904 slice ...string, 1905 ) *Collection { 1906 length := len(slice) 1907 1908 if length == 0 { 1909 return New.Collection.Cap(constants.Zero) 1910 } 1911 1912 newCollection := New.Collection.Cap(constants.Zero) 1913 1914 return newCollection.AddStringsPtr(&slice) 1915 } 1916 1917 func (it *Collection) AddNonEmptyStrings( 1918 slice ...string, 1919 ) *Collection { 1920 if len(slice) == 0 { 1921 return it 1922 } 1923 1924 return it. 1925 AddNonEmptyStringsPtr(&slice) 1926 } 1927 1928 func (it *Collection) AddFuncResult( 1929 getterFunctions ...func() string, 1930 ) *Collection { 1931 if getterFunctions == nil { 1932 return it 1933 } 1934 1935 items := it.items 1936 1937 for _, getterFunc := range getterFunctions { 1938 item := getterFunc() 1939 1940 items = append(items, item) 1941 } 1942 1943 it.items = items 1944 1945 return it 1946 } 1947 1948 func (it *Collection) AddNonEmptyStringsPtr( 1949 slice *[]string, 1950 ) *Collection { 1951 if slice == nil || len(*slice) == 0 { 1952 return it 1953 } 1954 1955 items := it.items 1956 1957 for _, addingItem := range *slice { 1958 items = append(items, addingItem) 1959 } 1960 1961 it.items = items 1962 1963 return it 1964 } 1965 1966 func (it *Collection) AddStringsByFuncChecking( 1967 slice *[]string, 1968 isIntegrityOkay func(line string) bool, 1969 ) *Collection { 1970 1971 for _, item := range *slice { 1972 if isIntegrityOkay(item) { 1973 it.Add(item) 1974 } 1975 } 1976 1977 return it 1978 } 1979 1980 func (it *Collection) ExpandSlicePlusAdd( 1981 slice *[]string, 1982 expandFunc func(line string) *[]string, 1983 ) *Collection { 1984 items := stringslice.ExpandByFunc(slice, expandFunc) 1985 1986 return it.AddStringsPtr(items) 1987 } 1988 1989 func (it *Collection) MergeSlicesOfSlicePtr(slices *[]*[]string) *Collection { 1990 it.items = *stringslice.MergeNewSlicesPtrOfSlicesPtr(slices) 1991 1992 return it 1993 } 1994 1995 func (it *Collection) MergeSlicesOfSlice(slices ...*[]string) *Collection { 1996 it.items = *stringslice.MergeNewSlicesPtrOfSlicesPtr(&slices) 1997 1998 return it 1999 } 2000 2001 // GetAllExceptCollection Get all items except the mentioned ones. 2002 // Always returns a copy of new strings. 2003 // It is like set A - B 2004 // Set A = this collection 2005 // Set B = itemsCollection given in parameters. 2006 func (it *Collection) GetAllExceptCollection(itemsCollection *Collection) *[]string { 2007 if itemsCollection == nil || itemsCollection.IsEmpty() { 2008 newItems := it.items 2009 2010 return &newItems 2011 } 2012 2013 finalList := make( 2014 []string, 2015 0, 2016 it.Length()) 2017 2018 for _, item := range it.items { 2019 if itemsCollection.Has(item) { 2020 continue 2021 } 2022 2023 finalList = append( 2024 finalList, 2025 item) 2026 } 2027 2028 return &finalList 2029 } 2030 2031 // GetAllExcept Get all items except the mentioned ones. 2032 // Always returns a copy of new strings. 2033 // It is like set A - B 2034 // Set A = this collection 2035 // Set B = items given in parameters. 2036 func (it *Collection) GetAllExcept(items []string) *[]string { 2037 if items == nil { 2038 newItems := it.items 2039 2040 return &newItems 2041 } 2042 2043 newCollection := New.Collection.StringsOptions( 2044 false, 2045 items, 2046 ) 2047 2048 return it.GetAllExceptCollection( 2049 newCollection) 2050 } 2051 2052 func (it *Collection) CharCollectionMap() *CharCollectionMap { 2053 length := it.Length() 2054 lengthByFourBestGuess := length / 4 2055 runeMap := New.CharCollectionMap.CapSelfCap( 2056 length, 2057 lengthByFourBestGuess) 2058 2059 return runeMap.AddStringsPtr(&it.items) 2060 } 2061 2062 func (it *Collection) SummaryString(sequence int) string { 2063 header := fmt.Sprintf( 2064 summaryOfCharCollectionMapLengthFormat, 2065 it, 2066 it.Length(), 2067 sequence) 2068 2069 return it.SummaryStringWithHeader(header) 2070 } 2071 2072 func (it *Collection) SummaryStringWithHeader(header string) string { 2073 if it.IsEmpty() { 2074 return header + commonJoiner + NoElements 2075 } 2076 2077 return header + it.String() 2078 } 2079 2080 func (it *Collection) String() string { 2081 if it.IsEmpty() { 2082 return commonJoiner + NoElements 2083 } 2084 2085 return commonJoiner + 2086 strings.Join( 2087 it.items, 2088 commonJoiner) 2089 } 2090 2091 func (it *Collection) CsvLines() []string { 2092 return simplewrap.DoubleQuoteWrapElements( 2093 false, 2094 it.items..., 2095 ) 2096 } 2097 2098 func (it *Collection) CsvLinesOptions( 2099 isSkipQuoteOnlyOnExistence bool, 2100 ) []string { 2101 return simplewrap.DoubleQuoteWrapElements( 2102 isSkipQuoteOnlyOnExistence, 2103 it.items...) 2104 } 2105 2106 func (it *Collection) Csv() string { 2107 if it.IsEmpty() { 2108 return constants.EmptyString 2109 } 2110 2111 return it.CsvOptions(false) 2112 } 2113 2114 func (it *Collection) CsvOptions(isSkipQuoteOnlyOnExistence bool) string { 2115 if it.IsEmpty() { 2116 return constants.EmptyString 2117 } 2118 2119 return strings.Join( 2120 it.CsvLinesOptions(isSkipQuoteOnlyOnExistence), 2121 constants.Comma) 2122 } 2123 2124 func (it *Collection) StringLock() string { 2125 if it.IsEmptyLock() { 2126 return commonJoiner + NoElements 2127 } 2128 2129 it.Lock() 2130 defer it.Unlock() 2131 2132 return commonJoiner + 2133 strings.Join( 2134 it.items, 2135 commonJoiner) 2136 } 2137 2138 func (it *Collection) AddCapacity( 2139 capacities ...int, 2140 ) *Collection { 2141 if capacities == nil || len(capacities) == 0 { 2142 return it 2143 } 2144 2145 currentCapacity := it.Capacity() 2146 2147 for _, capacity := range capacities { 2148 currentCapacity += capacity 2149 } 2150 2151 return it.Resize(currentCapacity) 2152 } 2153 2154 // Resize Only resize if capacity is bigger than the current one 2155 func (it *Collection) Resize( 2156 newCapacity int, 2157 ) *Collection { 2158 capacity := it.Capacity() 2159 if capacity >= newCapacity { 2160 return it 2161 } 2162 2163 newItems := make([]string, it.Length(), newCapacity) 2164 copy(newItems, it.items) 2165 2166 it.items = newItems 2167 2168 return it 2169 } 2170 2171 func (it *Collection) Joins( 2172 separator string, 2173 items ...string, 2174 ) string { 2175 if len(items) == 0 { 2176 return strings.Join(it.items, separator) 2177 } 2178 2179 newItems := make([]string, 0, it.Length()+len(items)) 2180 copy(newItems, it.items) 2181 2182 newItems = append(newItems, items...) 2183 2184 return strings.Join(newItems, separator) 2185 } 2186 2187 func (it *Collection) NonEmptyJoins( 2188 joiner string, 2189 ) string { 2190 return stringslice.NonEmptyJoinPtr( 2191 &it.items, 2192 joiner) 2193 } 2194 2195 func (it *Collection) NonWhitespaceJoins( 2196 joiner string, 2197 ) string { 2198 return stringslice.NonWhitespaceJoinPtr( 2199 &it.items, 2200 joiner) 2201 } 2202 2203 func (it *Collection) JsonModel() []string { 2204 return it.items 2205 } 2206 2207 func (it *Collection) JsonModelAny() interface{} { 2208 return it.JsonModel() 2209 } 2210 2211 func (it *Collection) MarshalJSON() ([]byte, error) { 2212 return json.Marshal(it.JsonModel()) 2213 } 2214 2215 func (it *Collection) UnmarshalJSON(data []byte) error { 2216 var dataModel []string 2217 2218 err := json.Unmarshal(data, &dataModel) 2219 2220 if err == nil { 2221 it.items = dataModel 2222 } 2223 2224 return err 2225 } 2226 2227 func (it Collection) Json() corejson.Result { 2228 return corejson.New(it) 2229 } 2230 2231 func (it Collection) JsonPtr() *corejson.Result { 2232 return corejson.NewPtr(it) 2233 } 2234 2235 //goland:noinspection GoLinterLocal 2236 func (it *Collection) ParseInjectUsingJson( 2237 jsonResult *corejson.Result, 2238 ) (*Collection, error) { 2239 err := jsonResult.Unmarshal(it) 2240 2241 if err != nil { 2242 return Empty.Collection(), err 2243 } 2244 2245 return it, nil 2246 } 2247 2248 // ParseInjectUsingJsonMust Panic if error 2249 //goland:noinspection GoLinterLocal 2250 func (it *Collection) ParseInjectUsingJsonMust( 2251 jsonResult *corejson.Result, 2252 ) *Collection { 2253 newUsingJson, err := 2254 it.ParseInjectUsingJson(jsonResult) 2255 2256 if err != nil { 2257 panic(err) 2258 } 2259 2260 return newUsingJson 2261 } 2262 2263 func (it *Collection) JsonParseSelfInject( 2264 jsonResult *corejson.Result, 2265 ) error { 2266 _, err := it.ParseInjectUsingJson( 2267 jsonResult, 2268 ) 2269 2270 return err 2271 } 2272 2273 func (it *Collection) Clear() *Collection { 2274 if it == nil { 2275 return nil 2276 } 2277 2278 it.items = it.items[:0] 2279 2280 return it 2281 } 2282 2283 func (it *Collection) Dispose() { 2284 if it == nil { 2285 return 2286 } 2287 2288 it.Clear() 2289 it.items = nil 2290 } 2291 2292 func (it *Collection) AsJsonMarshaller() corejson.JsonMarshaller { 2293 return it 2294 } 2295 2296 func (it *Collection) AsJsonContractsBinder() corejson.JsonContractsBinder { 2297 return it 2298 } 2299 2300 func (it *Collection) Serialize() ([]byte, error) { 2301 return corejson.Serialize.Raw(it) 2302 } 2303 2304 func (it *Collection) Deserialize(toPtr interface{}) (parsingErr error) { 2305 return it.JsonPtr().Deserialize(toPtr) 2306 }