starting the trie utility that can contruct trie from paths
This commit is contained in:
parent
9520047496
commit
5a202c097e
1 changed files with 65 additions and 0 deletions
65
src/main/java/com/redhat/trie/Util.java
Normal file
65
src/main/java/com/redhat/trie/Util.java
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) 2009 - 2012 Red Hat, Inc.
|
||||||
|
*
|
||||||
|
* This software is licensed to you under the GNU General Public License,
|
||||||
|
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
|
||||||
|
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
|
||||||
|
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
|
||||||
|
* along with this software; if not, see
|
||||||
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
|
||||||
|
*
|
||||||
|
* Red Hat trademarks are not licensed under GPLv2. No permission is
|
||||||
|
* granted to use or replicate Red Hat trademarks that are incorporated
|
||||||
|
* in this software or its documentation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.redhat.trie;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.StringTokenizer;
|
||||||
|
|
||||||
|
public class Util {
|
||||||
|
public static PathNode makePathTree(List<String> contents, PathNode parent) {
|
||||||
|
PathNode endMarker = new PathNode();
|
||||||
|
for (String path : contents) {
|
||||||
|
StringTokenizer st = new StringTokenizer(path, "/");
|
||||||
|
makePathForURL(st, parent, endMarker);
|
||||||
|
}
|
||||||
|
//condenseSubTreeNodes(endMarker);
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void makePathForURL(StringTokenizer st, PathNode parent, PathNode endMarker) {
|
||||||
|
if (st.hasMoreTokens()) {
|
||||||
|
String childVal = st.nextToken();
|
||||||
|
if (childVal.equals("")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean isNew = true;
|
||||||
|
for (NodePair child : parent.getChildren()) {
|
||||||
|
if (child.getName().equals(childVal) &&
|
||||||
|
!child.getConnection().equals(endMarker)) {
|
||||||
|
makePathForURL(st, child.getConnection(), endMarker);
|
||||||
|
isNew = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isNew) {
|
||||||
|
PathNode next = null;
|
||||||
|
if (st.hasMoreTokens()) {
|
||||||
|
next = new PathNode();
|
||||||
|
parent.addChild(new NodePair(childVal, next));
|
||||||
|
next.addParent(parent);
|
||||||
|
makePathForURL(st, next, endMarker);
|
||||||
|
} else {
|
||||||
|
parent.addChild(new NodePair(childVal, endMarker));
|
||||||
|
if (!endMarker.getParents().contains(parent)) {
|
||||||
|
endMarker.addParent(parent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue