Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Collections;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.eclipse.emf.common.util.URI;
import org.eclipse.xtext.naming.QualifiedName;
import org.junit.jupiter.api.Test;
Expand All @@ -32,6 +34,8 @@
@SuppressWarnings({"nls", "unused", "PMD.JUnitAssertionsShouldIncludeMessage"})
// CHECKSTYLE:CHECK-OFF MultipleStringLiteralsCheck
public class QualifiedNameSegmentTreeLookupTest {
private static final Logger LOGGER = LogManager.getLogger(QualifiedNameSegmentTreeLookupTest.class);

private final QualifiedNameSegmentTreeLookup<URI> lookup = new QualifiedNameSegmentTreeLookup<URI>(URI.class, true);

@Test
Expand Down Expand Up @@ -173,6 +177,42 @@ public void testLoadStore() throws IOException, ClassNotFoundException {
}
}

@Test
public void testGetMappings() {
final QualifiedName a = name("A");
final QualifiedName b = name("B");
final QualifiedName c = name("A.C");
final QualifiedName d = name("A.D");
final QualifiedName e = name("B.E");
final QualifiedName f = name("B.F");
final QualifiedName g = name("A.C.G");
final QualifiedName h = name("A.C.H");
final QualifiedName i = name("A.D.I");
final QualifiedName j = name("A.D.J");

List<QualifiedName> nameList = List.of(a, b, c, d, e, f, g, h, i, j);
for (QualifiedName qn : nameList) {
lookup.put(qn, uri(qn));
}

URI value = URI.createURI("scheme:/host");

lookup.put(c, value);
lookup.put(g, value);
lookup.put(h, value);
lookup.put(f, value);
lookup.put(b, value);

Collection<QualifiedName> result = lookup.getMappings(value);
Collection<QualifiedName> expected = List.of(c, g, h, f, b);

assertContentEquals(expected, result);

URI noSuchValue = URI.createURI("scheme:/anotherHost");
expected = lookup.getMappings(noSuchValue);
assertEquals(expected.size(), 0);
}

private QualifiedName name(final String str) {
return QualifiedNames.safeQualifiedName(str);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public interface QualifiedNameLookup<T> extends ICache<QualifiedName, T> {
*/
Collection<T> get(QualifiedNamePattern pattern, boolean excludeDuplicates);

/**
* Returns a collection of {@link QualifiedName}s the given value is mapped to.
*
* @param value
* value to find all mappings for, must not be {@code null}
* @return a {@link Collection} of {@link QualifiedName}s the given value is mapped to, never {@code null}
*/
Collection<QualifiedName> getMappings(T value);

/**
* 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
* present).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,27 @@ public void accept(final Visitor visitor) {
}
}

public List<List<String>> getMappings(final Object value) {
List<List<String>> result = new ArrayList<>();
if (children != null) {
for (SegmentNode node : children) {
for (List<String> mapping : node.getMappings(value)) {
if (!segment.isBlank()) {
mapping.add(0, segment);
}
result.add(mapping);
}
}
}
if (ArrayUtils.find(values, value) >= 0) {
List<String> l = new ArrayList<>(1);
l.add(segment);
result.add(l);
}

return result;
}

/**
* Adopted from {@link java.util.Collections#binarySearch(List, Object)} which can't be used here because the element being searched has a different type.
*
Expand Down Expand Up @@ -538,6 +559,22 @@ public Collection<T> get(final QualifiedNamePattern pattern, final boolean exclu
return root.matches(pattern.lowerInclusive(), 0, root.find(pattern.upperExclusive(), 0, false), pattern.isRecursivePattern(), excludeDuplicates);
}

@Override
public Collection<QualifiedName> getMappings(final T value) {

if (value == null) {
throw new IllegalArgumentException("QualifiedNameLookup does not support null values"); //$NON-NLS-1$
}

List<List<String>> mappings = root.getMappings(value);
List<QualifiedName> result = new ArrayList<>(mappings.size());
for (List<String> mapping : mappings) {
result.add(QualifiedName.create(mapping));
}

return result;
}

@Override
@SuppressFBWarnings("AT_NONATOMIC_OPERATIONS_ON_SHARED_VARIABLE")
public void put(final QualifiedName name, final T value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package com.avaloq.tools.ddk.xtext.naming;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -71,6 +72,19 @@ public Collection<T> get(final QualifiedNamePattern pattern, final boolean exclu
return result;
}

@Override
public Collection<QualifiedName> getMappings(final T value) {
Collection<QualifiedName> mappings = new ArrayList<>();

for (Map.Entry<QualifiedName, Object[]> entry : lookupMap.entrySet()) {
if (ArrayUtils.find(entry.getValue(), value) >= 0) {
mappings.add(entry.getKey());
}
}

return mappings;
}

@Override
public void removeMappings(final T value) {
Iterator<Map.Entry<QualifiedName, Object[]>> iter = lookupMap.entrySet().iterator();
Expand Down