PageRenderTime 34ms CodeModel.GetById 15ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/edu/uncc/parsets/data/CategoryTree.java

https://code.google.com/p/parsets/
Java | 133 lines | 77 code | 28 blank | 28 comment | 15 complexity | 662b339c5003d024d62fb333b4bf2d55 MD5 | raw file
  1package edu.uncc.parsets.data;
  2
  3import java.util.ArrayList;
  4import java.util.Iterator;
  5import java.util.List;
  6import java.util.TreeMap;
  7
  8/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  9 * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
 10 *                     and others (see Authors.txt for full list)
 11 * All rights reserved.
 12 * 
 13 * Redistribution and use in source and binary forms, with or without
 14 * modification, are permitted provided that the following conditions are met:
 15 * 
 16 *    * Redistributions of source code must retain the above copyright
 17 *      notice, this list of conditions and the following disclaimer.
 18 *    * Redistributions in binary form must reproduce the above copyright
 19 *      notice, this list of conditions and the following disclaimer in the
 20 *      documentation and/or other materials provided with the distribution.
 21 *    * Neither the name of UNC Charlotte nor the names of its contributors
 22 *      may be used to endorse or promote products derived from this software
 23 *      without specific prior written permission.
 24 *      
 25 * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
 26 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 28 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 29 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 32 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 35\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 36
 37public class CategoryTree implements Iterable<List<CategoryNode>> {
 38
 39	private ArrayList<List<CategoryNode>> levels;
 40
 41	private ArrayList<TreeMap<CategoryHandle, List<CategoryNode>>> catGroups;
 42
 43	private CategoryNode rootNode;
 44	
 45	public CategoryTree(int numLevels) {
 46		levels = new ArrayList<List<CategoryNode>>(numLevels);
 47		catGroups = new ArrayList<TreeMap<CategoryHandle,List<CategoryNode>>>(numLevels);
 48		for (int i = 0; i < numLevels; i++) {
 49			levels.add(new ArrayList<CategoryNode>());
 50			catGroups.add(new TreeMap<CategoryHandle, List<CategoryNode>>());
 51		}
 52	}
 53	
 54	public void addtoLevel(int level, CategoryNode node) {
 55		List<CategoryNode> levelList = levels.get(level);
 56		levelList.add(node);
 57		TreeMap<CategoryHandle, List<CategoryNode>> group = catGroups.get(level);
 58		if (level == 0)
 59			rootNode = node;
 60		else {
 61			List<CategoryNode> catList = group.get(node.getToCategory());
 62			if (catList == null) {
 63				catList = new ArrayList<CategoryNode>();
 64				group.put(node.getToCategory(), catList);
 65			}
 66			catList.add(node);
 67		}
 68	}
 69	
 70	public List<CategoryNode> getLevelList(int level) {
 71		return levels.get(level);
 72	}
 73
 74	public List<CategoryNode> getNodesForCategory(int level, CategoryHandle category) {
 75		return catGroups.get(level).get(category);
 76	}
 77	
 78	public Iterator<List<CategoryNode>> iterator() {
 79		return levels.iterator();
 80	}
 81
 82	public CategoryNode getRootNode() {
 83		return rootNode;
 84	}
 85	
 86	public void print() {
 87		rootNode.print();
 88	}
 89	
 90	public float getFilteredFrequency(CategoryHandle cat) {
 91		
 92		float total = 0;
 93		
 94		List<CategoryNode> nodes = getLevelList(levels.size()-1);
 95
 96		for(CategoryNode node : nodes) {
 97			if (node.isVisible()) 
 98				total += node.getCount();
 99		}
100
101		return (float)getFilteredCount(cat)/total;
102	}
103
104	public int getFilteredCount(CategoryHandle cat) {
105		
106		int count = 0;
107
108		for (int i=0; i<levels.size(); i++) {
109			List<CategoryNode> nodes = getNodesForCategory(i, cat);
110			if (nodes != null)
111				for (CategoryNode node : nodes)
112					if (node.isVisible()) 
113						count += node.getCount();
114		}
115		
116		return count;
117	}
118	
119	public int getFilteredTotal() {
120		int total = 0;
121		
122		List<CategoryNode> nodes = getLevelList(levels.size()-1);
123
124		for(CategoryNode node : nodes) {
125			if (node.isVisible()) 
126				total += node.getCount();
127		}
128		
129		return total;
130
131	}
132	
133}