moar documentation and unit test

This commit is contained in:
Vincent Batts 2012-11-01 13:29:33 -04:00
parent 90b8b201db
commit f06d01718a
2 changed files with 61 additions and 0 deletions

View File

@ -16,6 +16,9 @@
package com.redhat.trie;
public class HuffNode {
/**
* Empty node to be referenced as the authority on what is an "end node"
*/
public static final Object END_NODE = new Object();
private long id = 0;
@ -25,10 +28,18 @@ public class HuffNode {
private HuffNode right = null;
private NodeContext ctx = null;
/**
* storage of Object value, it's weighting, and children, assumes new NodeContext
*/
public HuffNode(Object value, int weight, HuffNode left, HuffNode right) {
this(new NodeContext(), value, weight, left, right);
}
/**
* storage of Object value, it's weighting, and children, and existing NodeContext.
*
* This will increment the NodeContext
*/
public HuffNode(NodeContext ctx, Object value, int weight, HuffNode left, HuffNode right) {
this.ctx = ctx;
this.value = value;
@ -38,10 +49,18 @@ public class HuffNode {
this.id = this.ctx.nextId();
}
/**
* storage of Object's value, and its weighting, assumes new NodeContext
*/
public HuffNode(Object value, int weight) {
this(new NodeContext(), value, weight);
}
/**
* storage of Object's value, and its weighting, and existing NodeContext.
*
* This will increment the NodeContext
*/
public HuffNode(NodeContext ctx, Object value, int weight) {
this.ctx = ctx;
this.value = value;
@ -49,30 +68,51 @@ public class HuffNode {
this.id = this.ctx.nextId();
}
/**
* return the NodeContext incrementor
*/
public NodeContext getContext() {
return this.ctx;
}
/**
* return this node's id, per the NodeContext incrementor
*/
public long getId() {
return this.id;
}
/**
* return the stored Object
*/
public Object getValue() {
return this.value;
}
/**
* return the node's weight
*/
public int getWeight() {
return this.weight;
}
/**
* node on the left
*/
public HuffNode getLeft() {
return this.left;
}
/**
* node on the right
*/
public HuffNode getRight() {
return this.right;
}
/**
* pretty information
*/
public String toString() {
return "ID: " + id +
", Value " + value +

View File

@ -63,9 +63,30 @@ public class TestHuffNode {
HuffNode hn = new HuffNode(ctx, new Object(), 0);
assertEquals(hn.getContext(), ctx);
assertEquals(hn.getId(), 0);
// it's +1 because the HuffNode will have run ctx.nextId()
assertEquals(hn.getContext().getId(), 1);
}
@Test
public void testObject(){
Object obj = new Object();
HuffNode hn = new HuffNode(obj, 1);
assertEquals(hn.getValue(), obj);
}
@Test
public void testChildren(){
Object obj = new Object();
HuffNode hn = new HuffNode(obj,
1,
new HuffNode(obj, 2),
new HuffNode(obj, 3));
assertEquals(hn.getValue(), obj);
assertEquals(hn.getLeft().getValue(), obj);
assertEquals(hn.getRight().getValue(), obj);
}
}