mirror of
https://gitee.com/zhangsken/aliyunpan.git
synced 2026-10-16 02:23:49 +08:00
fix sync download file bug
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user