gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/LinkedList.go (about) 1 package corestr 2 3 import ( 4 "encoding/json" 5 "fmt" 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/coreindexes" 12 "gitlab.com/evatix-go/core/errcore" 13 "gitlab.com/evatix-go/core/internal/strutilinternal" 14 ) 15 16 type LinkedList struct { 17 head, tail *LinkedListNode 18 length int 19 sync.Mutex 20 } 21 22 func (it *LinkedList) Tail() *LinkedListNode { 23 return it.tail 24 } 25 26 func (it *LinkedList) Head() *LinkedListNode { 27 return it.head 28 } 29 30 func (it *LinkedList) Length() int { 31 return it.length 32 } 33 34 func (it *LinkedList) incrementLength() int { 35 it.length++ 36 37 return it.length 38 } 39 40 func (it *LinkedList) incrementLengthUsingNumber(number int) int { 41 it.length += number 42 43 return it.length 44 } 45 46 func (it *LinkedList) setLengthToZero() int { 47 it.length = 0 48 49 return it.length 50 } 51 52 func (it *LinkedList) setLength(number int) int { 53 it.length = number 54 55 return it.length 56 } 57 58 func (it *LinkedList) decrementLength() int { 59 it.length-- 60 61 return it.length 62 } 63 64 func (it *LinkedList) LengthLock() int { 65 it.Lock() 66 defer it.Unlock() 67 68 return it.length 69 } 70 71 //goland:noinspection GoVetCopyLock 72 func (it *LinkedList) IsEquals( 73 anotherLinkedList LinkedList, 74 ) bool { 75 return it.IsEqualsWithSensitivePtr( 76 &anotherLinkedList, 77 true) 78 } 79 80 func (it *LinkedList) IsEqualsPtr( 81 anotherLinkedList *LinkedList, 82 ) bool { 83 return it.IsEqualsWithSensitivePtr( 84 anotherLinkedList, 85 true) 86 } 87 88 func (it *LinkedList) IsEqualsWithSensitivePtr( 89 anotherLinkedList *LinkedList, 90 isCaseSensitive bool, 91 ) bool { 92 if anotherLinkedList == nil && it == nil { 93 return true 94 } 95 96 if anotherLinkedList == nil || it == nil { 97 return false 98 } 99 100 if it == anotherLinkedList { 101 return true 102 } 103 104 if it.IsEmpty() && anotherLinkedList.IsEmpty() { 105 return true 106 } 107 108 if it.IsEmpty() || anotherLinkedList.IsEmpty() { 109 return false 110 } 111 112 if it.Length() != anotherLinkedList.Length() { 113 return false 114 } 115 116 leftNode := it.head 117 rightNode := anotherLinkedList.head 118 119 if leftNode == nil && rightNode == nil { 120 return true 121 } 122 123 if leftNode == nil || rightNode == nil { 124 return false 125 } 126 127 return leftNode.IsChainEqual(rightNode, isCaseSensitive) 128 } 129 130 func (it *LinkedList) IsEmptyLock() bool { 131 it.Lock() 132 defer it.Unlock() 133 134 return it.head == nil || it.length == 0 135 } 136 137 func (it *LinkedList) IsEmpty() bool { 138 return it.head == nil || 139 it.length == 0 140 } 141 142 func (it *LinkedList) HasItems() bool { 143 return it.head != nil && 144 it.length > 0 145 } 146 147 func (it *LinkedList) Add(item string) *LinkedList { 148 if it.IsEmpty() { 149 it.head = &LinkedListNode{ 150 Element: item, 151 next: nil, 152 } 153 154 it.tail = it.head 155 it.incrementLength() 156 157 return it 158 } 159 160 it.tail.next = &LinkedListNode{ 161 Element: item, 162 next: nil, 163 } 164 165 it.tail = it.tail.next 166 it.incrementLength() 167 168 return it 169 } 170 171 func (it *LinkedList) AddItemsMap(itemsMap map[string]bool) *LinkedList { 172 if len(itemsMap) == 0 { 173 return it 174 } 175 176 for key, isAdd := range itemsMap { 177 if !isAdd { 178 continue 179 } 180 181 it.Add(key) 182 } 183 184 return it 185 } 186 187 func (it *LinkedList) AddLock(item string) *LinkedList { 188 it.Lock() 189 defer it.Unlock() 190 191 return it.Add(item) 192 } 193 194 // InsertAt BigO(n) expensive operation. 195 func (it *LinkedList) InsertAt(index int, item string) *LinkedList { 196 if index < 1 { 197 return it.AddFront(item) 198 } 199 200 node := it.IndexAt(index - 1) 201 it.AddAfterNode(node, item) 202 203 return it 204 } 205 206 func (it *LinkedList) AddBackNode(node *LinkedListNode) *LinkedList { 207 return it.AppendNode(node) 208 } 209 210 func (it *LinkedList) AppendNode(node *LinkedListNode) *LinkedList { 211 if it.IsEmpty() { 212 it.head = node 213 it.tail = it.head 214 it.incrementLength() 215 216 return it 217 } 218 219 it.tail.next = node 220 it.tail = it.tail.next 221 it.incrementLength() 222 223 return it 224 } 225 226 func (it *LinkedList) AppendChainOfNodes(nodeHead *LinkedListNode) *LinkedList { 227 endOfChain, length := nodeHead.EndOfChain() 228 229 if it.IsEmpty() { 230 it.head = nodeHead 231 } else { 232 it.tail.next = nodeHead 233 } 234 235 it.tail = endOfChain 236 it.incrementLengthUsingNumber(length) 237 238 return it 239 } 240 241 func (it *LinkedList) PushBack(item string) *LinkedList { 242 return it.Add(item) 243 } 244 245 func (it *LinkedList) AddNonEmpty(item string) *LinkedList { 246 if item == "" { 247 return it 248 } 249 250 return it.Add(item) 251 } 252 253 func (it *LinkedList) AddNonEmptyWhitespace(item string) *LinkedList { 254 if strutilinternal.IsEmptyOrWhitespace(item) { 255 return it 256 } 257 258 return it.Add(item) 259 } 260 261 func (it *LinkedList) AddIf(isAdd bool, item string) *LinkedList { 262 if !isAdd { 263 return it 264 } 265 266 return it.Add(item) 267 } 268 269 func (it *LinkedList) AddIfMany( 270 isAdd bool, 271 addingStrings ...string, 272 ) *LinkedList { 273 if !isAdd { 274 return it 275 } 276 277 return it.AddStringsPtr(&addingStrings) 278 } 279 280 func (it *LinkedList) AddFunc(f func() string) *LinkedList { 281 return it.Add(f()) 282 } 283 284 func (it *LinkedList) AddFuncErr( 285 funcReturnsStringError func() (result string, err error), 286 errHandler func(errInput error), 287 ) *LinkedList { 288 r, err := funcReturnsStringError() 289 290 if err != nil { 291 errHandler(err) 292 293 return it 294 } 295 296 return it.Add(r) 297 } 298 299 func (it *LinkedList) Push(item string) *LinkedList { 300 return it.Add(item) 301 } 302 303 func (it *LinkedList) PushFront(item string) *LinkedList { 304 return it.AddFront(item) 305 } 306 307 func (it *LinkedList) AddFront(item string) *LinkedList { 308 if it.IsEmpty() { 309 return it.Add(item) 310 } 311 312 node := &LinkedListNode{ 313 Element: item, 314 next: it.head, 315 } 316 317 it.head = node 318 it.incrementLength() 319 320 return it 321 } 322 323 func (it *LinkedList) AttachWithNode(currentNode, addingNode *LinkedListNode) error { 324 if currentNode == nil { 325 return errcore. 326 CannotBeNilType. 327 Error("CurrentNode cannot be nil.", nil) 328 } 329 330 if currentNode.next != nil { 331 return errcore. 332 ShouldBeNilType. 333 Error("CurrentNode.next", nil) 334 } 335 336 addingNode.next = currentNode.next 337 currentNode.next = addingNode 338 339 it.incrementLength() 340 341 return nil 342 } 343 344 // AddCollectionToNode iSkipOnNil 345 func (it *LinkedList) AddCollectionToNode( 346 isSkipOnNull bool, 347 node *LinkedListNode, 348 collection *Collection, 349 ) *LinkedList { 350 return it.AddStringsPtrToNode( 351 isSkipOnNull, 352 node, 353 collection.ListPtr()) 354 } 355 356 func (it *LinkedList) Loop( 357 simpleProcessor LinkedListSimpleProcessor, 358 ) *LinkedList { 359 length := it.Length() 360 if length == 0 { 361 return it 362 } 363 364 node := it.head 365 arg := &LinkedListProcessorParameter{ 366 Index: 0, 367 CurrentNode: node, 368 PrevNode: nil, 369 IsFirstIndex: true, 370 IsEndingIndex: false, 371 } 372 373 isBreak := simpleProcessor(arg) 374 375 if isBreak { 376 return it 377 } 378 379 lenMinusOne := length - 1 380 index := 1 381 isEndingIndex := false 382 383 for node.HasNext() { 384 prev := node 385 node = node.Next() 386 isEndingIndex = lenMinusOne == index 387 388 arg2 := &LinkedListProcessorParameter{ 389 Index: index, 390 CurrentNode: node, 391 PrevNode: prev, 392 IsFirstIndex: false, 393 IsEndingIndex: isEndingIndex, 394 } 395 396 isBreak2 := simpleProcessor(arg2) 397 398 if isBreak2 { 399 return it 400 } 401 402 index++ 403 } 404 405 return it 406 } 407 408 func (it *LinkedList) Filter( 409 filter LinkedListFilter, 410 ) *[]*LinkedListNode { 411 length := it.Length() 412 list := make([]*LinkedListNode, 0, length) 413 414 if length == 0 { 415 return &list 416 } 417 418 node := it.head 419 arg := &LinkedListFilterParameter{ 420 Node: node, 421 Index: 0, 422 } 423 result := filter(arg) 424 425 if result.IsKeep { 426 list = append(list, result.Value) 427 } 428 429 if result.IsBreak { 430 return &list 431 } 432 433 index := 1 434 435 for node.HasNext() { 436 node = node.Next() 437 438 arg2 := &LinkedListFilterParameter{ 439 Node: node, 440 Index: index, 441 } 442 result2 := filter(arg2) 443 444 if result2.IsKeep { 445 list = append(list, result2.Value) 446 } 447 448 if result2.IsBreak { 449 return &list 450 } 451 452 index++ 453 } 454 455 return &list 456 } 457 458 func (it *LinkedList) RemoveNodeByElementValue( 459 element string, 460 isCaseSensitive bool, 461 isIgnorePanic bool, 462 ) *LinkedList { 463 if !isIgnorePanic && it.IsEmpty() { 464 errcore. 465 CannotRemoveIndexesFromEmptyCollectionType. 466 HandleUsingPanic("element cannot be removed from Empty linkedlist.", element) 467 } 468 469 var processor LinkedListSimpleProcessor = func( 470 arg *LinkedListProcessorParameter, 471 ) (isBreak bool) { 472 isSameNode := 473 (isCaseSensitive && arg.CurrentNode.Element == element) || 474 (!isCaseSensitive && strings.EqualFold(element, arg.CurrentNode.Element)) 475 476 if isSameNode && arg.IsFirstIndex { 477 it.head = arg.CurrentNode.next 478 it.decrementLength() 479 480 return false 481 } 482 483 if isSameNode { 484 arg.PrevNode.next = arg.CurrentNode.next 485 it.decrementLength() 486 } 487 488 return false 489 } 490 491 return it.Loop(processor) 492 } 493 494 func (it *LinkedList) RemoveNodeByIndex( 495 removingIndex int, 496 ) *LinkedList { 497 if removingIndex < 0 { 498 errcore. 499 CannotBeNegativeIndexType. 500 HandleUsingPanic( 501 "removeIndex was less than 0.", 502 removingIndex) 503 } 504 505 var singleProcessor LinkedListSimpleProcessor = func( 506 arg *LinkedListProcessorParameter, 507 ) (isBreak bool) { 508 hasIndex := removingIndex == arg.Index 509 510 if !hasIndex { 511 return false 512 } 513 514 isBreak = hasIndex 515 it.decrementLength() 516 517 if arg.IsFirstIndex { 518 it.head = 519 arg.CurrentNode.next 520 arg.CurrentNode = nil 521 return isBreak 522 } 523 524 if arg.IsEndingIndex { 525 arg.PrevNode.next = nil 526 arg.CurrentNode = nil 527 528 return isBreak 529 } 530 531 arg.PrevNode.next = arg.CurrentNode.next 532 arg.CurrentNode = nil 533 534 return isBreak 535 } 536 537 return it.Loop(singleProcessor) 538 } 539 540 func (it *LinkedList) RemoveNodeByIndexes( 541 isIgnorePanic bool, 542 removingIndexes ...int, 543 ) *LinkedList { 544 length := len(removingIndexes) 545 546 if length == 0 { 547 return it 548 } 549 550 if !isIgnorePanic && it.IsEmpty() && length > 0 { 551 errcore. 552 CannotRemoveIndexesFromEmptyCollectionType. 553 HandleUsingPanic("removingIndexes cannot be removed from Empty linkedlist.", removingIndexes) 554 } 555 556 removingIndexesCopy := removingIndexes 557 558 nonChainedNodes := it.Filter( 559 func( 560 arg *LinkedListFilterParameter, 561 ) *LinkedListFilterResult { 562 hasIndex := coreindexes.HasIndexPlusRemoveIndex(removingIndexesCopy, arg.Index) 563 if hasIndex { 564 // remove 565 return &LinkedListFilterResult{ 566 Value: arg.Node, 567 IsKeep: false, 568 IsBreak: false, 569 } 570 } 571 572 // not remove 573 return &LinkedListFilterResult{ 574 Value: arg.Node, 575 IsKeep: true, 576 IsBreak: false, 577 } 578 }) 579 580 nonChainedCollection := &NonChainedLinkedListNodes{ 581 items: nonChainedNodes, 582 isChainingApplied: false, 583 } 584 585 if nonChainedCollection.IsEmpty() { 586 return it 587 } 588 589 it.setLength(nonChainedCollection.Length()) 590 it.head = nonChainedCollection.ApplyChaining().First() 591 592 return it 593 } 594 595 func (it *LinkedList) GetCompareSummary( 596 right *LinkedList, 597 leftName, rightName string, 598 ) string { 599 lLen := it.Length() 600 rLen := right.Length() 601 602 leftStr := fmt.Sprintf( 603 linkedListCollectionCompareHeaderLeft, 604 leftName, 605 lLen, 606 it) 607 608 rightStr := fmt.Sprintf( 609 linkedListCollectionCompareHeaderRight, 610 rightName, 611 rLen, 612 right, 613 it.IsEqualsPtr(right), 614 lLen, 615 rLen) 616 617 return leftStr + rightStr 618 } 619 620 // RemoveNode skip if removingNode is nil 621 func (it *LinkedList) RemoveNode( 622 removingNode *LinkedListNode, 623 ) *LinkedList { 624 if removingNode == nil { 625 return it 626 } 627 628 if it.IsEmpty() { 629 errcore. 630 CannotRemoveIndexesFromEmptyCollectionType. 631 HandleUsingPanic("removingNode cannot be removed from Empty linkedlist.", removingNode.String()) 632 } 633 634 var processor LinkedListSimpleProcessor = func( 635 arg *LinkedListProcessorParameter, 636 ) (isBreak bool) { 637 isSameNode := arg.CurrentNode == removingNode 638 if isSameNode && arg.IsFirstIndex { 639 it.head = arg.CurrentNode.next 640 it.decrementLength() 641 642 return true 643 } 644 645 if isSameNode { 646 arg.PrevNode.next = arg.CurrentNode.next 647 it.decrementLength() 648 649 return true 650 } 651 652 return false 653 } 654 655 return it.Loop(processor) 656 } 657 658 // AddStringsPtrToNode iSkipOnNil 659 func (it *LinkedList) AddStringsPtrToNode( 660 isSkipOnNull bool, 661 node *LinkedListNode, 662 items *[]string, 663 ) *LinkedList { 664 if items == nil || node == nil && isSkipOnNull { 665 return it 666 } 667 668 if node == nil { 669 errcore. 670 CannotBeNilType. 671 HandleUsingPanic( 672 "node cannot be nil.", 673 nil) 674 } 675 676 length := len(*items) 677 678 if length == 0 { 679 return it 680 } 681 682 if length == 1 { 683 it.AddAfterNode(node, (*items)[0]) 684 685 return it 686 } 687 688 finalHead := &LinkedListNode{ 689 Element: (*items)[0], 690 next: nil, 691 } 692 693 nextNode := finalHead 694 695 for _, item := range (*items)[1:] { 696 nextNode = nextNode.AddNext(it, item) 697 } 698 699 //goland:noinspection GoNilness 700 nextNode.next = node.next 701 //goland:noinspection GoNilness 702 node.next = finalHead 703 it.incrementLength() 704 705 return it 706 } 707 708 func (it *LinkedList) AddAfterNode( 709 node *LinkedListNode, 710 item string, 711 ) *LinkedListNode { 712 newNode := &LinkedListNode{ 713 Element: item, 714 next: node.next, 715 } 716 717 node.next = newNode 718 it.incrementLength() 719 720 return newNode 721 } 722 723 // AddStringsPtr add to back 724 func (it *LinkedList) AddStringsPtr(items *[]string) *LinkedList { 725 if items == nil { 726 return it 727 } 728 729 for _, item := range *items { 730 it.Add(item) 731 } 732 733 return it 734 } 735 736 // AddStrings items add to back 737 func (it *LinkedList) AddStrings(items ...string) *LinkedList { 738 if len(items) == 0 { 739 return it 740 } 741 742 for _, item := range items { 743 it.Add(item) 744 } 745 746 return it 747 } 748 749 // AddStringsPtrLock add to back 750 func (it *LinkedList) AddStringsPtrLock(items *[]string) *LinkedList { 751 it.Lock() 752 defer it.Unlock() 753 754 return it.AddStringsPtr(items) 755 } 756 757 // IndexAt Expensive operation BigO(n) 758 func (it *LinkedList) IndexAt(index int) *LinkedListNode { 759 length := it.Length() 760 if index < 0 { 761 return nil 762 } 763 764 if length == 0 || length-1 < index { 765 errcore.OutOfRangeType.HandleUsingPanic( 766 "Given index is out of range. Whereas length:", 767 length) 768 } 769 770 if index == 0 { 771 return it.head 772 } 773 774 node := it.head 775 i := 1 776 for node.HasNext() { 777 node = node.Next() 778 779 if i == index { 780 return node 781 } 782 783 i++ 784 } 785 786 return nil 787 } 788 789 // SafePointerIndexAt Expensive operation BigO(n) 790 func (it *LinkedList) SafePointerIndexAt(index int) *string { 791 node := it.SafeIndexAt(index) 792 793 if node == nil { 794 return nil 795 } 796 797 return &node.Element 798 } 799 800 // SafePointerIndexAtUsingDefault Expensive operation BigO(n) 801 func (it *LinkedList) SafePointerIndexAtUsingDefault( 802 index int, 803 defaultString string, 804 ) string { 805 node := it.SafeIndexAt(index) 806 807 if node == nil { 808 return defaultString 809 } 810 811 return node.Element 812 } 813 814 // SafeIndexAt Expensive operation BigO(n) 815 func (it *LinkedList) SafeIndexAt(index int) *LinkedListNode { 816 length := it.Length() 817 isExitCondition := index < 0 || length == 0 || length-1 < index 818 if isExitCondition { 819 return nil 820 } 821 822 if index == 0 { 823 return it.head 824 } 825 826 node := it.head 827 i := 1 828 for node.HasNext() { 829 node = node.Next() 830 831 if i == index { 832 return node 833 } 834 835 i++ 836 } 837 838 return nil 839 } 840 841 // SafeIndexAtLock Expensive operation BigO(n) 842 func (it *LinkedList) SafeIndexAtLock(index int) *LinkedListNode { 843 it.Lock() 844 defer it.Unlock() 845 846 return it.SafeIndexAt(index) 847 } 848 849 // SafePointerIndexAtUsingDefaultLock Expensive operation BigO(n) 850 func (it *LinkedList) SafePointerIndexAtUsingDefaultLock( 851 index int, 852 defaultString string, 853 ) string { 854 it.Lock() 855 defer it.Unlock() 856 857 return it.SafePointerIndexAtUsingDefault(index, defaultString) 858 } 859 860 func (it *LinkedList) GetNextNodes(count int) *[]*LinkedListNode { 861 counter := 0 862 863 return it.Filter( 864 func( 865 arg *LinkedListFilterParameter, 866 ) *LinkedListFilterResult { 867 isBreak := counter >= count-1 868 return &LinkedListFilterResult{ 869 Value: arg.Node, 870 IsKeep: true, 871 IsBreak: isBreak, 872 } 873 }) 874 } 875 876 func (it *LinkedList) GetAllLinkedNodes() *[]*LinkedListNode { 877 return it.Filter( 878 func( 879 arg *LinkedListFilterParameter, 880 ) *LinkedListFilterResult { 881 return &LinkedListFilterResult{ 882 Value: arg.Node, 883 IsKeep: true, 884 IsBreak: false, 885 } 886 }) 887 } 888 889 // AddPointerStringsPtr skip on nil, add to back 890 func (it *LinkedList) AddPointerStringsPtr(items *[]*string) *LinkedList { 891 if items == nil { 892 return it 893 } 894 895 for _, item := range *items { 896 if item == nil { 897 continue 898 } 899 900 it.Add(*item) 901 } 902 903 return it 904 } 905 906 // AddCollection skip on nil 907 func (it *LinkedList) AddCollection(collection *Collection) *LinkedList { 908 if collection == nil { 909 return it 910 } 911 912 for _, item := range collection.items { 913 it.Add(item) 914 } 915 916 return it 917 } 918 919 func (it *LinkedList) ToCollection(addCapacity int) *Collection { 920 newLength := it.Length() + addCapacity 921 collection := New.Collection.Cap(newLength) 922 923 if it.IsEmpty() { 924 return collection 925 } 926 927 node := it.head 928 collection.Add(node.Element) 929 930 for node.HasNext() { 931 node = node.Next() 932 collection.Add(node.Element) 933 } 934 935 return collection 936 } 937 938 // List must return slice. 939 func (it *LinkedList) List() []string { 940 list := make( 941 []string, 942 0, 943 it.Length()) 944 945 if it.IsEmpty() { 946 return list 947 } 948 949 node := it.head 950 list = append(list, node.Element) 951 952 for node.HasNext() { 953 node = node.Next() 954 list = append(list, node.Element) 955 } 956 957 return list 958 } 959 960 // ListPtr must return slice. 961 func (it *LinkedList) ListPtr() *[]string { 962 list := it.List() 963 964 return &list 965 } 966 967 // ListPtrLock must return slice. 968 func (it *LinkedList) ListPtrLock() *[]string { 969 it.Lock() 970 defer it.Unlock() 971 972 return it.ListPtr() 973 } 974 975 func (it *LinkedList) String() string { 976 if it.IsEmpty() { 977 return commonJoiner + NoElements 978 } 979 980 return commonJoiner + 981 strings.Join( 982 it.List(), 983 commonJoiner) 984 } 985 986 func (it *LinkedList) StringLock() string { 987 if it.IsEmptyLock() { 988 return commonJoiner + NoElements 989 } 990 991 it.Lock() 992 defer it.Unlock() 993 994 return commonJoiner + 995 strings.Join( 996 *it.ListPtr(), 997 commonJoiner) 998 } 999 1000 func (it *LinkedList) Join( 1001 separator string, 1002 ) string { 1003 return strings.Join(*it.ListPtr(), separator) 1004 } 1005 1006 func (it *LinkedList) JoinLock( 1007 separator string, 1008 ) string { 1009 it.Lock() 1010 defer it.Unlock() 1011 1012 return strings.Join(*it.ListPtr(), separator) 1013 } 1014 1015 func (it *LinkedList) Joins( 1016 separator string, 1017 items ...string, 1018 ) string { 1019 if items == nil || it.Length() == 0 { 1020 return strings.Join(items, separator) 1021 } 1022 1023 collection := it.ToCollection(len(items) + 1024 constants.ArbitraryCapacity2) 1025 collection.AddStringsPtr(&items) 1026 1027 return collection.Join(separator) 1028 } 1029 1030 func (it *LinkedList) JsonModel() []string { 1031 return it.ToCollection(0).JsonModel() 1032 } 1033 1034 func (it *LinkedList) JsonModelAny() interface{} { 1035 return it.JsonModel() 1036 } 1037 1038 func (it *LinkedList) MarshalJSON() ([]byte, error) { 1039 return json.Marshal(it.JsonModel()) 1040 } 1041 1042 func (it *LinkedList) UnmarshalJSON(data []byte) error { 1043 var dataModelStrings []string 1044 err := json.Unmarshal(data, &dataModelStrings) 1045 1046 if err == nil { 1047 it.Clear() 1048 it.AddStrings(dataModelStrings...) 1049 } 1050 1051 return err 1052 } 1053 1054 func (it *LinkedList) RemoveAll() *LinkedList { 1055 return it.Clear() 1056 } 1057 1058 func (it *LinkedList) Clear() *LinkedList { 1059 if it.IsEmpty() { 1060 return it 1061 } 1062 1063 it.head = nil 1064 it.tail = nil 1065 it.setLengthToZero() 1066 1067 return it 1068 } 1069 1070 func (it LinkedList) Json() corejson.Result { 1071 return corejson.New(it) 1072 } 1073 1074 func (it LinkedList) JsonPtr() *corejson.Result { 1075 return corejson.NewPtr(it) 1076 } 1077 1078 func (it *LinkedList) ParseInjectUsingJson( 1079 jsonResult *corejson.Result, 1080 ) (*LinkedList, error) { 1081 err := jsonResult.Unmarshal(it) 1082 1083 if err != nil { 1084 return New.LinkedList.Create(), err 1085 } 1086 1087 return it, nil 1088 } 1089 1090 // ParseInjectUsingJsonMust Panic if error 1091 func (it *LinkedList) ParseInjectUsingJsonMust( 1092 jsonResult *corejson.Result, 1093 ) *LinkedList { 1094 newUsingJson, err := 1095 it.ParseInjectUsingJson(jsonResult) 1096 1097 if err != nil { 1098 panic(err) 1099 } 1100 1101 return newUsingJson 1102 } 1103 1104 // JsonParseSelfInject Panic if error 1105 func (it *LinkedList) JsonParseSelfInject( 1106 jsonResult *corejson.Result, 1107 ) error { 1108 _, err := it.ParseInjectUsingJson( 1109 jsonResult, 1110 ) 1111 1112 return err 1113 } 1114 1115 func (it *LinkedList) AsJsonMarshaller() corejson.JsonMarshaller { 1116 return it 1117 }