Skip to content

Package: BazaarContext

BazaarContext

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: * jonas - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.bazaar;
15:
16: import java.util.HashMap;
17: import java.util.Map;
18:
19: import org.eclipse.emfforms.bazaar.internal.BazaarContextImpl;
20:
21: /**
22: * Provides the necessary parameters, the {@link Vendor} of a {@link Bazaar} might request. Parameters are provided
23: * in a {@link Map} from their key to their value. Those parameters can be adapted to other ones by
24: * {@link BazaarContextFunction}s
25: *
26: */
27: public interface BazaarContext {
28:
29:         /**
30:          * Returns a map containing the parameters available for {@link Vendor}s on a {@link Bazaar}. The key is either
31:          * an id or the full class name. the value is the parameter which will be provided to {@link Vendor}s
32:          *
33:          * @return A {@link Map} containing the parameters for {@link Vendor} from (key|full class name) to the object
34:          * as value.
35:          */
36:         Map<String, Object> getContextMap();
37:
38:         //
39:         // Nested types
40:         //
41:
42:         /**
43:          * A fluent <em>builder</em> of {@link BazaarContext}s.
44:          *
45:          * @author Christian W. Damus
46:          */
47:         final class Builder {
48:                 private final Map<String, Object> contextMap = new HashMap<String, Object>();
49:
50:                 /**
51:                  * Creates a new empty context builder.
52:                  */
53:                 private Builder() {
54:                         super();
55:                 }
56:
57:                 /**
58:                  * Creates a new context builder with the given initial {@code values}.
59:                  *
60:                  * @param values initial context values
61:                  */
62:                 private Builder(Map<String, ?> values) {
63:                         super();
64:
65:                         contextMap.putAll(values);
66:                 }
67:
68:                 /**
69:                  * Creates a new empty context builder.
70:                  *
71:                  * @return an initially empty builder
72:                  */
73:                 public static Builder empty() {
74:                         return new Builder();
75:                 }
76:
77:                 /**
78:                  * Creates a new context builder with the given initial {@code values}.
79:                  *
80:                  * @param values initial context values
81:                  * @return an initialized builder
82:                  */
83:                 public static Builder with(Map<String, ?> values) {
84:                         return new Builder(values);
85:                 }
86:
87:                 /**
88:                  * Adds a context value of the given type.
89:                  *
90:                  * @param key the context {@code value}'s type
91:                  * @param value the context value
92:                  *
93:                  * @return this builder
94:                  *
95:                  * @param <T> the value type
96:                  */
97:                 public <T> Builder put(Class<T> key, T value) {
98:                         return put(key.getName(), value);
99:                 }
100:
101:                 /**
102:                  * Adds a self-typed context value.
103:                  *
104:                  * @param value the context value to add
105:                  *
106:                  * @return this builder
107:                  *
108:                  * @see #put(Class, Object)
109:                  *
110:                  * @throws NullPointerException if the {@code value} is {@code null}
111:                  */
112:                 public Builder add(Object value) {
113:                         return put(value.getClass().getName(), value);
114:                 }
115:
116:                 /**
117:                  * Adds a context value for the given name, such as might be used with
118:                  * {@linkplain javax.inject.Named named injection}.
119:                  *
120:                  * @param name the context {@code value}'s name
121:                  * @param value the context value
122:                  *
123:                  * @return this builder
124:                  *
125:                  * @see javax.inject.Named
126:                  */
127:                 public Builder put(String name, Object value) {
128:                         contextMap.put(name, value);
129:                         return this;
130:                 }
131:
132:                 /**
133:                  * Create the bazaar context. Further updates to the builder will have
134:                  * no effect on the resulting context.
135:                  *
136:                  * @return the bazaar context
137:                  */
138:                 public BazaarContext build() {
139:                         return new BazaarContextImpl(contextMap);
140:                 }
141:         }
142: }