Java Code Examples for org.apache.commons.collections4.Trie#put()

The following examples show how to use org.apache.commons.collections4.Trie#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinList.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
private static Trie<String, BinRecord> createTrie() {
    Trie<String, BinRecord> trie = new PatriciaTrie<>();
    Iterable<CSVRecord> records;
    try (InputStream in = BinList.class.getResourceAsStream(BINLIST);
            BOMInputStream bomStream = new BOMInputStream(in);
            InputStreamReader inStream =
                    new InputStreamReader(bomStream, StandardCharsets.UTF_8)) {
        records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(inStream).getRecords();
    } catch (NullPointerException | IOException e) {
        LOGGER.warn("Exception while loading: " + BINLIST, e);
        return trie;
    }

    for (CSVRecord record : records) {
        trie.put(
                record.get("bin"),
                new BinRecord(
                        record.get("bin"),
                        record.get("brand"),
                        record.get("category"),
                        record.get("issuer")));
    }
    return trie;
}
 
Example 2
Source File: MPQViewer.java    From riiablo with Apache License 2.0 6 votes vote down vote up
private void treeify(Trie<String, Node> nodes, Node root, String path) {
  Node parent = root;
  String[] parts = path.split("\\\\");
  StringBuilder builder = new StringBuilder(path.length());
  for (String part : parts) {
    if (part.isEmpty()) {
      break;
    }

    builder.append(part).append("\\");
    String partPath = builder.toString();
    Node node = nodes.get(partPath);
    if (node == null) {
      node = new BaseNode(new VisLabel(part));
      nodes.put(partPath, node);
      parent.add(node);
    }

    parent = node;
  }
}
 
Example 3
Source File: PatriciaTrieTest.java    From jesterj with Apache License 2.0 6 votes vote down vote up
public void testPrefixMapClear() {
    final Trie<CharSequence, Integer> trie = new PatriciaTrie<>();
    trie.put("Anna", 1);
    trie.put("Anael", 2);
    trie.put("Analu", 3);
    trie.put("Andreas", 4);
    trie.put("Andrea", 5);
    trie.put("Andres", 6);
    trie.put("Anatole", 7);
    final SortedMap<CharSequence, Integer> prefixMap = trie.prefixMap("And");
    assertEquals(new HashSet<>(Arrays.asList("Andrea", "Andreas", "Andres")), prefixMap.keySet());
    assertEquals(Arrays.asList(5, 4, 6), new ArrayList<>(prefixMap.values()));

    prefixMap.clear();
    assertTrue(prefixMap.isEmpty());
    assertTrue(prefixMap.keySet().isEmpty());
    assertTrue(prefixMap.values().isEmpty());
    assertEquals(new HashSet<>(Arrays.asList("Anael", "Analu", "Anatole", "Anna")), trie.keySet());
    assertEquals(Arrays.asList(2, 3, 7, 1), new ArrayList<>(trie.values()));
}
 
Example 4
Source File: PatriciaTrieTest.java    From jesterj with Apache License 2.0 6 votes vote down vote up
public void testPrefixMapClearUsingRemove() {
    final Trie<CharSequence, Integer> trie = new PatriciaTrie<>();
    trie.put("Anna", 1);
    trie.put("Anael", 2);
    trie.put("Analu", 3);
    trie.put("Andreas", 4);
    trie.put("Andrea", 5);
    trie.put("Andres", 6);
    trie.put("Anatole", 7);
    final SortedMap<CharSequence, Integer> prefixMap = trie.prefixMap("And");
    assertEquals(new HashSet<>(Arrays.asList("Andrea", "Andreas", "Andres")), prefixMap.keySet());
    assertEquals(Arrays.asList(5, 4, 6), new ArrayList<>(prefixMap.values()));

    final Set<CharSequence> keys = new HashSet<>(prefixMap.keySet());
    for (final CharSequence key : keys) {
        prefixMap.remove(key);
    }
    assertTrue(prefixMap.keySet().isEmpty());
    assertTrue(prefixMap.values().isEmpty());
    assertEquals(new HashSet<>(Arrays.asList("Anael", "Analu", "Anatole", "Anna")), trie.keySet());
    assertEquals(Arrays.asList(2, 3, 7, 1), new ArrayList<>(trie.values()));
}
 
