Skip to content

Package: ValidationProviderHelper$Wrapper

ValidationProviderHelper$Wrapper

nameinstructionbranchcomplexitylinemethod
ValidationProviderHelper.Wrapper(ValidationProviderHelper, ValidationProvider)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
setContext(ViewModelContext)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
unsetContext(ViewModelContext)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
validate(EObject)
M: 22 C: 18
45%
M: 3 C: 3
50%
M: 2 C: 2
50%
M: 5 C: 5
50%
M: 0 C: 1
100%
validate(ViewModelContext, EObject)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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: * mat - initial API and implementation
13: * Christian W. Damus - bug 552715
14: ******************************************************************************/
15: package org.eclipse.emf.ecp.view.internal.validation;
16:
17: import java.util.ArrayList;
18: import java.util.Collection;
19: import java.util.Collections;
20: import java.util.HashMap;
21: import java.util.LinkedHashSet;
22: import java.util.List;
23: import java.util.Map;
24: import java.util.Set;
25:
26: import org.eclipse.core.runtime.CoreException;
27: import org.eclipse.core.runtime.IConfigurationElement;
28: import org.eclipse.core.runtime.IExtensionRegistry;
29: import org.eclipse.core.runtime.Platform;
30: import org.eclipse.emf.common.util.Diagnostic;
31: import org.eclipse.emf.ecore.EObject;
32: import org.eclipse.emf.ecp.view.spi.context.ViewModelContext;
33: import org.eclipse.emf.ecp.view.spi.validation.ValidationProvider;
34: import org.eclipse.emfforms.common.spi.validation.ValidationService;
35:
36: /**
37: * Helper class for fetching ECP validators.
38: * See {@link ValidationService#addValidator(org.eclipse.emfforms.common.spi.validation.Validator)}.
39: *
40: * @author Mat Hansen <mhansen@eclipsesource.com>
41: */
42: public final class ValidationProviderHelper {
43:
44:         private final Map<ValidationProvider, ValidationProvider> providers = new HashMap<>();
45:         private final ViewModelContext context;
46:         private final ValidationServiceImpl validationService;
47:
48:         /**
49:          * Initializes me with the validation service that owns me.
50:          *
51:          * @param context the view model context
52:          * @param validationService my owner
53:          */
54:         ValidationProviderHelper(ViewModelContext context, ValidationServiceImpl validationService) {
55:                 super();
56:
57:                 this.context = context;
58:                 this.validationService = validationService;
59:         }
60:
61:         /**
62:          * Query the registered validation providers.
63:          *
64:          * @return the registered validation providers
65:          */
66:         public Set<ValidationProvider> getValidationProviders() {
67:                 return Collections.unmodifiableSet(providers.keySet());
68:         }
69:
70:         /**
71:          * Initialize my providers.
72:          */
73:         public void initialize() {
74:                 fetchValidationProviders().forEach(p -> validationService.addValidationProvider(p, false));
75:         }
76:
77:         /**
78:          * Dispose and clear out the registered validation providers.
79:          */
80:         public void dispose() {
81:                 try {
82:                         providers.keySet().forEach(p -> validationService.removeValidationProvider(p, false));
83:                 } finally {
84:                         providers.clear();
85:                 }
86:         }
87:
88:         /**
89:          * Wrap a validation {@code provider} to inject my view model {@code context} through the core validation service.
90:          *
91:          * @param provider the provider to wrap
92:          *
93:          * @return the wrapper
94:          */
95:         ValidationProvider wrap(ValidationProvider provider) {
96:                 return providers.computeIfAbsent(provider, Wrapper::new);
97:         }
98:
99:         /**
100:          * Unwrap a validation {@code provider}.
101:          *
102:          * @param provider the provider to unwrap (usually a wrapper)
103:          *
104:          * @return the unwrapped provider
105:          */
106:         ValidationProvider unwrap(ValidationProvider provider) {
107:                 return provider instanceof Wrapper ? ((Wrapper) provider).delegate : provider;
108:         }
109:
110:         /**
111:          * Fetch all known ECP validators using the ECP validationProvider extension point.
112:          *
113:          * @return the validators found
114:          *
115:          * @deprecated Since 1.23, use instances of this class, instead
116:          */
117:         @Deprecated
118:         public static Set<ValidationProvider> fetchValidationProviders() {
119:                 final Set<ValidationProvider> providers = new LinkedHashSet<ValidationProvider>();
120:
121:                 final IExtensionRegistry extensionRegistry = Platform.getExtensionRegistry();
122:                 if (extensionRegistry == null) {
123:                         return providers;
124:                 }
125:                 final IConfigurationElement[] controls = extensionRegistry
126:                         .getConfigurationElementsFor("org.eclipse.emf.ecp.view.validation.validationProvider"); //$NON-NLS-1$
127:                 for (final IConfigurationElement e : controls) {
128:                         try {
129:                                 final ValidationProvider provider = (ValidationProvider) e.createExecutableExtension("class"); //$NON-NLS-1$
130:                                 providers.add(provider);
131:                         } catch (final CoreException e1) {
132:                                 Activator.logException(e1);
133:                         }
134:                 }
135:                 return providers;
136:         }
137:
138:         //
139:         // Nested types
140:         //
141:
142:         /**
143:          * A wrapper for a validation provider that injects the current view model context into it.
144:          */
145:         private final class Wrapper implements ValidationProvider {
146:
147:                 private final ValidationProvider delegate;
148:
149:                 Wrapper(ValidationProvider delegate) {
150:                         super();
151:
152:                         this.delegate = delegate;
153:                 }
154:
155:                 @Override
156:                 public void setContext(ViewModelContext context) {
157:                         delegate.setContext(context);
158:                 }
159:
160:                 @Override
161:                 public void unsetContext(ViewModelContext context) {
162:                         delegate.unsetContext(context);
163:                 }
164:
165:                 @SuppressWarnings("unchecked") // The core validation service doesn't write to the list
166:                 @Override
167:                 public List<Diagnostic> validate(EObject eObject) {
168:                         final Iterable<? extends Diagnostic> delegated = delegate.validate(context, eObject);
169:•                        if (delegated == null) {
170:                                 return Collections.emptyList();
171:                         }
172:•                        if (delegated instanceof List<?>) {
173:                                 return (List<Diagnostic>) delegated;
174:                         }
175:•                        if (delegated instanceof Collection<?>) {
176:                                 return new ArrayList<>((Collection<Diagnostic>) delegated);
177:                         }
178:                         final List<Diagnostic> result = new ArrayList<Diagnostic>();
179:                         delegated.forEach(result::add);
180:                         return result;
181:                 }
182:
183:                 @Override
184:                 public Iterable<? extends Diagnostic> validate(ViewModelContext userContext, EObject object) {
185:                         return delegate.validate(userContext, object);
186:                 }
187:
188:         }
189:
190: }