Package: FilterService

FilterService

nameinstructionbranchcomplexitylinemethod
FilterService(Method, Object)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
FilterService(Method, Object, int)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
validateAllType(ValidationServices, IReadOnlyQueryEnvironment, Map)
M: 118 C: 0
0%
M: 14 C: 0
0%
M: 8 C: 0
0%
M: 23 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2015 Obeo.
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: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.query.services;
12:
13: import java.lang.reflect.Method;
14: import java.util.LinkedHashSet;
15: import java.util.List;
16: import java.util.Map;
17: import java.util.Map.Entry;
18: import java.util.Set;
19:
20: import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
21: import org.eclipse.acceleo.query.runtime.impl.JavaMethodService;
22: import org.eclipse.acceleo.query.runtime.impl.ValidationServices;
23: import org.eclipse.acceleo.query.validation.type.ICollectionType;
24: import org.eclipse.acceleo.query.validation.type.IType;
25: import org.eclipse.acceleo.query.validation.type.NothingType;
26: import org.eclipse.acceleo.query.validation.type.SequenceType;
27: import org.eclipse.acceleo.query.validation.type.SetType;
28:
29: /**
30: * {@link org.eclipse.acceleo.query.runtime.IService#validateAllType(org.eclipse.acceleo.query.runtime.impl.ValidationServices, org.eclipse.acceleo.query.runtime.impl.EPackageProvider, java.util.Map)
31: * Validates} type according to the second argument of the service.
32: *
33: * @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
34: */
35: public class FilterService extends JavaMethodService {
36:
37:         /**
38:          * The index of the filtering {@link IType}.
39:          */
40:         final int filterIndex;
41:
42:         /**
43:          * Creates a new service instance given a method and an instance.
44:          *
45:          * @param serviceMethod
46:          * the method that realizes the service
47:          * @param serviceInstance
48:          * the instance on which the service must be called
49:          */
50:         public FilterService(Method serviceMethod, Object serviceInstance) {
51:                 this(serviceMethod, serviceInstance, 1);
52:         }
53:
54:         /**
55:          * Creates a new service instance given a method and an instance.
56:          *
57:          * @param serviceMethod
58:          * the method that realizes the service
59:          * @param serviceInstance
60:          * the instance on which the service must be called
61:          * @param filterIndex
62:          * the index of the filtering {@link IType}
63:          * @since 4.0.0
64:          */
65:         public FilterService(Method serviceMethod, Object serviceInstance, int filterIndex) {
66:                 super(serviceMethod, serviceInstance);
67:                 this.filterIndex = filterIndex;
68:         }
69:
70:         @Override
71:         public Set<IType> validateAllType(ValidationServices services, IReadOnlyQueryEnvironment queryEnvironment,
72:                         Map<List<IType>, Set<IType>> allTypes) {
73:                 final Set<IType> result = new LinkedHashSet<IType>();
74:                 final StringBuilder builder = new StringBuilder();
75:
76:•                for (Entry<List<IType>, Set<IType>> entry : allTypes.entrySet()) {
77:•                        for (IType possibleType : entry.getValue()) {
78:                                 final IType rawType;
79:•                                if (possibleType instanceof ICollectionType) {
80:                                         rawType = ((ICollectionType)possibleType).getCollectionType();
81:                                 } else {
82:                                         rawType = possibleType;
83:                                 }
84:•                                if (rawType instanceof NothingType) {
85:                                         builder.append("\n");
86:                                         builder.append(((NothingType)rawType).getMessage());
87:                                 } else {
88:                                         result.add(possibleType);
89:                                 }
90:                         }
91:                 }
92:
93:•                if (result.isEmpty()) {
94:                         final NothingType nothing = services.nothing("Nothing will be left after calling %s:" + builder
95:                                         .toString(), getName());
96:•                        if (List.class.isAssignableFrom(getMethod().getReturnType())) {
97:                                 result.add(new SequenceType(queryEnvironment, nothing));
98:•                        } else if (Set.class.isAssignableFrom(getMethod().getReturnType())) {
99:                                 result.add(new SetType(queryEnvironment, nothing));
100:                         } else {
101:                                 result.add(nothing);
102:                         }
103:                 }
104:
105:                 return result;
106:         }
107:
108: }