Example 5
Source File: SuggestionServiceImpl.java    From intellij-spring-assistant with MIT License 4 votes vote down vote up
private void addPropertiesToIndex(Module module,
    Trie<String, MetadataSuggestionNode> rootSearchIndex,
    SpringConfigurationMetadata springConfigurationMetadata, String containerArchiveOrFileRef) {
  List<SpringConfigurationMetadataProperty> properties =
      springConfigurationMetadata.getProperties();
  properties.sort(comparing(SpringConfigurationMetadataProperty::getName));
  for (SpringConfigurationMetadataProperty property : properties) {
    String[] pathSegments = toSanitizedPathSegments(property.getName());
    String[] rawPathSegments = toRawPathSegments(property.getName());
    MetadataSuggestionNode closestMetadata =
        findDeepestMetadataMatch(rootSearchIndex, pathSegments, false);

    int startIndex;
    if (closestMetadata == null) { // path does not have a corresponding root element
      boolean onlyRootSegmentExists = pathSegments.length == 1;
      if (onlyRootSegmentExists) {
        closestMetadata = MetadataPropertySuggestionNode
            .newInstance(rawPathSegments[0], property, null, containerArchiveOrFileRef);
      } else {
        closestMetadata = MetadataNonPropertySuggestionNode
            .newInstance(rawPathSegments[0], null, containerArchiveOrFileRef);
      }
      rootSearchIndex.put(pathSegments[0], closestMetadata);

      // since we already handled the root level item, let addChildren start from index 1 of pathSegments
      startIndex = 1;
    } else {
      startIndex = closestMetadata.numOfHopesToRoot() + 1;
    }

    boolean haveMoreSegmentsLeft = startIndex < rawPathSegments.length;

    if (haveMoreSegmentsLeft) {
      if (!closestMetadata.isProperty()) {
        MetadataNonPropertySuggestionNode.class.cast(closestMetadata)
            .addChildren(property, rawPathSegments, startIndex, containerArchiveOrFileRef);
      } else {
        log.warn("Detected conflict between a new group & existing property for suggestion path "
            + closestMetadata.getPathFromRoot(module)
            + ". Ignoring property. Existing non property node belongs to (" + closestMetadata
            .getBelongsTo().stream().collect(joining(",")) + "), New property belongs to "
            + containerArchiveOrFileRef);
      }
    } else {
      if (!closestMetadata.isProperty()) {
        log.warn(
            "Detected conflict between a new metadata property & existing non property node for suggestion path "
                + closestMetadata.getPathFromRoot(module)
                + ". Ignoring property. Existing non property node belongs to (" + closestMetadata
                .getBelongsTo().stream().collect(joining(",")) + "), New property belongs to "
                + containerArchiveOrFileRef);
      } else {
        closestMetadata.addRefCascadeTillRoot(containerArchiveOrFileRef);
        log.debug("Detected a duplicate metadata property for suggestion path " + closestMetadata
            .getPathFromRoot(module) + ". Ignoring property. Existing property belongs to ("
            + closestMetadata.getBelongsTo().stream().collect(joining(","))
            + "), New property belongs to " + containerArchiveOrFileRef);
      }
    }
  }
}
 
Example 6
Source File: SuggestionServiceImpl.java    From intellij-spring-assistant with MIT License 4 votes vote down vote up
private void addGroupsToIndex(Module module, Trie<String, MetadataSuggestionNode> rootSearchIndex,
    SpringConfigurationMetadata springConfigurationMetadata, String containerArchiveOrFileRef) {
  List<SpringConfigurationMetadataGroup> groups = springConfigurationMetadata.getGroups();
  if (groups != null) {
    groups.sort(comparing(SpringConfigurationMetadataGroup::getName));
    for (SpringConfigurationMetadataGroup group : groups) {
      String[] pathSegments = toSanitizedPathSegments(group.getName());
      String[] rawPathSegments = toRawPathSegments(group.getName());

      MetadataSuggestionNode closestMetadata = MetadataSuggestionNode.class
          .cast(findDeepestMetadataMatch(rootSearchIndex, pathSegments, false));

      int startIndex;
      if (closestMetadata == null) { // path does not have a corresponding root element
        // lets build just the root element. Rest of the path segments will be taken care of by the addChildren method
        boolean onlyRootSegmentExists = pathSegments.length == 1;
        MetadataNonPropertySuggestionNode newGroupSuggestionNode =
            MetadataNonPropertySuggestionNode
                .newInstance(rawPathSegments[0], null, containerArchiveOrFileRef);
        if (onlyRootSegmentExists) {
          newGroupSuggestionNode.setGroup(module, group);
        }
        rootSearchIndex.put(pathSegments[0], newGroupSuggestionNode);

        closestMetadata = newGroupSuggestionNode;
        // since we already handled the root level item, let addChildren start from index 1 of pathSegments
        startIndex = 1;
      } else {
        startIndex = closestMetadata.numOfHopesToRoot() + 1;
      }

      if (closestMetadata.isProperty()) {
        log.warn(
            "Detected conflict between an existing metadata property & new group for suggestion path "
                + closestMetadata.getPathFromRoot(module)
                + ". Ignoring new group. Existing Property belongs to (" + closestMetadata
                .getBelongsTo().stream().collect(joining(",")) + "), New Group belongs to "
                + containerArchiveOrFileRef);
      } else {
        // lets add container as a reference till root
        MetadataNonPropertySuggestionNode groupSuggestionNode =
            MetadataNonPropertySuggestionNode.class.cast(closestMetadata);
        groupSuggestionNode.addRefCascadeTillRoot(containerArchiveOrFileRef);

        boolean haveMoreSegmentsLeft = startIndex < rawPathSegments.length;
        if (haveMoreSegmentsLeft) {
          groupSuggestionNode
              .addChildren(module, group, rawPathSegments, startIndex, containerArchiveOrFileRef);
        } else {
          // Node is an intermediate node that has neither group nor property assigned to it, lets assign this group to it
          // Can happen when `a.b.c` is already added to the metadata tree from an earlier metadata source & now we are trying to add a group for `a.b`
          // In this e.g, startIndex would be 2. So, there is no point in adding children. We only need to update the tree appropriately
          groupSuggestionNode.setGroup(module, group);
        }
      }
    }
  }
}