Skip to content

Package: ConcurrentLinkedSetQueue

ConcurrentLinkedSetQueue

nameinstructionbranchcomplexitylinemethod
ConcurrentLinkedSetQueue()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addAll(Collection)
M: 0 C: 26
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
clear()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
iterator()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
offer(Object)
M: 0 C: 22
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
poll()
M: 0 C: 12
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
remove(Object)
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
removeAll(Collection)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
retainAll(Collection)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * nicole.behlen - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.internal.validation;
15:
16: import java.util.ArrayList;
17: import java.util.Collection;
18: import java.util.Collections;
19: import java.util.Iterator;
20: import java.util.List;
21: import java.util.NoSuchElementException;
22: import java.util.Set;
23: import java.util.concurrent.ConcurrentHashMap;
24: import java.util.concurrent.ConcurrentLinkedQueue;
25:
26: /**
27: * An extension of {@link ConcurrentLinkedQueue} that additionally contains a set to avoid unnecessary
28: * duplicated entries in cases of non-concurrent additions.
29: * Duplication may still occur if multiple threads enqueue the same objects.
30: *
31: * @author nicole.behlen
32: *
33: * @param <E> the generic type of the elements to be queued.
34: */
35: public class ConcurrentLinkedSetQueue<E> extends ConcurrentLinkedQueue<E> {
36:
37:         private static final long serialVersionUID = 290189755090394151L;
38:
39:         private final Set<Object> concurrentSet = Collections.newSetFromMap(new ConcurrentHashMap<Object, Boolean>());
40:
41:         @Override
42:         public void clear() {
43:                 super.clear();
44:                 concurrentSet.clear();
45:         }
46:
47:         @Override
48:         public boolean removeAll(Collection<?> c) {
49:                 final boolean result = super.removeAll(c);
50:                 concurrentSet.removeAll(c);
51:                 return result;
52:         }
53:
54:         @Override
55:         public boolean retainAll(Collection<?> c) {
56:                 final boolean result = super.retainAll(c);
57:                 concurrentSet.retainAll(c);
58:                 return result;
59:         }
60:
61:         /**
62:          * Adds the object to the tail of this queue, if not already contained.
63:          *
64:          * @param e adds if not already contained the object to the tail of this queue
65:          * @return <code>false</code> if object is already contained in queue, <code>true</code> otherwise.
66:          * @throws NullPointerException - if the specified element is null
67:          */
68:         @Override
69:         public boolean offer(E e) {
70:•                if (e == null) {
71:                         throw new NullPointerException();
72:                 }
73:•                if (!concurrentSet.contains(e)) {
74:                         concurrentSet.add(e);
75:                         return super.offer(e);
76:                 }
77:                 return false;
78:         }
79:
80:         @Override
81:         public E poll() {
82:                 final E next = super.poll();
83:•                if (next != null) {
84:                         concurrentSet.remove(next);
85:                 }
86:                 return next;
87:         }
88:
89:         @Override
90:         public boolean remove(Object o) {
91:•                if (o != null) {
92:                         concurrentSet.remove(o);
93:                 }
94:                 return super.remove(o);
95:         }
96:
97:         @Override
98:         public boolean addAll(Collection<? extends E> c) {
99:•                if (c == this) {
100:                         // As historically specified in AbstractQueue#addAll
101:                         throw new IllegalArgumentException();
102:                 }
103:
104:                 final List<E> newColl = new ArrayList<>(c);
105:                 newColl.removeAll(concurrentSet);
106:                 concurrentSet.addAll(newColl);
107:                 return super.addAll(newColl);
108:         }
109:
110:         @Override
111:         public Iterator<E> iterator() {
112:                 final Iterator<E> iter = super.iterator();
113:                 return new IterWithRemove<>(iter);
114:         }
115:
116:         /**
117:          *
118:          * Iterator over the elements of this queue in proper sequence, that removes
119:          * the elements from the {@link ConcurrentLinkedSetQueue#concurrentSet}.
120:          *
121:          * @author nicole.behlen
122:          *
123:          * @param <I> the generic type of the elements to iterate.
124:          */
125:         private class IterWithRemove<I> implements Iterator<I> {
126:
127:                 private final Iterator<I> delegate;
128:
129:                 private I lastReturnedElement;
130:
131:                 /**
132:                  * Constructor setting the original iterator.
133:                  */
134:                 IterWithRemove(Iterator<I> delegate) {
135:                         this.delegate = delegate;
136:                 }
137:
138:                 @Override
139:                 public boolean hasNext() {
140:                         return this.delegate.hasNext();
141:                 }
142:
143:                 @Override
144:                 public I next() {
145:                         if (this.delegate.hasNext()) {
146:                                 lastReturnedElement = this.delegate.next();
147:                                 return lastReturnedElement;
148:                         }
149:                         lastReturnedElement = null;
150:                         throw new NoSuchElementException();
151:                 }
152:
153:                 @Override
154:                 public void remove() {
155:                         if (lastReturnedElement != null) {
156:                                 concurrentSet.remove(lastReturnedElement);
157:                                 lastReturnedElement = null;
158:                         }
159:                         delegate.remove();
160:                 }
161:         }
162: }