Skip to content

Package: BazaarUtil

BazaarUtil

nameinstructionbranchcomplexitylinemethod
adapt(Dictionary)
M: 24 C: 8
25%
M: 3 C: 1
25%
M: 2 C: 1
33%
M: 5 C: 3
38%
M: 0 C: 1
100%
builder(Object)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
createBaseContext(Dictionary)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createBazaar(Object)
M: 0 C: 6
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) 2011-2018 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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.spi.bazaar;
15:
16: import java.util.Dictionary;
17: import java.util.Enumeration;
18: import java.util.HashMap;
19: import java.util.Map;
20:
21: import org.eclipse.emfforms.bazaar.Bazaar;
22: import org.eclipse.emfforms.bazaar.BazaarContext;
23: import org.eclipse.emfforms.bazaar.Create;
24: import org.eclipse.emfforms.bazaar.StaticBid;
25: import org.eclipse.emfforms.bazaar.Vendor;
26:
27: /**
28: * Utility class providing common functionality for the creation of {@link Bazaar Bazaars}.
29: *
30: * @author Lucas Koehler
31: *
32: */
33: public final class BazaarUtil {
34:         /**
35:          * Prevent instantiation of this utility class.
36:          */
37:         private BazaarUtil() {
38:         }
39:
40:         /**
41:          * Creates a new {@link Bazaar} initialized with a given default product. The created bazaar has a vendor which can
42:          * always provide the default product. Thereby, this vendor always bids the lowest valid amount.
43:          *
44:          * @param defaultProduct The default product which can always be created
45:          * @param <T> The type of the bazaar's created products.
46:          * @return The created {@link Bazaar}
47:          */
48:         public static <T> Bazaar<T> createBazaar(T defaultProduct) {
49:                 final Bazaar.Builder<T> builder = builder(defaultProduct);
50:                 return builder.build();
51:         }
52:
53:         private static <T> Bazaar.Builder<T> builder(final T defaultStrategy) {
54:                 /**
55:                  * A vendor of the default, that tries to lose every bid.
56:                  */
57:                 @StaticBid(bid = Double.NEGATIVE_INFINITY)
58:                 class DefaultVendor implements Vendor<T> {
59:                         /**
60:                          * Return the default strategy.
61:                          *
62:                          * @return the default strategy
63:                          */
64:                         @Create
65:                         public T createDefault() {
66:                                 return defaultStrategy;
67:                         }
68:                 }
69:
70:                 final Bazaar.Builder<T> result = Bazaar.Builder.empty();
71:                 return result.threadSafe().add(new DefaultVendor());
72:         }
73:
74:         /**
75:          * Creates a basic {@link org.eclipse.emfforms.bazaar.BazaarContext.Builder Bazaar Context Builder} with the given
76:          * initial values.
77:          *
78:          * @param properties {@link Dictionary} containing the initial values
79:          * @return The created {@link org.eclipse.emfforms.bazaar.BazaarContext.Builder BazaarContext.Builder}
80:          */
81:         public static BazaarContext.Builder createBaseContext(Dictionary<String, ?> properties) {
82:                 return BazaarContext.Builder.with(adapt(properties));
83:         }
84:
85:         /**
86:          * Adapt a {@code dictionary} as a map.
87:          *
88:          * @param dictionary a dictionary
89:          * @return the {@code dictionary}, as a map
90:          */
91:         private static Map<String, ?> adapt(Dictionary<String, ?> dictionary) {
92:                 // The OSGi implementation of the read-only properties for all sorts of
93:                 // things is a map
94:•                if (dictionary instanceof Map<?, ?>) {
95:                         @SuppressWarnings("unchecked")
96:                         final Map<String, ?> result = (Map<String, ?>) dictionary;
97:                         return result;
98:                 }
99:
100:                 final Map<String, Object> result = new HashMap<String, Object>();
101:•                for (final Enumeration<String> keys = dictionary.keys(); keys.hasMoreElements();) {
102:                         final String next = keys.nextElement();
103:                         result.put(next, dictionary.get(next));
104:                 }
105:                 return result;
106:         }
107: }