gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/Hashmap.go (about) 1 package corestr 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "strconv" 7 "strings" 8 "sync" 9 10 "gitlab.com/evatix-go/core/constants" 11 "gitlab.com/evatix-go/core/coredata/corejson" 12 "gitlab.com/evatix-go/core/errcore" 13 "gitlab.com/evatix-go/core/internal/mapdiffinternal" 14 ) 15 16 type Hashmap struct { 17 hasMapUpdated bool 18 isEmptySet bool 19 length int 20 items map[string]string 21 cachedList []string 22 sync.Mutex 23 } 24 25 func (it *Hashmap) IsEmpty() bool { 26 if it == nil { 27 return true 28 } 29 30 if it.hasMapUpdated { 31 it.isEmptySet = len(it.items) == 0 32 } 33 34 return it.isEmptySet 35 } 36 37 func (it *Hashmap) HasItems() bool { 38 return it != nil && !it.IsEmpty() 39 } 40 41 func (it *Hashmap) Collection() *Collection { 42 return New.Collection.StringsOptions(false, it.ValuesList()) 43 } 44 45 func (it *Hashmap) IsEmptyLock() bool { 46 it.Lock() 47 defer it.Unlock() 48 49 return it.IsEmpty() 50 } 51 52 func (it *Hashmap) AddOrUpdatePtr( 53 key, val *string, 54 ) *Hashmap { 55 it.items[*key] = *val 56 it.hasMapUpdated = true 57 58 return it 59 } 60 61 func (it *Hashmap) AddOrUpdateWithWgLock( 62 key, val string, 63 group *sync.WaitGroup, 64 ) *Hashmap { 65 it.Lock() 66 67 it.items[key] = val 68 it.hasMapUpdated = true 69 70 it.Unlock() 71 group.Done() 72 73 return it 74 } 75 76 func (it *Hashmap) AddOrUpdatePtrLock( 77 key, val *string, 78 ) *Hashmap { 79 it.Lock() 80 81 it.items[*key] = *val 82 it.hasMapUpdated = true 83 84 it.Unlock() 85 86 return it 87 } 88 89 func (it *Hashmap) AddOrUpdateKeyStrValInt( 90 key string, 91 val int, 92 ) *Hashmap { 93 it.items[key] = strconv.Itoa(val) 94 it.hasMapUpdated = true 95 96 return it 97 } 98 99 func (it *Hashmap) AddOrUpdateKeyStrValFloat( 100 key string, 101 val float32, 102 ) *Hashmap { 103 it.items[key] = fmt.Sprintf("%f", val) 104 it.hasMapUpdated = true 105 106 return it 107 } 108 109 func (it *Hashmap) AddOrUpdateKeyStrValFloat64( 110 key string, val float64, 111 ) *Hashmap { 112 it.items[key] = fmt.Sprintf("%f", val) 113 it.hasMapUpdated = true 114 115 return it 116 } 117 118 func (it *Hashmap) AddOrUpdateKeyStrValAny( 119 key string, 120 val interface{}, 121 ) *Hashmap { 122 it.items[key] = fmt.Sprintf(constants.SprintValueFormat, val) 123 it.hasMapUpdated = true 124 125 return it 126 } 127 128 func (it *Hashmap) AddOrUpdateKeyValueAny( 129 pair KeyAnyValuePair, 130 ) *Hashmap { 131 it.items[pair.Key] = pair.ValueString() 132 it.hasMapUpdated = true 133 134 return it 135 } 136 137 func (it *Hashmap) AddOrUpdateKeyVal( 138 keyVal KeyValuePair, 139 ) (isAddedNewly bool) { 140 _, isAlreadyExist := it.items[keyVal.Key] 141 142 it.items[keyVal.Key] = keyVal.Value 143 it.hasMapUpdated = true 144 145 return !isAlreadyExist 146 } 147 148 func (it *Hashmap) AddOrUpdate(key, val string) (isAddedNewly bool) { 149 _, isAlreadyExist := it.items[key] 150 151 it.items[key] = val 152 it.hasMapUpdated = true 153 154 return !isAlreadyExist 155 } 156 157 func (it *Hashmap) Set(key, val string) (isAddedNewly bool) { 158 _, isAlreadyExist := it.items[key] 159 160 it.items[key] = val 161 it.hasMapUpdated = true 162 163 return !isAlreadyExist 164 } 165 166 func (it *Hashmap) SetTrim(key, val string) (isAddedNewly bool) { 167 key = strings.TrimSpace(key) 168 val = strings.TrimSpace(val) 169 170 return it.Set(key, val) 171 } 172 173 func (it *Hashmap) SetBySplitter( 174 splitter, line string, 175 ) (isAddedNewly bool) { 176 splits := strings.SplitN( 177 line, splitter, constants.Two) 178 179 if len(splits) >= 2 { 180 // all okay 181 182 return it.Set(splits[0], splits[len(splits)-1]) 183 } 184 185 return it.Set(splits[0], "") 186 } 187 188 func (it *Hashmap) AddOrUpdateStringsPtrWgLock( 189 keys, values *[]string, wg *sync.WaitGroup, 190 ) *Hashmap { 191 if keys == nil || values == nil { 192 return it 193 } 194 195 it.Lock() 196 for i, key := range *keys { 197 it.items[key] = (*values)[i] 198 } 199 200 it.hasMapUpdated = true 201 it.Unlock() 202 wg.Done() 203 204 return it 205 } 206 207 func (it *Hashmap) AddOrUpdateStringsPtr( 208 keys, values *[]string, 209 ) *Hashmap { 210 if keys == nil || values == nil { 211 return it 212 } 213 214 for i, key := range *keys { 215 it.items[key] = (*values)[i] 216 } 217 218 it.hasMapUpdated = true 219 220 return it 221 } 222 223 func (it *Hashmap) AddOrUpdateStringsPtrLock( 224 keys, values *[]string, 225 ) *Hashmap { 226 if keys == nil || values == nil { 227 return it 228 } 229 230 it.Lock() 231 for i, key := range *keys { 232 it.items[key] = (*values)[i] 233 } 234 235 it.hasMapUpdated = true 236 it.Unlock() 237 238 return it 239 } 240 241 func (it *Hashmap) AddOrUpdateHashmap( 242 hashmap2 *Hashmap, 243 ) *Hashmap { 244 if hashmap2 == nil { 245 return it 246 } 247 248 for key, val := range hashmap2.items { 249 it.items[key] = val 250 } 251 252 it.hasMapUpdated = true 253 254 return it 255 } 256 257 func (it *Hashmap) AddOrUpdateMap( 258 itemsMap map[string]string, 259 ) *Hashmap { 260 if len(itemsMap) == 0 { 261 return it 262 } 263 264 for key, val := range itemsMap { 265 it.items[key] = val 266 } 267 268 it.hasMapUpdated = true 269 270 return it 271 } 272 273 func (it *Hashmap) AddOrUpdateMapPtr( 274 itemsMap *map[string]string, 275 ) *Hashmap { 276 if itemsMap == nil || len(*itemsMap) == 0 { 277 return it 278 } 279 280 for key, val := range *itemsMap { 281 it.items[key] = val 282 } 283 284 it.hasMapUpdated = true 285 286 return it 287 } 288 289 func (it *Hashmap) AddsOrUpdates( 290 KeyValuePair ...KeyValuePair, 291 ) *Hashmap { 292 if KeyValuePair == nil { 293 return it 294 } 295 296 for _, keyVal := range KeyValuePair { 297 it.items[keyVal.Key] = keyVal.Value 298 } 299 300 it.hasMapUpdated = true 301 302 return it 303 } 304 305 func (it *Hashmap) AddOrUpdateKeyAnyValsPtr( 306 pairs *[]KeyAnyValuePair, 307 ) *Hashmap { 308 if pairs == nil || *pairs == nil { 309 return it 310 } 311 312 for _, pair := range *pairs { 313 it.items[pair.Key] = pair.ValueString() 314 } 315 316 it.hasMapUpdated = true 317 318 return it 319 } 320 321 func (it *Hashmap) AddOrUpdateKeyValsPtr( 322 pairs *[]KeyValuePair, 323 ) *Hashmap { 324 if pairs == nil || *pairs == nil { 325 return it 326 } 327 328 for _, pair := range *pairs { 329 it.items[pair.Key] = pair.Value 330 } 331 332 it.hasMapUpdated = true 333 334 return it 335 } 336 337 func (it *Hashmap) AddOrUpdateCollection( 338 keys, values *Collection, 339 ) *Hashmap { 340 if (keys == nil || keys.IsEmpty()) || (values == nil || values.IsEmpty()) { 341 return it 342 } 343 344 for i, element := range keys.items { 345 it.items[element] = values.items[i] 346 } 347 348 it.hasMapUpdated = true 349 350 return it 351 } 352 353 // AddsOrUpdatesAnyUsingFilter Keep result from filter. 354 func (it *Hashmap) AddsOrUpdatesAnyUsingFilter( 355 filter IsKeyAnyValueFilter, 356 pairs ...KeyAnyValuePair, 357 ) *Hashmap { 358 if pairs == nil { 359 return it 360 } 361 362 for _, pair := range pairs { 363 result, isKeep, isBreak := filter(pair) 364 365 if isKeep { 366 it.items[pair.Key] = result 367 it.hasMapUpdated = true 368 } 369 370 if isBreak { 371 return it 372 } 373 } 374 375 return it 376 } 377 378 // AddsOrUpdatesAnyUsingFilterLock Keep result from filter. 379 func (it *Hashmap) AddsOrUpdatesAnyUsingFilterLock( 380 filter IsKeyAnyValueFilter, 381 pairs ...KeyAnyValuePair, 382 ) *Hashmap { 383 if pairs == nil { 384 return it 385 } 386 387 for _, pair := range pairs { 388 result, isKeep, isBreak := filter(pair) 389 390 if isKeep { 391 it.Lock() 392 it.items[pair.Key] = result 393 it.Unlock() 394 395 it.hasMapUpdated = true 396 } 397 398 if isBreak { 399 return it 400 } 401 } 402 403 return it 404 } 405 406 func (it *Hashmap) AddsOrUpdatesUsingFilter( 407 filter IsKeyValueFilter, 408 pairs ...KeyValuePair, 409 ) *Hashmap { 410 if pairs == nil { 411 return it 412 } 413 414 for _, pair := range pairs { 415 result, isKeep, isBreak := filter(pair) 416 417 if isKeep { 418 it.items[pair.Key] = result 419 it.hasMapUpdated = true 420 } 421 422 if isBreak { 423 return it 424 } 425 } 426 427 return it 428 } 429 430 func (it *Hashmap) ConcatNew( 431 isCloneOnEmptyAsWell bool, 432 hashmaps ...*Hashmap, 433 ) *Hashmap { 434 if len(hashmaps) == 0 { 435 return New.Hashmap.UsingMapOptions( 436 isCloneOnEmptyAsWell, 437 constants.Zero, 438 it.items, 439 ) 440 } 441 442 length := it.Length() + constants.Capacity2 443 444 for _, h := range hashmaps { 445 if h == nil { 446 continue 447 } 448 449 length += h.length 450 } 451 452 newHashmap := New.Hashmap.UsingMapOptions( 453 true, 454 length, 455 it.items, 456 ) 457 458 newHashmap.AddOrUpdateHashmap(it) 459 460 for _, hashmap2 := range hashmaps { 461 newHashmap.AddOrUpdateHashmap( 462 hashmap2) 463 } 464 465 return newHashmap 466 } 467 468 func (it *Hashmap) ConcatNewUsingMaps( 469 isCloneOnEmptyAsWell bool, 470 hashmaps ...*map[string]string, 471 ) *Hashmap { 472 if len(hashmaps) == 0 { 473 return New.Hashmap.UsingMapOptions( 474 isCloneOnEmptyAsWell, 475 constants.Zero, 476 it.items, 477 ) 478 } 479 480 length := it.Length() + 481 constants.Capacity5 482 for _, h := range hashmaps { 483 if h == nil { 484 continue 485 } 486 487 length += len(*h) 488 } 489 490 newHashmap := New.Hashmap.UsingMapOptions( 491 true, 492 length, 493 it.items, 494 ) 495 496 newHashmap.AddOrUpdateHashmap(it) 497 498 for _, hashmap2 := range hashmaps { 499 newHashmap.AddOrUpdateMapPtr( 500 hashmap2) 501 } 502 503 return newHashmap 504 } 505 506 func (it *Hashmap) AddOrUpdateLock(key, value string) *Hashmap { 507 it.Lock() 508 defer it.Unlock() 509 510 it.items[key] = value 511 it.hasMapUpdated = true 512 513 return it 514 } 515 516 func (it *Hashmap) Has(key string) bool { 517 _, isFound := it.items[key] 518 519 return isFound 520 } 521 522 func (it *Hashmap) IsKeyMissing(key string) bool { 523 _, isFound := it.items[key] 524 525 return !isFound 526 } 527 528 func (it *Hashmap) IsKeyMissingLock(key string) bool { 529 it.Lock() 530 _, isFound := it.items[key] 531 it.Unlock() 532 533 return !isFound 534 } 535 536 func (it *Hashmap) HasLock(key string) bool { 537 it.Lock() 538 _, isFound := it.items[key] 539 it.Unlock() 540 541 return isFound 542 } 543 544 func (it *Hashmap) HasAllStringsPtr(keys *[]string) bool { 545 for _, key := range *keys { 546 _, isFound := it.items[key] 547 548 if !isFound { 549 // not found 550 return false 551 } 552 } 553 554 // all found. 555 return true 556 } 557 558 func (it *Hashmap) DiffRaw( 559 rightMap map[string]string, 560 ) map[string]string { 561 mapDiffer := mapdiffinternal.HashmapDiff(rightMap) 562 563 return mapDiffer.DiffRaw(rightMap) 564 } 565 566 func (it *Hashmap) Diff( 567 rightMap *Hashmap, 568 ) *Hashmap { 569 rawMap := it.DiffRaw(rightMap.Items()) 570 571 return New.Hashmap.UsingMap(rawMap) 572 } 573 574 // HasAllCollectionItems return false on items is nil or Empty. 575 func (it *Hashmap) HasAllCollectionItems( 576 collection *Collection, 577 ) bool { 578 if collection == nil || collection.IsEmpty() { 579 return false 580 } 581 582 return it.HasAllStringsPtr(collection.ListPtr()) 583 } 584 585 func (it *Hashmap) HasAll(keys ...string) bool { 586 for _, key := range keys { 587 _, isFound := it.items[key] 588 589 if !isFound { 590 // not found 591 return false 592 } 593 } 594 595 // all found. 596 return true 597 } 598 599 func (it *Hashmap) HasAnyItem() bool { 600 return it != nil && it.Length() > 0 601 } 602 603 func (it *Hashmap) HasAny(keys ...string) bool { 604 for _, key := range keys { 605 _, isFound := it.items[key] 606 607 if isFound { 608 // any found 609 return true 610 } 611 } 612 613 // all not found. 614 return false 615 } 616 617 func (it *Hashmap) HasWithLock(key string) bool { 618 it.Lock() 619 defer it.Unlock() 620 621 _, isFound := it.items[key] 622 623 return isFound 624 } 625 626 // GetKeysFilteredItems must return slice. 627 func (it *Hashmap) GetKeysFilteredItems( 628 filter IsStringFilter, 629 ) *[]string { 630 if it.IsEmpty() { 631 return &([]string{}) 632 } 633 634 filteredList := make( 635 []string, 636 0, 637 it.Length()) 638 639 i := 0 640 for key := range it.items { 641 result, isKeep, isBreak := 642 filter(key, i) 643 644 i++ 645 if !isKeep { 646 continue 647 } 648 649 filteredList = append( 650 filteredList, 651 result) 652 653 if isBreak { 654 return &filteredList 655 } 656 } 657 658 return &filteredList 659 } 660 661 // GetKeysFilteredCollection must return items. 662 func (it *Hashmap) GetKeysFilteredCollection( 663 filter IsStringFilter, 664 ) *Collection { 665 if it.IsEmpty() { 666 return Empty.Collection() 667 } 668 669 filteredList := make( 670 []string, 671 0, 672 it.Length()) 673 674 i := 0 675 for key := range it.items { 676 result, isKeep, isBreak := filter(key, i) 677 i++ 678 679 if !isKeep { 680 continue 681 } 682 683 filteredList = append( 684 filteredList, 685 result) 686 687 if isBreak { 688 return New.Collection.StringsOptions( 689 false, filteredList) 690 } 691 } 692 693 return New.Collection.StringsOptions( 694 false, filteredList) 695 } 696 697 func (it *Hashmap) Items() map[string]string { 698 return it.items 699 } 700 701 func (it *Hashmap) SafeItems() map[string]string { 702 if it == nil { 703 return nil 704 } 705 706 return it.items 707 } 708 709 //goland:noinspection GoLinterLocal 710 func (it *Hashmap) ItemsCopyLock() *map[string]string { 711 it.Lock() 712 713 copiedItemsMap := &it.items 714 715 it.Unlock() 716 717 return copiedItemsMap 718 } 719 720 func (it *Hashmap) ValuesCollection() *Collection { 721 return New.Collection.StringsOptions( 722 false, it.ValuesList()) 723 } 724 725 func (it *Hashmap) ValuesHashset() *Hashset { 726 return New.Hashset.StringsPtr( 727 it.ValuesListPtr()) 728 } 729 730 func (it *Hashmap) ValuesCollectionLock() *Collection { 731 return New.Collection.StringsOptions( 732 false, *it.ValuesListCopyPtrLock()) 733 } 734 735 func (it *Hashmap) ValuesHashsetLock() *Hashset { 736 return New.Hashset.StringsPtr( 737 it.ValuesListCopyPtrLock()) 738 } 739 740 func (it *Hashmap) ValuesList() []string { 741 return *it.ValuesListPtr() 742 } 743 744 func (it *Hashmap) ValuesListPtr() *[]string { 745 if it.hasMapUpdated || it.cachedList == nil { 746 it.setCached() 747 } 748 749 return &it.cachedList 750 } 751 752 func (it *Hashmap) KeysValuesCollection() ( 753 keys, values *Collection, 754 ) { 755 wg := sync.WaitGroup{} 756 wg.Add(2) 757 758 go func() { 759 keys = New.Collection.Strings( 760 it.Keys(), 761 ) 762 763 wg.Done() 764 }() 765 766 go func() { 767 values = New.Collection.StringsPtr( 768 it.ValuesListPtr(), 769 ) 770 771 wg.Done() 772 }() 773 774 wg.Wait() 775 776 return keys, values 777 } 778 779 func (it *Hashmap) KeysValuesList() ( 780 keys, values []string, 781 ) { 782 wg := sync.WaitGroup{} 783 wg.Add(2) 784 785 go func() { 786 keys = it.Keys() 787 wg.Done() 788 }() 789 790 go func() { 791 values = it.ValuesList() 792 wg.Done() 793 }() 794 795 wg.Wait() 796 797 return keys, values 798 } 799 800 func (it *Hashmap) KeysValuePairs() []*KeyValuePair { 801 pairs := make([]*KeyValuePair, it.Length()) 802 803 i := 0 804 for k, v := range it.items { 805 pairs[i] = &KeyValuePair{ 806 Key: k, 807 Value: v, 808 } 809 810 i++ 811 } 812 813 return pairs 814 } 815 func (it *Hashmap) KeysValuePairsCollection() *KeyValueCollection { 816 pairs := New.KeyValues.Cap(it.Length()) 817 818 for k, v := range it.items { 819 pairs.Add(k, v) 820 } 821 822 return pairs 823 } 824 825 func (it *Hashmap) KeysValuesListLock() ( 826 keys, values []string, 827 ) { 828 it.Lock() 829 wg := sync.WaitGroup{} 830 wg.Add(2) 831 832 go func() { 833 keys = it.Keys() 834 wg.Done() 835 }() 836 go func() { 837 values = it.ValuesList() 838 wg.Done() 839 }() 840 841 wg.Wait() 842 it.Unlock() 843 844 return keys, values 845 } 846 847 func (it *Hashmap) AllKeys() []string { 848 length := len(it.items) 849 keys := make([]string, length) 850 851 if length == 0 { 852 return keys 853 } 854 855 i := 0 856 for k := range it.items { 857 keys[i] = k 858 i++ 859 } 860 861 return keys 862 } 863 864 func (it *Hashmap) Keys() []string { 865 return it.AllKeys() 866 } 867 868 func (it *Hashmap) KeysCollection() *Collection { 869 return New.Collection.Strings( 870 it.Keys(), 871 ) 872 } 873 874 func (it *Hashmap) KeysLock() []string { 875 length := it.LengthLock() 876 keys := make([]string, length) 877 878 if length == 0 { 879 return keys 880 } 881 882 i := 0 883 it.Lock() 884 for k := range it.items { 885 keys[i] = k 886 i++ 887 } 888 889 it.Unlock() 890 891 return keys 892 } 893 894 // ValuesListCopyPtrLock 895 // 896 // a slice must be returned 897 func (it *Hashmap) ValuesListCopyPtrLock() *[]string { 898 it.Lock() 899 defer it.Unlock() 900 901 return &(*it.ValuesListPtr()) 902 } 903 904 func (it *Hashmap) setCached() { 905 length := it.Length() 906 list := make([]string, length) 907 908 if length == 0 { 909 it.cachedList = list 910 it.hasMapUpdated = false 911 912 return 913 } 914 915 i := 0 916 917 for _, val := range it.items { 918 list[i] = val 919 i++ 920 } 921 922 it.hasMapUpdated = false 923 it.cachedList = list 924 } 925 926 // ValuesToLower CreateUsingAliasMap a new items with all lower strings 927 func (it *Hashmap) ValuesToLower() *Hashmap { 928 newMap := make(map[string]string, it.Length()) 929 930 var toLower string 931 for key, value := range it.items { 932 toLower = strings.ToLower(key) 933 newMap[toLower] = value 934 } 935 936 return New.Hashmap.UsingMapOptions( 937 false, 938 0, 939 newMap, 940 ) 941 } 942 943 func (it *Hashmap) Length() int { 944 if it == nil { 945 return 0 946 } 947 948 if it.hasMapUpdated || it.length < 0 { 949 it.length = len(it.items) 950 } 951 952 return it.length 953 } 954 955 func (it *Hashmap) LengthLock() int { 956 it.Lock() 957 defer it.Unlock() 958 959 return it.Length() 960 } 961 962 //goland:noinspection GoLinterLocal,GoVetCopyLock 963 func (it *Hashmap) IsEqual(another Hashmap) bool { //nolint:govet 964 return it.IsEqualPtr(&another) 965 } 966 967 func (it *Hashmap) IsEqualPtrLock(another *Hashmap) bool { 968 it.Lock() 969 defer it.Unlock() 970 971 return it.IsEqualPtr(another) 972 } 973 974 func (it *Hashmap) IsEqualPtr(another *Hashmap) bool { 975 if it == nil && another == nil { 976 return true 977 } 978 979 if it == nil || another == nil { 980 return false 981 } 982 983 if it == another { 984 // ptr same 985 return true 986 } 987 988 if it.IsEmpty() && another.IsEmpty() { 989 return true 990 } 991 992 if it.IsEmpty() || another.IsEmpty() { 993 return false 994 } 995 996 leftLength := it.Length() 997 rightLength := another.Length() 998 999 if leftLength != rightLength { 1000 return false 1001 } 1002 1003 for key, value := range it.items { 1004 result, has := another.items[key] 1005 1006 if !has || !(result != value) { 1007 return false 1008 } 1009 } 1010 1011 return true 1012 } 1013 1014 func (it *Hashmap) Remove(key string) *Hashmap { 1015 delete(it.items, key) 1016 it.hasMapUpdated = true 1017 1018 return it 1019 } 1020 1021 func (it *Hashmap) RemoveWithLock(key string) *Hashmap { 1022 it.Lock() 1023 defer it.Unlock() 1024 1025 it.Remove(key) 1026 1027 return it 1028 } 1029 1030 func (it *Hashmap) String() string { 1031 if it.IsEmpty() { 1032 return commonJoiner + NoElements 1033 } 1034 1035 return commonJoiner + 1036 strings.Join( 1037 it.ValuesList(), 1038 commonJoiner) 1039 } 1040 1041 func (it *Hashmap) StringLock() string { 1042 if it.IsEmptyLock() { 1043 return commonJoiner + NoElements 1044 } 1045 1046 it.Lock() 1047 defer it.Unlock() 1048 1049 return commonJoiner + 1050 strings.Join( 1051 *it.ValuesListPtr(), 1052 commonJoiner) 1053 } 1054 1055 // GetValuesExceptKeysInHashset Get all Collection except the mentioned ones. 1056 // Always returns a copy of new strings. 1057 // It is like set A - B 1058 // Set A = this Hashmap 1059 // Set B = anotherHashset given in parameters. 1060 func (it *Hashmap) GetValuesExceptKeysInHashset( 1061 anotherHashset *Hashset, 1062 ) *[]string { 1063 if anotherHashset == nil || anotherHashset.IsEmpty() { 1064 return it.ValuesListPtr() 1065 } 1066 1067 finalList := make( 1068 []string, 1069 0, 1070 it.Length()) 1071 1072 for key, value := range it.items { 1073 if anotherHashset.Has(key) { 1074 continue 1075 } 1076 1077 finalList = append( 1078 finalList, 1079 value) 1080 } 1081 1082 return &finalList 1083 } 1084 1085 // GetValuesKeysExcept Get all items except the mentioned ones. 1086 // Always returns a copy of new strings. 1087 // It is like set A - B 1088 // Set A = this Hashmap 1089 // Set B = items given in parameters. 1090 func (it *Hashmap) GetValuesKeysExcept( 1091 items *[]string, 1092 ) *[]string { 1093 if items == nil { 1094 return it.ValuesListPtr() 1095 } 1096 1097 newCollection := New.Hashset.StringsPtr( 1098 items) 1099 1100 return it.GetValuesExceptKeysInHashset( 1101 newCollection) 1102 } 1103 1104 // GetAllExceptCollection Get all Hashmap items except the mentioned ones in collection. 1105 // Always returns a copy of new strings. 1106 // It is like set A - B 1107 // Set A = this Hashmap 1108 // Set B = collection given in parameters. 1109 func (it *Hashmap) GetAllExceptCollection( 1110 collection *Collection, 1111 ) *[]string { 1112 if collection == nil { 1113 return it.ValuesListPtr() 1114 } 1115 1116 return it.GetValuesExceptKeysInHashset( 1117 collection.HashsetAsIs()) 1118 } 1119 1120 // GetAllExceptCollectionPtr Get all items except the mentioned ones in collectionPtr. 1121 // Always returns a copy of new strings. 1122 // It is like set A - B 1123 // Set A = this Hashmap 1124 // Set B = collectionPtr given in parameters. 1125 func (it *Hashmap) GetAllExceptCollectionPtr( 1126 collectionPtr *CollectionPtr, 1127 ) *[]string { 1128 if collectionPtr == nil { 1129 return it.ValuesListPtr() 1130 } 1131 1132 return it.GetValuesExceptKeysInHashset( 1133 collectionPtr.HashsetAsIs()) 1134 } 1135 1136 // Join values 1137 func (it *Hashmap) Join( 1138 separator string, 1139 ) string { 1140 return strings.Join(*it.ValuesListPtr(), separator) 1141 } 1142 1143 func (it *Hashmap) JoinKeys( 1144 separator string, 1145 ) string { 1146 return strings.Join(it.Keys(), separator) 1147 } 1148 1149 func (it *Hashmap) JsonModel() map[string]string { 1150 return it.items 1151 } 1152 1153 func (it *Hashmap) JsonModelAny() interface{} { 1154 return it.JsonModel() 1155 } 1156 1157 func (it *Hashmap) MarshalJSON() ([]byte, error) { 1158 return json.Marshal(it.JsonModel()) 1159 } 1160 1161 func (it *Hashmap) UnmarshalJSON(data []byte) error { 1162 var dataModelItems map[string]string 1163 err := json.Unmarshal(data, &dataModelItems) 1164 1165 if err == nil { 1166 it.items = dataModelItems 1167 it.length = len(it.items) 1168 it.hasMapUpdated = false 1169 it.isEmptySet = it.length == 0 1170 it.cachedList = nil 1171 } 1172 1173 return err 1174 } 1175 1176 func (it Hashmap) Json() corejson.Result { 1177 return corejson.New(it) 1178 } 1179 1180 func (it Hashmap) JsonPtr() *corejson.Result { 1181 return corejson.NewPtr(it) 1182 } 1183 1184 // ParseInjectUsingJson It will not update the self but creates a new one. 1185 func (it *Hashmap) ParseInjectUsingJson( 1186 jsonResult *corejson.Result, 1187 ) (*Hashmap, error) { 1188 err := jsonResult.Unmarshal(it) 1189 1190 if err != nil { 1191 return Empty.Hashmap(), err 1192 } 1193 1194 return it, nil 1195 } 1196 1197 // ParseInjectUsingJsonMust Panic if error 1198 func (it *Hashmap) ParseInjectUsingJsonMust( 1199 jsonResult *corejson.Result, 1200 ) *Hashmap { 1201 hashSet, err := it.ParseInjectUsingJson(jsonResult) 1202 1203 if err != nil { 1204 panic(err) 1205 } 1206 1207 return hashSet 1208 } 1209 1210 func (it *Hashmap) ToError(sep string) error { 1211 return errcore.SliceError(sep, it.KeyValStringLines()) 1212 } 1213 1214 func (it *Hashmap) ToDefaultError() error { 1215 return errcore.SliceError( 1216 constants.NewLineUnix, it.KeyValStringLines()) 1217 } 1218 1219 func (it *Hashmap) KeyValStringLines() *[]string { 1220 return it.ToStringsUsingCompiler(func(key, val string) string { 1221 return key + constants.HyphenAngelRight + val 1222 }) 1223 } 1224 1225 func (it *Hashmap) Clear() *Hashmap { 1226 if it == nil { 1227 return it 1228 } 1229 1230 it.items = nil 1231 it.items = map[string]string{} 1232 it.cachedList = it.cachedList[:0] 1233 it.hasMapUpdated = true 1234 1235 return it 1236 } 1237 1238 func (it *Hashmap) Dispose() { 1239 if it == nil { 1240 return 1241 } 1242 1243 it.items = nil 1244 it.cachedList = nil 1245 } 1246 1247 func (it *Hashmap) ToStringsUsingCompiler( 1248 compilerFunc func( 1249 key, 1250 val string, 1251 ) string, 1252 ) *[]string { 1253 length := it.Length() 1254 slice := make([]string, length) 1255 1256 if length == 0 { 1257 return &slice 1258 } 1259 1260 index := 0 1261 for key, val := range it.items { 1262 line := compilerFunc(key, val) 1263 slice[index] = line 1264 1265 index++ 1266 } 1267 1268 return &slice 1269 } 1270 1271 func (it *Hashmap) AsJsoner() corejson.Jsoner { 1272 return it 1273 } 1274 1275 func (it *Hashmap) JsonParseSelfInject( 1276 jsonResult *corejson.Result, 1277 ) error { 1278 _, err := it.ParseInjectUsingJson( 1279 jsonResult, 1280 ) 1281 1282 return err 1283 } 1284 1285 func (it *Hashmap) AsJsonContractsBinder() corejson.JsonContractsBinder { 1286 return it 1287 } 1288 1289 func (it *Hashmap) AsJsonParseSelfInjector() corejson.JsonParseSelfInjector { 1290 return it 1291 } 1292 1293 func (it *Hashmap) AsJsonMarshaller() corejson.JsonMarshaller { 1294 return it 1295 } 1296 1297 func (it *Hashmap) ClonePtr() *Hashmap { 1298 if it == nil { 1299 return nil 1300 } 1301 1302 cloned := it.Clone() 1303 1304 return &cloned 1305 } 1306 1307 func (it Hashmap) Clone() Hashmap { 1308 empty := Empty.Hashmap() 1309 jsonResult := it.JsonPtr() 1310 1311 return *empty.ParseInjectUsingJsonMust(jsonResult) 1312 } 1313 1314 func (it *Hashmap) Get(key string) (val string, isFound bool) { 1315 val, isFound = it.items[key] 1316 1317 return val, isFound 1318 } 1319 1320 func (it *Hashmap) Serialize() ([]byte, error) { 1321 return corejson.Serialize.Raw(it) 1322 } 1323 1324 func (it *Hashmap) Deserialize(toPtr interface{}) (parsingErr error) { 1325 return it.JsonPtr().Deserialize(toPtr) 1326 }