moar documentation and unit test
This commit is contained in:
parent
90b8b201db
commit
f06d01718a
2 changed files with 61 additions and 0 deletions
|
@ -16,6 +16,9 @@
|
||||||
package com.redhat.trie;
|
package com.redhat.trie;
|
||||||
|
|
||||||
public class HuffNode {
|
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();
|
public static final Object END_NODE = new Object();
|
||||||
|
|
||||||
private long id = 0;
|
private long id = 0;
|
||||||
|
@ -25,10 +28,18 @@ public class HuffNode {
|
||||||
private HuffNode right = null;
|
private HuffNode right = null;
|
||||||
private NodeContext ctx = 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) {
|
public HuffNode(Object value, int weight, HuffNode left, HuffNode right) {
|
||||||
this(new NodeContext(), value, weight, left, 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) {
|
public HuffNode(NodeContext ctx, Object value, int weight, HuffNode left, HuffNode right) {
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
@ -38,10 +49,18 @@ public class HuffNode {
|
||||||
this.id = this.ctx.nextId();
|
this.id = this.ctx.nextId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* storage of Object's value, and its weighting, assumes new NodeContext
|
||||||
|
*/
|
||||||
public HuffNode(Object value, int weight) {
|
public HuffNode(Object value, int weight) {
|
||||||
this(new NodeContext(), value, 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) {
|
public HuffNode(NodeContext ctx, Object value, int weight) {
|
||||||
this.ctx = ctx;
|
this.ctx = ctx;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
@ -49,30 +68,51 @@ public class HuffNode {
|
||||||
this.id = this.ctx.nextId();
|
this.id = this.ctx.nextId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return the NodeContext incrementor
|
||||||
|
*/
|
||||||
public NodeContext getContext() {
|
public NodeContext getContext() {
|
||||||
return this.ctx;
|
return this.ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return this node's id, per the NodeContext incrementor
|
||||||
|
*/
|
||||||
public long getId() {
|
public long getId() {
|
||||||
return this.id;
|
return this.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return the stored Object
|
||||||
|
*/
|
||||||
public Object getValue() {
|
public Object getValue() {
|
||||||
return this.value;
|
return this.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* return the node's weight
|
||||||
|
*/
|
||||||
public int getWeight() {
|
public int getWeight() {
|
||||||
return this.weight;
|
return this.weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* node on the left
|
||||||
|
*/
|
||||||
public HuffNode getLeft() {
|
public HuffNode getLeft() {
|
||||||
return this.left;
|
return this.left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* node on the right
|
||||||
|
*/
|
||||||
public HuffNode getRight() {
|
public HuffNode getRight() {
|
||||||
return this.right;
|
return this.right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* pretty information
|
||||||
|
*/
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "ID: " + id +
|
return "ID: " + id +
|
||||||
", Value " + value +
|
", Value " + value +
|
||||||
|
|
|
@ -63,9 +63,30 @@ public class TestHuffNode {
|
||||||
HuffNode hn = new HuffNode(ctx, new Object(), 0);
|
HuffNode hn = new HuffNode(ctx, new Object(), 0);
|
||||||
assertEquals(hn.getContext(), ctx);
|
assertEquals(hn.getContext(), ctx);
|
||||||
|
|
||||||
|
assertEquals(hn.getId(), 0);
|
||||||
// it's +1 because the HuffNode will have run ctx.nextId()
|
// it's +1 because the HuffNode will have run ctx.nextId()
|
||||||
assertEquals(hn.getContext().getId(), 1);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue