Skip to content

Commit d2c3d3f

Browse files
committed
modify Add method
1 parent 3df7857 commit d2c3d3f

2 files changed

Lines changed: 34 additions & 36 deletions

File tree

queue/interface_collection.go

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,12 @@ type Collection interface {
100100
ContainsAll(c Collection) bool
101101

102102
/**
103-
* Adds all of the elements in the specified collection to this collection
104-
* (optional operation). The behavior of this operation is undefined if
105-
* the specified collection is modified while the operation is in progress.
106-
* (This implies that the behavior of this call is undefined if the
107-
* specified collection is this collection, and this collection is
108-
* nonempty.)
109-
*
110-
* @param c collection containing elements to be added to this collection
111-
* @return <tt>true</tt> if this collection changed as a result of the call
112-
* @throws UnsupportedOperationException if the <tt>addAll</tt> operation
113-
* is not supported by this collection
114-
* @throws ClassCastException if the class of an element of the specified
115-
* collection prevents it from being added to this collection
116-
* @throws NullPointerException if the specified collection contains a
117-
* null element and this collection does not permit null elements,
118-
* or if the specified collection is null
119-
* @throws IllegalArgumentException if some property of an element of the
120-
* specified collection prevents it from being added to this
121-
* collection
122-
* @throws IllegalStateException if not all the elements can be added at
123-
* this time due to insertion restrictions
124-
* @see #add(Object)
103+
* @Description:
104+
* @param c
105+
* @return bool indicate whether c has changed or not when the method return
106+
* @return error indicate error happened during adding.
125107
*/
126-
AddAll(c Collection) bool
108+
AddAll(c Collection) (bool, error)
127109

128110
/**
129111
* Removes all of this collection's elements that are also contained in the
@@ -151,10 +133,10 @@ type Collection interface {
151133
RemoveAll(c Collection) bool
152134

153135
/**
154-
* Removes all of the elements of this collection that satisfy the given
155-
* predicate. Errors or runtime exceptions thrown during iteration or by
156-
* the predicate are relayed to the caller.
157-
*
136+
* Removes all of the elements of this collection that satisfy the given
137+
* predicate. Errors or runtime exceptions thrown during iteration or by
138+
* the predicate are relayed to the caller.
139+
*
158140
*/
159141
RemoveIf(filter func(value interface{}) bool) bool
160142

queue/linked_blocking_queue.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -352,21 +352,37 @@ func (q *LinkedBlockingQueue) ContainsAll(c Collection) bool {
352352
return containsAll
353353
}
354354

355-
func (q *LinkedBlockingQueue) AddAll(c Collection) bool {
355+
/**
356+
* @Description: if q's capacity is not enough, it will add all non-nil element of Collection c to q until q is full.
357+
nil element in c will be skipped . it'll return FullError/ NilPointerError to indicate error.
358+
* @receiver q
359+
* @param c if c is nil, it'll panic NilPointerError, if c is
360+
* @return bool indicates whether the origin queue has been changed or not when the func return
361+
*/
362+
func (q *LinkedBlockingQueue) AddAll(c Collection) (modified bool, err error) {
356363
if c == nil {
357-
panic(NilPointerError)
364+
return false, NilPointerError
358365
}
359-
if toAdd, ok := c.(*LinkedBlockingQueue); ok && toAdd == q {
360-
panic(IllegalArgumentError)
361-
}
362-
modified := false
366+
q.fullyLock()
367+
defer q.fullyUnlock()
368+
remainingCapacity := int64(q.RemainingCapacity())
369+
var n int64
363370
c.Range(func(value interface{}) bool {
364-
if q.Add(value) {
365-
modified = true
371+
if value == nil {
372+
err = NilPointerError
373+
return true
366374
}
375+
if n == remainingCapacity {
376+
err = FullError
377+
return false
378+
}
379+
modified = true
380+
q.head.PushBack(value)
381+
n++
367382
return true
368383
})
369-
return modified
384+
atomic.AddInt64(&q.length, n)
385+
return
370386
}
371387

372388
func (q *LinkedBlockingQueue) RemoveAll(c Collection) bool {

0 commit comments

Comments
 (0)