Skip to content

Package: ThreadSafeBazaar

ThreadSafeBazaar

nameinstructionbranchcomplexitylinemethod
ThreadSafeBazaar()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
ThreadSafeBazaar(Collection, Map, Bazaar.PriorityOverlapCallBack)
M: 0 C: 49
100%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 0 C: 10
100%
M: 0 C: 1
100%
addContextFunction(String, BazaarContextFunction)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
addVendor(Vendor)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
createProduct(BazaarContext)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
createProducts(BazaarContext)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
removeVendor(Vendor)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
setPriorityOverlapCallBack(Bazaar.PriorityOverlapCallBack)
M: 0 C: 13
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2018 Christian W. Damus 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: * Christian W. Damus - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.bazaar.internal;
15:
16: import java.util.Collection;
17: import java.util.List;
18: import java.util.Map;
19: import java.util.concurrent.locks.ReadWriteLock;
20: import java.util.concurrent.locks.ReentrantReadWriteLock;
21:
22: import org.eclipse.emfforms.bazaar.Bazaar;
23: import org.eclipse.emfforms.bazaar.BazaarContext;
24: import org.eclipse.emfforms.bazaar.BazaarContextFunction;
25: import org.eclipse.emfforms.bazaar.Vendor;
26:
27: /**
28: * A thread-safe implementation of the {@link Bazaar}.
29: * It supports any number of threads concurrently creating products,
30: * to the exclusion of at most one thread changing the bazaar's configuration
31: * (adding/removing vendors, context functions, and so on).
32: *
33: * @param <T> the type of product produced by this {@link Bazaar}
34: *
35: * @author Christian W. Damus
36: */
37: public final class ThreadSafeBazaar<T> extends BazaarImpl<T> {
38:
39:         private final ReadWriteLock lock = new ReentrantReadWriteLock();
40:
41:         /**
42:          * Initializes me.
43:          */
44:         public ThreadSafeBazaar() {
45:                 super();
46:         }
47:
48:         /**
49:          * Initializes me with an initial configuration.
50:          *
51:          * @param vendors optional vendors to add (may be {@code null} if not needed)
52:          * @param contextFunctions optional context functions to add (may be {@code null} if not needed)
53:          * @param priorityOverlapCallBack optional overlap call-back to set (may be {@code null} if not needed)
54:          */
55:         public ThreadSafeBazaar(Collection<? extends Vendor<? extends T>> vendors,
56:                 Map<String, ? extends BazaarContextFunction> contextFunctions,
57:                 PriorityOverlapCallBack<? super T> priorityOverlapCallBack) {
58:
59:                 super();
60:
61:                 // Don't need synchronization for an object that is under construction
62:                 // as it is not yet available to other threads
63:•                if (vendors != null) {
64:•                        for (final Vendor<? extends T> next : vendors) {
65:                                 super.addVendor(next);
66:                         }
67:                 }
68:•                if (contextFunctions != null) {
69:•                        for (final Map.Entry<String, ? extends BazaarContextFunction> next : contextFunctions.entrySet()) {
70:                                 super.addContextFunction(next.getKey(), next.getValue());
71:                         }
72:                 }
73:                 super.setPriorityOverlapCallBack(priorityOverlapCallBack);
74:         }
75:
76:         //
77:         // Queries
78:         //
79:
80:         @Override
81:         public T createProduct(BazaarContext bazaarContext) {
82:                 lock.readLock().lock();
83:
84:                 try {
85:                         return super.createProduct(bazaarContext);
86:                 } finally {
87:                         lock.readLock().unlock();
88:                 }
89:         }
90:
91:         @Override
92:         public List<T> createProducts(BazaarContext bazaarContext) {
93:                 lock.readLock().lock();
94:
95:                 try {
96:                         return super.createProducts(bazaarContext);
97:                 } finally {
98:                         lock.readLock().unlock();
99:                 }
100:         }
101:
102:         //
103:         // Mutations
104:         //
105:
106:         @Override
107:         public void addVendor(Vendor<? extends T> vendor) {
108:                 lock.writeLock().lock();
109:
110:                 try {
111:                         super.addVendor(vendor);
112:                 } finally {
113:                         lock.writeLock().unlock();
114:                 }
115:         }
116:
117:         @Override
118:         public void removeVendor(Vendor<? extends T> vendor) {
119:                 lock.writeLock().lock();
120:
121:                 try {
122:                         super.removeVendor(vendor);
123:                 } finally {
124:                         lock.writeLock().unlock();
125:                 }
126:         }
127:
128:         @Override
129:         public void addContextFunction(String key, BazaarContextFunction contextFunction) {
130:                 lock.writeLock().lock();
131:
132:                 try {
133:                         super.addContextFunction(key, contextFunction);
134:                 } finally {
135:                         lock.writeLock().unlock();
136:                 }
137:         }
138:
139:         @Override
140:         public void setPriorityOverlapCallBack(PriorityOverlapCallBack<? super T> priorityOverlapCallBack) {
141:                 lock.writeLock().lock();
142:
143:                 try {
144:                         super.setPriorityOverlapCallBack(priorityOverlapCallBack);
145:                 } finally {
146:                         lock.writeLock().unlock();
147:                 }
148:         }
149: }