PageRenderTime 22ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/edu/uncc/parsets/data/CategoryNode.java

https://code.google.com/p/parsets/
Java | 178 lines | 115 code | 32 blank | 31 comment | 17 complexity | 0ca315c4f08fa5c178d58dfeaffb15bc MD5 | raw file
  1. package edu.uncc.parsets.data;
  2. import java.text.NumberFormat;
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  6. * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
  7. * and others (see Authors.txt for full list)
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * * Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * * Neither the name of UNC Charlotte nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
  23. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  26. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  31. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  33. public class CategoryNode implements Iterable<CategoryNode>, Comparable<CategoryNode> {
  34. private int count;
  35. private float ratio;
  36. private CategoryHandle toCategory;
  37. private boolean visible;
  38. private CategoryNode parent;
  39. private ArrayList<CategoryNode> children = new ArrayList<CategoryNode>();
  40. private String pathName;
  41. public CategoryNode(CategoryNode parent, CategoryHandle toCategory, int count) {
  42. this.parent = parent;
  43. this.toCategory = toCategory;
  44. this.count = count;
  45. if (parent != null) {
  46. parent.addChild(this);
  47. pathName = parent.pathName+"/"+toCategory.getName();
  48. } else
  49. pathName = "/";
  50. visible = true;
  51. }
  52. public void setCount(int count){
  53. this.count = count;
  54. }
  55. public int getCount() {
  56. return count;
  57. }
  58. public float getRatio() {
  59. return ratio;
  60. }
  61. public String getTooltipText(int filteredTotal) {
  62. CategoryNode node = this;
  63. ArrayList<CategoryHandle> cats = new ArrayList<CategoryHandle>();
  64. while (node.getParent() != null) {
  65. cats.add(0, node.getToCategory());
  66. node = node.getParent();
  67. }
  68. String s = "";
  69. for(CategoryHandle category : cats) {
  70. s += category.getName();
  71. if (cats.indexOf(category) < cats.size()-1) {
  72. s += ", ";
  73. }
  74. }
  75. s += "\n" + count + ", ";
  76. float percentage = ((float)count/(float)filteredTotal)*100f;
  77. if (percentage < 1) {
  78. NumberFormat f = NumberFormat.getInstance();
  79. f.setMaximumFractionDigits(2);
  80. s += f.format(percentage) + "%";
  81. }
  82. else
  83. s += (int)percentage + "%";
  84. return s;
  85. }
  86. public CategoryHandle getToCategory() {
  87. return toCategory;
  88. }
  89. public void addChild(CategoryNode n) {
  90. children.add(n);
  91. }
  92. public Iterator<CategoryNode> iterator() {
  93. return children.iterator();
  94. }
  95. public CategoryNode getParent() {
  96. return parent;
  97. }
  98. public void updateValues() {
  99. if (children.size() > 0) {
  100. count = 0;
  101. for (CategoryNode n : children) {
  102. if (n.isVisible()) {
  103. n.updateValues();
  104. count += n.count;
  105. }
  106. }
  107. for (CategoryNode n : children) {
  108. if (n.isVisible())
  109. n.ratio = (float)n.count/(float)count;
  110. }
  111. }
  112. }
  113. public void print() {
  114. System.err.println(pathName+" => "+count);
  115. for (CategoryNode n : children)
  116. n.print();
  117. }
  118. /**
  119. * @return the children
  120. */
  121. public ArrayList<CategoryNode> getChildren() {
  122. return children;
  123. }
  124. public boolean isVisible() {
  125. return visible;
  126. }
  127. public void setVisible(boolean visible) {
  128. this.visible = visible;
  129. for (CategoryNode child : children) {
  130. child.setVisible(visible);
  131. }
  132. }
  133. @Override
  134. public int compareTo(CategoryNode o) {
  135. return pathName.compareTo(o.pathName);
  136. }
  137. @Override
  138. public boolean equals(Object o) {
  139. return (o instanceof CategoryNode) && compareTo((CategoryNode)o) == 0;
  140. }
  141. @Override
  142. public int hashCode() {
  143. assert false : "hashCode not designed";
  144. return 42; // any arbitrary constant will do
  145. }
  146. }