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