2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.api.schema;
10 import java.util.HashMap;
11 import java.util.IdentityHashMap;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 // FIXME: relocate to yang-data-util, where this can have a proper test suite
16 final class DuplicateFinder {
17 private final Map<NormalizedNode, DuplicateEntry> identities = new IdentityHashMap<>();
18 private final Map<NormalizedNode, DuplicateEntry> duplicates = new HashMap<>();
20 private DuplicateFinder() {
24 private void findDuplicates(final YangInstanceIdentifier path, final NormalizedNode node) {
25 final var i = identities.get(node);
27 final var d = duplicates.get(node);
29 final var n = new DuplicateEntry(path);
30 identities.put(node, n);
31 duplicates.put(node, n);
36 if (node instanceof NormalizedNodeContainer<?> container) {
37 for (var child : container.body()) {
38 findDuplicates(path.node(child.name()), child);
47 * Recursively scan a {@link NormalizedNode} instance and its children and produce a collection of
48 * {@link DuplicateEntry} objects. Each holds the original definition path and a list of hard/softlinks.
50 * @param node Root node, may not be null.
51 * @return List of entries
53 static Map<NormalizedNode, DuplicateEntry> findDuplicates(final NormalizedNode node) {
54 final DuplicateFinder finder = new DuplicateFinder();
55 finder.findDuplicates(YangInstanceIdentifier.of(), node);
56 return finder.identities;