From 5a202c097e71ae363d6456a7e231a2d420fb3cfd Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 4 Oct 2012 14:35:31 -0400 Subject: [PATCH] starting the trie utility that can contruct trie from paths --- src/main/java/com/redhat/trie/Util.java | 65 +++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/main/java/com/redhat/trie/Util.java diff --git a/src/main/java/com/redhat/trie/Util.java b/src/main/java/com/redhat/trie/Util.java new file mode 100644 index 0000000..13744ae --- /dev/null +++ b/src/main/java/com/redhat/trie/Util.java @@ -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 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); + } + } + } + } + } +} +