Skip to content

Package: DefaultEAttributeValueConverterImpl

DefaultEAttributeValueConverterImpl

nameinstructionbranchcomplexitylinemethod
DefaultEAttributeValueConverterImpl()
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%
DefaultEAttributeValueConverterImpl(String, double)
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%
convertToLiteral(EObject, EStructuralFeature, Object)
M: 45 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
convertToModelValue(EObject, EStructuralFeature, String)
M: 40 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
fromString(EStructuralFeature, String)
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%
isApplicable(EObject, EStructuralFeature, Object, EStructuralFeatureValueConverter.Direction)
M: 12 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
toString(EStructuralFeature, Object)
M: 3 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-2016 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: * Mathias Schaefer - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.common.converter;
15:
16: import java.util.ArrayList;
17: import java.util.Collection;
18: import java.util.List;
19: import java.util.StringTokenizer;
20:
21: import org.eclipse.emf.ecore.EAttribute;
22: import org.eclipse.emf.ecore.EObject;
23: import org.eclipse.emf.ecore.EReference;
24: import org.eclipse.emf.ecore.EStructuralFeature;
25: import org.eclipse.emf.ecore.util.EcoreUtil;
26: import org.eclipse.emfforms.spi.common.converter.EStructuralFeatureValueConverter;
27: import org.osgi.service.component.annotations.Component;
28:
29: /**
30: * Default value converter for generic EAttribute handling.
31: * Does currently not handle EReferences.
32: *
33: * @author Mathias Schaefer <mschaefer@eclipsesource.com>
34: *
35: */
36: @Component
37: public class DefaultEAttributeValueConverterImpl implements EStructuralFeatureValueConverter {
38:
39:         /**
40:          * Default list separator token.
41:          */
42:         public static final String DEFAULT_LIST_SEPARATOR = ","; //$NON-NLS-1$
43:         /**
44:          * The default priority for this converter implementation.
45:          * Make sure your converter has a higher priority.
46:          */
47:         public static final double DEFAULT_PRIORITY = 2d;
48:
49:         private final String listSeparator;
50:         private final double priority;
51:
52:         /**
53:          * Default no value constructor (will use the
54:          * DEFAULT_LIST_SEPARATOR and DEFAULT_PRIORITY for this converter).
55:          */
56:         public DefaultEAttributeValueConverterImpl() {
57:                 this(DEFAULT_LIST_SEPARATOR, DEFAULT_PRIORITY);
58:         }
59:
60:         /**
61:          * Constructor which allows to override the list separator.
62:          *
63:          * @param listSeparator the separator token to split lists (i.e. "|")
64:          * @param priority the priority for this converter
65:          */
66:         protected DefaultEAttributeValueConverterImpl(String listSeparator, double priority) {
67:                 this.listSeparator = listSeparator;
68:                 this.priority = priority;
69:         }
70:
71:         @Override
72:         public double isApplicable(EObject eObject, EStructuralFeature feature, Object value, Direction direction) {
73:•                if (Direction.MODEL_TO_LITERAL.equals(direction) || feature instanceof EReference) {
74:                         return NOT_APPLICABLE;
75:                 }
76:                 return priority; // otherwise always applicable
77:         }
78:
79:         @Override
80:         public Object convertToModelValue(EObject eObject, EStructuralFeature feature, String literal) {
81:
82:                 try {
83:•                        if (feature.isMany()) {
84:                                 final List<Object> objects = new ArrayList<Object>();
85:                                 final StringTokenizer tokenizer = new StringTokenizer(literal, listSeparator, false);
86:•                                while (tokenizer.hasMoreTokens()) {
87:
88:                                         final String item = tokenizer.nextToken();
89:                                         objects.add(fromString(feature, item.trim()));
90:
91:                                 }
92:                                 return objects;
93:                         }
94:                         return fromString(feature, literal);
95:                 }
96:                 // BEGIN SUPRESS CATCH EXCEPTION
97:                 catch (final RuntimeException ex) {// END SUPRESS CATCH EXCEPTION
98:                         // silently ignore this (conversion can fail for various reasons)
99:                         return null;
100:                 }
101:
102:         }
103:
104:         @SuppressWarnings("unchecked")
105:         @Override
106:         public Object convertToLiteral(EObject eObject, EStructuralFeature feature, Object instance) {
107:                 try {
108:•                        if (instance instanceof Collection) {
109:                                 final StringBuilder stringBuilder = new StringBuilder();
110:•                                for (final Object object : (Collection<Object>) instance) {
111:•                                        if (stringBuilder.length() > 0) {
112:                                                 stringBuilder.append(listSeparator);
113:                                         }
114:                                         stringBuilder.append(String.valueOf(toString(feature, object)));
115:                                 }
116:                                 return stringBuilder.toString();
117:                         }
118:                         return toString(feature, instance);
119:                 }
120:                 // BEGIN SUPRESS CATCH EXCEPTION
121:                 catch (final RuntimeException ex) {// END SUPRESS CATCH EXCEPTION
122:                         // silently ignore this (conversion can fail for various reasons)
123:                         return null;
124:                 }
125:         }
126:
127:         /**
128:          * Convert literal to Object.
129:          *
130:          * @param feature target feature
131:          * @param literal to convert
132:          * @return converted object
133:          */
134:         protected Object fromString(EStructuralFeature feature, String literal) {
135:                 return EcoreUtil.createFromString(((EAttribute) feature).getEAttributeType(), literal);
136:         }
137:
138:         /**
139:          * Basic conversion from model to string literal.
140:          *
141:          * @param feature the feauter
142:          * @param instance the model value
143:          * @return the string value
144:          */
145:         protected Object toString(EStructuralFeature feature, Object instance) {
146:                 return String.valueOf(instance);
147:
148:         }
149:
150: }