Skip to content

Package: LocalizedEnumeratorComparator

LocalizedEnumeratorComparator

nameinstructionbranchcomplexitylinemethod
LocalizedEnumeratorComparator(EMFFormsLocalizationService, BundleResolver, ReportService)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
compare(EStructuralFeature, Enumerator, Enumerator)
M: 0 C: 54
100%
M: 0 C: 6
100%
M: 0 C: 4
100%
M: 0 C: 15
100%
M: 0 C: 1
100%
lambda$0(EStructuralFeature, String, EClassifier)
M: 0 C: 30
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
lambda$1(String, Enumerator, Bundle)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
lambda$2(String, Enumerator, Bundle)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

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: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.table.swt;
15:
16: import java.text.MessageFormat;
17: import java.util.HashMap;
18: import java.util.Map;
19: import java.util.Optional;
20:
21: import org.eclipse.core.runtime.IStatus;
22: import org.eclipse.emf.common.util.Enumerator;
23: import org.eclipse.emf.ecore.EClassifier;
24: import org.eclipse.emf.ecore.EStructuralFeature;
25: import org.eclipse.emfforms.spi.common.BundleResolver;
26: import org.eclipse.emfforms.spi.common.BundleResolver.NoBundleFoundException;
27: import org.eclipse.emfforms.spi.common.report.AbstractReport;
28: import org.eclipse.emfforms.spi.common.report.ReportService;
29: import org.eclipse.emfforms.spi.common.sort.NumberAwareStringComparator;
30: import org.eclipse.emfforms.spi.localization.EMFFormsLocalizationService;
31: import org.osgi.framework.Bundle;
32:
33: /**
34: * Retrieves the localized labels for two {@link Enumerator} values and compares them to each other.
35: * Enumerators are localized by retrieving their literals' labels from the corresponding edit bundle by using the
36: * {@link EMFFormsLocalizationService}. Retrieved edit bundles are cached to avoid re-resolving them for every
37: * comparison.
38: *
39: * @author Lucas Koehler
40: * @since 1.22
41: *
42: */
43: public class LocalizedEnumeratorComparator implements FeatureAwareComparator<Enumerator> {
44:
45:         /**
46:          * Template to generate the localization key for an enum value. First parameter is the EEnum's type name, second
47:          * parameter is the value's name.
48:          */
49:         private static final String ENUM_KEY_TEMPLATE = "_UI_%s_%s_literal"; //$NON-NLS-1$
50:
51:         // use Optional to avoid trying to resolve absent edit bundles over and over again
52:         private final Map<EClassifier, Optional<Bundle>> editBundles = new HashMap<>();
53:         private final EMFFormsLocalizationService localizationService;
54:         private final BundleResolver bundleResolver;
55:         private final ReportService reportService;
56:
57:         /**
58:          * Default constructor.
59:          *
60:          * @param localizationService The {@link EMFFormsLocalizationService} to get the localized literal labels
61:          * @param bundleResolver The {@link BundleResolver} to resolve the edit bundles
62:          * @param reportService The {@link ReportService} to report missing edit bundles
63:          */
64:         public LocalizedEnumeratorComparator(EMFFormsLocalizationService localizationService, BundleResolver bundleResolver,
65:                 ReportService reportService) {
66:                 this.localizationService = localizationService;
67:                 this.bundleResolver = bundleResolver;
68:                 this.reportService = reportService;
69:         }
70:
71:         @Override
72:         public int compare(EStructuralFeature feature, Enumerator leftValue, Enumerator rightValue) {
73:•                if (leftValue == null) {
74:•                        if (rightValue == null) {
75:                                 return 0;
76:                         }
77:                         return 1;
78:•                } else if (rightValue == null) {
79:                         return -1;
80:                 }
81:
82:                 final String typeName = feature.getEType().getName();
83:                 final Optional<Bundle> editBundle = editBundles.computeIfAbsent(feature.getEType(), key -> {
84:                         try {
85:                                 return Optional.of(bundleResolver.getEditBundle(feature.getEContainingClass()));
86:                         } catch (final NoBundleFoundException ex) {
87:                                 reportService
88:                                         .report(new AbstractReport(
89:                                                 MessageFormat.format(
90:                                                         "No edit bundle was found for EEnum ''{0}''. Hence, its literals cannot be internationalized for feature ''{1}''.", //$NON-NLS-1$
91:                                                         typeName, feature.getName()),
92:                                                 IStatus.WARNING));
93:                         }
94:                         return Optional.empty();
95:                 });
96:
97:                 final String leftLabel = editBundle
98:                         .map(eB -> localizationService.getString(eB,
99:                                 String.format(ENUM_KEY_TEMPLATE, typeName, leftValue.getName())))
100:                         .orElse(leftValue.getLiteral());
101:
102:                 final String rightLabel = editBundle
103:                         .map(eB -> localizationService.getString(eB,
104:                                 String.format(ENUM_KEY_TEMPLATE, typeName, rightValue.getName())))
105:                         .orElse(rightValue.getLiteral());
106:
107:                 return NumberAwareStringComparator.getInstance().compare(leftLabel, rightLabel);
108:         }
109: }