gitlab.com/evatix-go/core@v1.3.55/coredata/corestr/NonChainedLinkedCollectionNodes.go (about) 1 package corestr 2 3 type NonChainedLinkedCollectionNodes struct { 4 items *[]*LinkedCollectionNode 5 isChainingApplied bool 6 } 7 8 func NewNonChainedLinkedCollectionNodes( 9 capacity int, 10 ) *NonChainedLinkedCollectionNodes { 11 items := make([]*LinkedCollectionNode, 0, capacity) 12 13 return &NonChainedLinkedCollectionNodes{ 14 items: &items, 15 } 16 } 17 18 func (receiver *NonChainedLinkedCollectionNodes) IsChainingApplied() bool { 19 return receiver.isChainingApplied 20 } 21 22 func (receiver *NonChainedLinkedCollectionNodes) Items() *[]*LinkedCollectionNode { 23 return receiver.items 24 } 25 26 func (receiver *NonChainedLinkedCollectionNodes) Length() int { 27 if receiver.items == nil { 28 return 0 29 } 30 31 return len(*receiver.items) 32 } 33 34 func (receiver *NonChainedLinkedCollectionNodes) IsEmpty() bool { 35 return receiver.items == nil || len(*receiver.items) == 0 36 } 37 38 func (receiver *NonChainedLinkedCollectionNodes) Adds( 39 nodes ...*LinkedCollectionNode, 40 ) *NonChainedLinkedCollectionNodes { 41 if nodes == nil { 42 return receiver 43 } 44 45 for i := range nodes { 46 *receiver.items = append( 47 *receiver.items, 48 nodes[i]) 49 } 50 51 return receiver 52 } 53 54 func (receiver *NonChainedLinkedCollectionNodes) HasItems() bool { 55 return !receiver.IsEmpty() 56 } 57 58 func (receiver *NonChainedLinkedCollectionNodes) First() *LinkedCollectionNode { 59 return (*receiver.items)[0] 60 } 61 62 func (receiver *NonChainedLinkedCollectionNodes) FirstOrDefault() *LinkedCollectionNode { 63 if receiver.IsEmpty() { 64 return nil 65 } 66 67 return (*receiver.items)[0] 68 } 69 70 func (receiver *NonChainedLinkedCollectionNodes) Last() *LinkedCollectionNode { 71 return (*receiver.items)[receiver.Length()-1] 72 } 73 74 func (receiver *NonChainedLinkedCollectionNodes) LastOrDefault() *LinkedCollectionNode { 75 if receiver.IsEmpty() { 76 return nil 77 } 78 79 return (*receiver.items)[receiver.Length()-1] 80 } 81 82 // ApplyChaining Warning Mutates data inside. 83 func (receiver *NonChainedLinkedCollectionNodes) ApplyChaining() *NonChainedLinkedCollectionNodes { 84 length := receiver.Length() 85 if length == 0 || receiver.isChainingApplied { 86 return receiver 87 } 88 89 receiver.isChainingApplied = true 90 for i, node := range *receiver.items { 91 if i+1 >= length { 92 break 93 } 94 95 nextNode := (*receiver.items)[i+1] 96 node.next = nextNode 97 } 98 99 if receiver.HasItems() { 100 receiver.Last().next = nil 101 } 102 103 return receiver 104 } 105 106 func (receiver *NonChainedLinkedCollectionNodes) ToChainedNodes() *[]*LinkedCollectionNode { 107 length := receiver.Length() 108 list := make([]*LinkedCollectionNode, length) 109 110 if length == 0 { 111 return &list 112 } 113 114 for i, node := range *receiver.items { 115 if i+1 >= length { 116 break 117 } 118 119 curNode := node.Clone() 120 list = append(list, curNode) 121 nextNode := (*receiver.items)[i+1] 122 nextNodeClone := nextNode.Clone() 123 curNode.next = nextNodeClone 124 list = append(list, nextNodeClone) 125 } 126 127 return &list 128 }