Skip to content

Package: NumberFormatHelper

NumberFormatHelper

nameinstructionbranchcomplexitylinemethod
getNumberFormat(EAttribute)
M: 44 C: 37
46%
M: 9 C: 7
44%
M: 7 C: 2
22%
M: 10 C: 13
57%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 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: * Eugen - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.spreadsheet.core.converter;
15:
16: import org.eclipse.emf.common.util.EMap;
17: import org.eclipse.emf.ecore.EAnnotation;
18: import org.eclipse.emf.ecore.EAttribute;
19: import org.eclipse.emf.ecore.EDataType;
20:
21: /**
22: * Helper Class to retrieve a valid string representation of the requested format.
23: *
24: * @author Eugen Neufeld
25: *
26: */
27: public final class NumberFormatHelper {
28:
29:         private NumberFormatHelper() {
30:         }
31:
32:         private static final String EMF_META_DATA = "http:///org/eclipse/emf/ecore/util/ExtendedMetaData"; //$NON-NLS-1$
33:         private static final String FRACTION_DIGITS = "fractionDigits"; //$NON-NLS-1$
34:         private static final String TOTAL_DIGITS = "totalDigits"; //$NON-NLS-1$
35:
36:         /**
37:          * Retrieves a format string for numbers.
38:          *
39:          * @param eAttribute The {@link EAttribute}
40:          * @return The retrieved format string or null if it is no decimal number
41:          */
42:         public static String getNumberFormat(EAttribute eAttribute) {
43:                 final EDataType eDataType = eAttribute.getEAttributeType();
44:•                if (!Number.class.isAssignableFrom(eDataType.getInstanceClass())) {
45:                         return null;
46:                 }
47:
48:                 final EAnnotation eAnnotation = eDataType.getEAnnotation(EMF_META_DATA);
49:•                if (eAnnotation == null) {
50:                         return null;
51:                 }
52:                 final EMap<String, String> details = eAnnotation.getDetails();
53:•                if (details == null) {
54:                         return null;
55:                 }
56:                 int numTotalDigits = -1;
57:•                if (details.containsKey(TOTAL_DIGITS)) {
58:                         numTotalDigits = Integer.parseInt(details.get(TOTAL_DIGITS));
59:                 }
60:                 int numFractionDigits = -1;
61:•                if (details.containsKey(FRACTION_DIGITS)) {
62:                         numFractionDigits = Integer.parseInt(details.get(FRACTION_DIGITS));
63:                 }
64:•                if (numTotalDigits > 0 && numFractionDigits > 0) {
65:
66:                         final StringBuilder sb = new StringBuilder();
67:                         sb.append("0"); //$NON-NLS-1$
68:                         sb.append("."); //$NON-NLS-1$
69:•                        for (int i = 0; i < numFractionDigits; i++) {
70:                                 sb.append("0"); //$NON-NLS-1$
71:                         }
72:                         return sb.toString();
73:                 }
74:                 return null;
75:         }
76: }