Package: SynchronizedCollection

SynchronizedCollection

nameinstructionbranchcomplexitylinemethod
SynchronizedCollection(Collection, Object)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
add(Object)
M: 0 C: 12
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: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
clear()
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
contains(Object)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
containsAll(Collection)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
equals(Object)
M: 37 C: 89
71%
M: 3 C: 13
81%
M: 2 C: 7
78%
M: 5 C: 20
80%
M: 0 C: 1
100%
hashCode()
M: 0 C: 20
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
isEmpty()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
iterator()
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
remove(Object)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
removeAll(Collection)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
retainAll(Collection)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
size()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toArray()
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
toArray(Object[])
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2008, 2010 VMware Inc.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * VMware Inc. - initial contribution
10: *******************************************************************************/
11:
12: package org.eclipse.virgo.util.common;
13:
14: import java.util.Collection;
15: import java.util.Iterator;
16:
17: /**
18: * {@link SynchronizedCollection} wraps a given {@link Collection} and protects access to the collection and any
19: * iterators created from the collection by synchronizing on a given monitor.
20: * <p />
21: *
22: * <strong>Concurrent Semantics</strong><br />
23: *
24: * This class is thread safe, but ConcurrentModificationException may still be thrown.
25: *
26: * @param <T> type of element in collection
27: */
28: public class SynchronizedCollection<T> extends SynchronizedObject implements Collection<T> {
29:
30: private final Collection<T> collection;
31:
32: private final Object monitor;
33:
34: private static final Object tieMonitor = new Object();
35:
36: /**
37: * Creates a {@link SynchronizedCollection} wrapping the given collection and synchronizing on the given monitor,
38: * neither of which may be <code>null</code>.
39: *
40: * @param collection the collection to be wrapped, which must not be <code>null</code>
41: * @param monitor the monitor which will be used to synchronize access to the collection
42: */
43: @SuppressWarnings("unchecked")
44: public SynchronizedCollection(Collection<? extends T> collection, Object monitor) {
45: super(collection, monitor); // throws an exception if either argument is null
46: this.collection = (Collection<T>) collection;
47: this.monitor = monitor;
48: }
49:
50: /**
51: * {@inheritDoc}
52: */
53: @Override
54: public int hashCode() {
55: final int prime = 31;
56: int result = 1;
57: synchronized (this.monitor) {
58: result = prime * result + collection.hashCode();
59: }
60: return result;
61: }
62:
63: /**
64: * {@inheritDoc}
65: */
66: @SuppressWarnings("unchecked")
67: @Override
68: public boolean equals(Object obj) {
69:• if (this == obj) {
70: return true;
71: }
72:• if (obj == null) {
73: return false;
74: }
75:• if (getClass() != obj.getClass()) {
76: return false;
77: }
78: SynchronizedCollection<T> other = (SynchronizedCollection<T>) obj;
79: int thisHash = System.identityHashCode(this);
80: int otherHash = System.identityHashCode(other);
81:• if (thisHash < otherHash) {
82: synchronized (this.monitor) {
83: synchronized (other.monitor) {
84:• if (!collection.equals(other.collection)) {
85: return false;
86: }
87: }
88: }
89:• } else if (thisHash > otherHash) {
90: synchronized (other.monitor) {
91: synchronized (this.monitor) {
92:• if (!collection.equals(other.collection)) {
93: return false;
94: }
95: }
96: }
97: } else {
98: synchronized (tieMonitor) {
99: synchronized (this.monitor) {
100: synchronized (other.monitor) {
101:• if (!collection.equals(other.collection)) {
102: return false;
103: }
104: }
105: }
106: }
107: }
108: return true;
109: }
110:
111: /**
112: * {@inheritDoc}
113: */
114: public boolean add(T e) {
115: synchronized (this.monitor) {
116: return this.collection.add(e);
117: }
118: }
119:
120: /**
121: * {@inheritDoc}
122: */
123: public boolean addAll(Collection<? extends T> c) {
124: synchronized (this.monitor) {
125: return this.collection.addAll(c);
126: }
127: }
128:
129: /**
130: * {@inheritDoc}
131: */
132: public void clear() {
133: synchronized (this.monitor) {
134: this.collection.clear();
135: }
136: }
137:
138: /**
139: * {@inheritDoc}
140: */
141: public boolean contains(Object o) {
142: synchronized (this.monitor) {
143: return this.collection.contains(o);
144: }
145: }
146:
147: /**
148: * {@inheritDoc}
149: */
150: public boolean containsAll(Collection<?> c) {
151: synchronized (this.monitor) {
152: return this.collection.containsAll(c);
153: }
154: }
155:
156: /**
157: * {@inheritDoc}
158: */
159: public boolean isEmpty() {
160: synchronized (this.monitor) {
161: return this.collection.isEmpty();
162: }
163: }
164:
165: /**
166: * {@inheritDoc}
167: */
168: public Iterator<T> iterator() {
169: synchronized (this.monitor) {
170: return new SynchronizedIterator<T>(this.collection.iterator(), this.monitor);
171: }
172: }
173:
174: /**
175: * {@inheritDoc}
176: */
177: public boolean remove(Object o) {
178: synchronized (this.monitor) {
179: return this.collection.remove(o);
180: }
181: }
182:
183: /**
184: * {@inheritDoc}
185: */
186: public boolean removeAll(Collection<?> c) {
187: synchronized (this.monitor) {
188: return this.collection.removeAll(c);
189: }
190: }
191:
192: /**
193: * {@inheritDoc}
194: */
195: public boolean retainAll(Collection<?> c) {
196: synchronized (this.monitor) {
197: return this.collection.retainAll(c);
198: }
199: }
200:
201: /**
202: * {@inheritDoc}
203: */
204: public int size() {
205: synchronized (this.monitor) {
206: return this.collection.size();
207: }
208: }
209:
210: /**
211: * {@inheritDoc}
212: */
213: public Object[] toArray() {
214: synchronized (this.monitor) {
215: return this.collection.toArray();
216: }
217: }
218:
219: /**
220: * {@inheritDoc}
221: */
222: public <U> U[] toArray(U[] a) {
223: synchronized (this.monitor) {
224: return this.collection.toArray(a);
225: }
226: }
227:
228: }