fix sync download file bug

This commit is contained in:
tickstep
2022-06-04 13:20:33 +08:00
parent f2bcc977cc
commit 8955eeec92
10 changed files with 548 additions and 209 deletions
+39 -1
View File
@@ -1,6 +1,7 @@
package collection
import (
"strings"
"sync"
)
@@ -34,7 +35,7 @@ func (q *Queue) PushUnique(item interface{}) {
q.queueList = []interface{}{}
} else {
for _, qItem := range q.queueList {
if item.(QueueItem).HashCode() == qItem.(QueueItem).HashCode() {
if strings.Compare(item.(QueueItem).HashCode(), qItem.(QueueItem).HashCode()) == 0 {
return
}
}
@@ -61,3 +62,40 @@ func (q *Queue) Length() int {
defer q.mutex.Unlock()
return len(q.queueList)
}
func (q *Queue) Remove(item interface{}) {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.queueList == nil {
q.queueList = []interface{}{}
}
if len(q.queueList) == 0 {
return
}
j := 0
for _, qItem := range q.queueList {
if strings.Compare(item.(QueueItem).HashCode(), qItem.(QueueItem).HashCode()) != 0 {
q.queueList[j] = qItem
j++
}
}
q.queueList = q.queueList[:j]
return
}
func (q *Queue) Contains(item interface{}) bool {
q.mutex.Lock()
defer q.mutex.Unlock()
if q.queueList == nil {
q.queueList = []interface{}{}
}
if len(q.queueList) == 0 {
return false
}
for _, qItem := range q.queueList {
if strings.Compare(item.(QueueItem).HashCode(), qItem.(QueueItem).HashCode()) == 0 {
return true
}
}
return false
}
+24
View File
@@ -0,0 +1,24 @@
package collection
import (
"fmt"
"testing"
)
type item struct {
Name string
}
func (i *item) HashCode() string {
return i.Name
}
func TestRemove(t *testing.T) {
q := NewFifoQueue()
q.Push(&item{Name: "1"})
q.Push(&item{Name: "2"})
q.Push(&item{Name: "3"})
q.Push(&item{Name: "4"})
q.Remove(&item{Name: "3"})
fmt.Println(q)
}