From 4dbdeb49169f2fbc5f4451527cdab9ae60c5fcd2 Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 1 Nov 2012 17:14:34 -0400 Subject: [PATCH] working on the PathNode includes() logic, and moar documentation and unit testing --- pom.xml | 129 ++++++++++++------ src/main/java/com/redhat/trie/NodePair.java | 3 + src/main/java/com/redhat/trie/PathNode.java | 43 +++++- src/main/java/com/redhat/trie/PathTree.java | 35 ++--- .../java/com/redhat/trie/TestNodePair.java | 18 +++ .../java/com/redhat/trie/TestPathNode.java | 23 ++++ .../java/com/redhat/trie/TestPathTree.java | 50 +++++++ 7 files changed, 241 insertions(+), 60 deletions(-) create mode 100644 src/test/java/com/redhat/trie/TestNodePair.java create mode 100644 src/test/java/com/redhat/trie/TestPathNode.java create mode 100644 src/test/java/com/redhat/trie/TestPathTree.java diff --git a/pom.xml b/pom.xml index d2a0673..d76a86e 100644 --- a/pom.xml +++ b/pom.xml @@ -1,47 +1,92 @@ - 4.0.0 - org.redhat.trie - trie - 0.0.1-SNAPSHOT + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + org.redhat.trie + trie + 0.0.1-SNAPSHOT - - - junit - junit - 4.10 - test - - - org.bouncycastle - bcprov-jdk16 - 1.44 - - + + + junit + junit + 4.10 + test + + + org.bouncycastle + bcprov-jdk16 + 1.44 + + - - - - maven-compiler-plugin - 2.3.2 - - 1.6 - 1.6 - - - - org.apache.maven.plugins - maven-jar-plugin - 2.4 - - - - com.hashbangbash.trie.App - - - - - - + + + + maven-compiler-plugin + 2.3.2 + + 1.6 + 1.6 + + + + + org.apache.maven.plugins + maven-javadoc-plugin + + + + methodundertest + m + Method Under Test: + + + servicesused + m + Services Used: + + + testdescription + m + Description of Test: + + + datadependencies + m + Data Dependencies: + + + inputs + m + Inputs: + + + externalconsumers + m + External Consumers: + + + expectedresults + m + Expected Results: + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + com.hashbangbash.trie.App + + + + + + diff --git a/src/main/java/com/redhat/trie/NodePair.java b/src/main/java/com/redhat/trie/NodePair.java index 6abb38c..8a3cec4 100644 --- a/src/main/java/com/redhat/trie/NodePair.java +++ b/src/main/java/com/redhat/trie/NodePair.java @@ -36,6 +36,9 @@ public class NodePair implements Comparable { this.connection = connection; } + /** + * pretty information + */ public String toString() { return "Name: " + name + ", Connection: " + connection.getId(); } diff --git a/src/main/java/com/redhat/trie/PathNode.java b/src/main/java/com/redhat/trie/PathNode.java index 310f82f..124f52f 100644 --- a/src/main/java/com/redhat/trie/PathNode.java +++ b/src/main/java/com/redhat/trie/PathNode.java @@ -74,7 +74,7 @@ public class PathNode { /* * same number of children with the same names for child nodes */ - boolean isEquivalentTo(PathNode that) { + public boolean isEquivalentTo(PathNode that) { if (this.getChildren().size() != that.getChildren().size()) { return false; } @@ -95,6 +95,47 @@ public class PathNode { return true; } + /** + * check whether current PathNode, includes the paths in PathNode that, like a mask. + * + * TODO - this is a stub + * + * @param that PathNode to check for + * @return boolean of truth! + */ + public boolean includes(PathNode that) { + // if we are at the end of the tree we're checking against, + // then it includes everything up to this point. + if (this.getChildren().size() == 0 || that.getChildren().size() == 0) { + return true; + } + + // why can java allow a list of primitives ... + List found = new ArrayList(); + boolean result; + + for (NodePair thisnp : this.getChildren()) { + for (NodePair thatnp : that.getChildren()) { + // keep checking, even if we hist a variablized value + if (thisnp.getName().startsWith("$") || thisnp.getName().equals(thatnp.getName())) { + result = thisnp.getConnection().includes(thatnp.getConnection()); + found.add(new Boolean(result).booleanValue()); + break; + } + found.add(Boolean.FALSE); + } + } + + if (found.contains(Boolean.FALSE)) { + return false; + } else { + return true; + } + } + + /** + * pretty information + */ public String toString() { String parentList = ""; for (PathNode parent : parents) { diff --git a/src/main/java/com/redhat/trie/PathTree.java b/src/main/java/com/redhat/trie/PathTree.java index 6eed793..4a4400d 100644 --- a/src/main/java/com/redhat/trie/PathTree.java +++ b/src/main/java/com/redhat/trie/PathTree.java @@ -112,8 +112,8 @@ public class PathTree { */ public void setPayload(byte[] payload) { this.modified = true; - setNodeBits(null); - setNodeCount(0); + this.setNodeBits(null); + this.setNodeCount(0); this.pathNodeContext = new NodeContext(); this.huffNodeContext = new NodeContext(); @@ -190,10 +190,10 @@ public class PathTree { } catch (IOException ex) { throw new PayloadException(); } - setDictOffset(inf.getBytesRead()); + this.setDictOffset(inf.getBytesRead()); int weight = 1; - for (String name : byteArrayToStringList(baos.toByteArray())) { + for (String name : this.byteArrayToStringList(baos.toByteArray())) { this.pathDictionary.add(new HuffNode(getHuffNodeContext(), name, weight++)); } this.pathDictionary.add(new HuffNode(HuffNode.END_NODE, weight)); @@ -210,17 +210,18 @@ public class PathTree { */ private List getNodeDictionary() throws PayloadException { if (this.pathDictionary == null) { - getPathDictionary(); // this has to run before the nodeDictionary bits are ready + this.getPathDictionary(); // this has to run before the nodeDictionary bits are ready } if (this.modified || this.pathDictionary == null || this.nodeDictionary == null) { this.nodeDictionary = new ArrayList(); - setNodeBits(new StringBuffer()); + this.setNodeBits(new StringBuffer()); - ByteArrayInputStream bais = new ByteArrayInputStream(getPayload(), - (new Long(getDictOffset())).intValue(), (new Long(getPayload().length - getDictOffset()).intValue())); + ByteArrayInputStream bais = new ByteArrayInputStream(this.getPayload(), + (new Long(this.getDictOffset())).intValue(), + (new Long(this.getPayload().length - this.getDictOffset()).intValue())); int value = bais.read(); // check for size bits - setNodeCount(value); + this.setNodeCount(value); if (value > 127) { byte[] count = new byte[value - 128]; try { @@ -232,7 +233,7 @@ public class PathTree { for (int k = 0; k < value - 128; k++) { total = (total << 8) | (count[k] & 0xFF); } - setNodeCount(total); + this.setNodeCount(total); } value = bais.read(); while (value != -1) { @@ -240,11 +241,11 @@ public class PathTree { for (int pad = 0; pad < 8 - someBits.length(); pad++) { this.nodeBits.append("0"); } - this.nodeBits.append(someBits); + this.getNodeBits().append(someBits); value = bais.read(); } - for (int j = 0; j < getNodeCount(); j++) { + for (int j = 0; j < this.getNodeCount(); j++) { this.nodeDictionary.add(new HuffNode(new PathNode(getPathNodeContext()), j)); } } @@ -319,7 +320,7 @@ public class PathTree { public void setContentSets(List contentSets) throws PayloadException { this.modified = true; this.nodeBits = null; - setNodeCount(0); + this.setNodeCount(0); this.pathNodeContext = new NodeContext(); this.huffNodeContext = new NodeContext(); @@ -365,9 +366,9 @@ public class PathTree { PathNode endMarker = new PathNode(new NodeContext()); for (String path : contents) { StringTokenizer st = new StringTokenizer(path, "/"); - makePathForURL(st, parent, endMarker); + this.makePathForURL(st, parent, endMarker); } - condenseSubTreeNodes(endMarker); + this.condenseSubTreeNodes(endMarker); return parent; } @@ -768,7 +769,7 @@ public class PathTree { for (NodePair child : parent.getChildren()) { if (child.getName().equals(childVal) && !child.getConnection().equals(endMarker)) { - makePathForURL(st, child.getConnection(), endMarker); + this.makePathForURL(st, child.getConnection(), endMarker); isNew = false; } } @@ -778,7 +779,7 @@ public class PathTree { next = new PathNode(parent.getContext()); parent.addChild(new NodePair(childVal, next)); next.addParent(parent); - makePathForURL(st, next, endMarker); + this.makePathForURL(st, next, endMarker); } else { parent.addChild(new NodePair(childVal, endMarker)); if (!endMarker.getParents().contains(parent)) { diff --git a/src/test/java/com/redhat/trie/TestNodePair.java b/src/test/java/com/redhat/trie/TestNodePair.java new file mode 100644 index 0000000..0dc18c9 --- /dev/null +++ b/src/test/java/com/redhat/trie/TestNodePair.java @@ -0,0 +1,18 @@ +package com.redhat.trie; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +public class TestNodePair { + + @Test + public void testNew0() { + NodePair np = new NodePair("foo", new PathNode()); + assertNotNull(np); + } + + +} + diff --git a/src/test/java/com/redhat/trie/TestPathNode.java b/src/test/java/com/redhat/trie/TestPathNode.java new file mode 100644 index 0000000..d8bcda0 --- /dev/null +++ b/src/test/java/com/redhat/trie/TestPathNode.java @@ -0,0 +1,23 @@ +package com.redhat.trie; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +public class TestPathNode { + + @Test + public void testNew0() { + PathNode pn = new PathNode(); + assertNotNull(pn); + } + + @Test + public void testNew1() { + PathNode pn = new PathNode(new NodeContext()); + assertNotNull(pn); + } + +} + diff --git a/src/test/java/com/redhat/trie/TestPathTree.java b/src/test/java/com/redhat/trie/TestPathTree.java new file mode 100644 index 0000000..e832c67 --- /dev/null +++ b/src/test/java/com/redhat/trie/TestPathTree.java @@ -0,0 +1,50 @@ +package com.redhat.trie; + +import java.util.List; +import java.util.ArrayList; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.InputStream; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +public class TestPathTree { + + @Test + public void testNew0() { + PathTree pt = new PathTree(); + assertNotNull(pt); + } + + @Test + public void testNew1() { + PathTree pt = new PathTree(); + assertNotNull(pt); + } + + private InputStream resStream(String filename) { + return getClass().getClassLoader().getResourceAsStream(filename); + } + + private List loadContents(String filename) throws IOException { + String content; + List contentList = new ArrayList(); + InputStream in = resStream(filename); + BufferedReader br = new BufferedReader(new InputStreamReader(in)); + + try { + while ((content = br.readLine()) != null) { + contentList.add(content); + } + } catch (IOException ex) { + throw ex; + } + return contentList; + } +} + +