From f06d01718acb73fe8e2c195f2e02eab8a53c08ff Mon Sep 17 00:00:00 2001 From: Vincent Batts Date: Thu, 1 Nov 2012 13:29:33 -0400 Subject: [PATCH] moar documentation and unit test --- src/main/java/com/redhat/trie/HuffNode.java | 40 +++++++++++++++++++ .../java/com/redhat/trie/TestHuffNode.java | 21 ++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/main/java/com/redhat/trie/HuffNode.java b/src/main/java/com/redhat/trie/HuffNode.java index b023ac3..174128e 100644 --- a/src/main/java/com/redhat/trie/HuffNode.java +++ b/src/main/java/com/redhat/trie/HuffNode.java @@ -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 + diff --git a/src/test/java/com/redhat/trie/TestHuffNode.java b/src/test/java/com/redhat/trie/TestHuffNode.java index c153103..ea995c0 100644 --- a/src/test/java/com/redhat/trie/TestHuffNode.java +++ b/src/test/java/com/redhat/trie/TestHuffNode.java @@ -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); + } + }