Skip to content

Commit 7aea68b

Browse files
authored
Merge pull request #1228 from ava-fred/work.251117
feat: add getMappings method to QualifiedNameLookup
2 parents 68b5243 + 1f59632 commit 7aea68b

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookupTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Collections;
2323
import java.util.List;
2424

25+
import org.apache.log4j.LogManager;
26+
import org.apache.log4j.Logger;
2527
import org.eclipse.emf.common.util.URI;
2628
import org.eclipse.xtext.naming.QualifiedName;
2729
import org.junit.jupiter.api.Test;
@@ -32,6 +34,8 @@
3234
@SuppressWarnings({"nls", "unused", "PMD.JUnitAssertionsShouldIncludeMessage"})
3335
// CHECKSTYLE:CHECK-OFF MultipleStringLiteralsCheck
3436
public class QualifiedNameSegmentTreeLookupTest {
37+
private static final Logger LOGGER = LogManager.getLogger(QualifiedNameSegmentTreeLookupTest.class);
38+
3539
private final QualifiedNameSegmentTreeLookup<URI> lookup = new QualifiedNameSegmentTreeLookup<URI>(URI.class, true);
3640

3741
@Test
@@ -173,6 +177,42 @@ public void testLoadStore() throws IOException, ClassNotFoundException {
173177
}
174178
}
175179

180+
@Test
181+
public void testGetMappings() {
182+
final QualifiedName a = name("A");
183+
final QualifiedName b = name("B");
184+
final QualifiedName c = name("A.C");
185+
final QualifiedName d = name("A.D");
186+
final QualifiedName e = name("B.E");
187+
final QualifiedName f = name("B.F");
188+
final QualifiedName g = name("A.C.G");
189+
final QualifiedName h = name("A.C.H");
190+
final QualifiedName i = name("A.D.I");
191+
final QualifiedName j = name("A.D.J");
192+
193+
List<QualifiedName> nameList = List.of(a, b, c, d, e, f, g, h, i, j);
194+
for (QualifiedName qn : nameList) {
195+
lookup.put(qn, uri(qn));
196+
}
197+
198+
URI value = URI.createURI("scheme:/host");
199+
200+
lookup.put(c, value);
201+
lookup.put(g, value);
202+
lookup.put(h, value);
203+
lookup.put(f, value);
204+
lookup.put(b, value);
205+
206+
Collection<QualifiedName> result = lookup.getMappings(value);
207+
Collection<QualifiedName> expected = List.of(c, g, h, f, b);
208+
209+
assertContentEquals(expected, result);
210+
211+
URI noSuchValue = URI.createURI("scheme:/anotherHost");
212+
expected = lookup.getMappings(noSuchValue);
213+
assertEquals(expected.size(), 0);
214+
}
215+
176216
private QualifiedName name(final String str) {
177217
return QualifiedNames.safeQualifiedName(str);
178218
}

com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameLookup.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public interface QualifiedNameLookup<T> extends ICache<QualifiedName, T> {
4848
*/
4949
Collection<T> get(QualifiedNamePattern pattern, boolean excludeDuplicates);
5050

51+
/**
52+
* Returns a collection of {@link QualifiedName}s the given value is mapped to.
53+
*
54+
* @param value
55+
* value to find all mappings for, must not be {@code null}
56+
* @return a {@link Collection} of {@link QualifiedName}s the given value is mapped to, never {@code null}
57+
*/
58+
Collection<QualifiedName> getMappings(T value);
59+
5160
/**
5261
* Adds a mapping for the given key and value. If there already are values associated with the given key, then the given value will be added (unless already
5362
* present).

com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookup.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,27 @@ public void accept(final Visitor visitor) {
284284
}
285285
}
286286

287+
public List<List<String>> getMappings(final Object value) {
288+
List<List<String>> result = new ArrayList<>();
289+
if (children != null) {
290+
for (SegmentNode node : children) {
291+
for (List<String> mapping : node.getMappings(value)) {
292+
if (!segment.isBlank()) {
293+
mapping.add(0, segment);
294+
}
295+
result.add(mapping);
296+
}
297+
}
298+
}
299+
if (ArrayUtils.find(values, value) >= 0) {
300+
List<String> l = new ArrayList<>(1);
301+
l.add(segment);
302+
result.add(l);
303+
}
304+
305+
return result;
306+
}
307+
287308
/**
288309
* Adopted from {@link java.util.Collections#binarySearch(List, Object)} which can't be used here because the element being searched has a different type.
289310
*
@@ -538,6 +559,22 @@ public Collection<T> get(final QualifiedNamePattern pattern, final boolean exclu
538559
return root.matches(pattern.lowerInclusive(), 0, root.find(pattern.upperExclusive(), 0, false), pattern.isRecursivePattern(), excludeDuplicates);
539560
}
540561

562+
@Override
563+
public Collection<QualifiedName> getMappings(final T value) {
564+
565+
if (value == null) {
566+
throw new IllegalArgumentException("QualifiedNameLookup does not support null values"); //$NON-NLS-1$
567+
}
568+
569+
List<List<String>> mappings = root.getMappings(value);
570+
List<QualifiedName> result = new ArrayList<>(mappings.size());
571+
for (List<String> mapping : mappings) {
572+
result.add(QualifiedName.create(mapping));
573+
}
574+
575+
return result;
576+
}
577+
541578
@Override
542579
@SuppressFBWarnings("AT_NONATOMIC_OPERATIONS_ON_SHARED_VARIABLE")
543580
public void put(final QualifiedName name, final T value) {

com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/naming/TreeSetLookup.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*******************************************************************************/
1111
package com.avaloq.tools.ddk.xtext.naming;
1212

13+
import java.util.ArrayList;
1314
import java.util.Collection;
1415
import java.util.Iterator;
1516
import java.util.Map;
@@ -71,6 +72,19 @@ public Collection<T> get(final QualifiedNamePattern pattern, final boolean exclu
7172
return result;
7273
}
7374

75+
@Override
76+
public Collection<QualifiedName> getMappings(final T value) {
77+
Collection<QualifiedName> mappings = new ArrayList<>();
78+
79+
for (Map.Entry<QualifiedName, Object[]> entry : lookupMap.entrySet()) {
80+
if (ArrayUtils.find(entry.getValue(), value) >= 0) {
81+
mappings.add(entry.getKey());
82+
}
83+
}
84+
85+
return mappings;
86+
}
87+
7488
@Override
7589
public void removeMappings(final T value) {
7690
Iterator<Map.Entry<QualifiedName, Object[]>> iter = lookupMap.entrySet().iterator();

0 commit comments

Comments
 (0)