PageRenderTime 69ms CodeModel.GetById 33ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 1ms

/edu/uncc/parsets/data/old/DataDimension.java

https://code.google.com/p/parsets/
Java | 186 lines | 120 code | 32 blank | 34 comment | 20 complexity | d5dfdcccb56062733bc5a041be6d70b1 MD5 | raw file
  1package edu.uncc.parsets.data.old;
  2
  3import java.util.ArrayList;
  4import java.util.List;
  5import java.util.Map;
  6import java.util.TreeMap;
  7
  8import edu.uncc.parsets.data.DataType;
  9import edu.uncc.parsets.util.PSLogging;
 10
 11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 12 * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
 13 *                     and others (see Authors.txt for full list)
 14 * All rights reserved.
 15 * 
 16 * Redistribution and use in source and binary forms, with or without
 17 * modification, are permitted provided that the following conditions are met:
 18 * 
 19 *    * Redistributions of source code must retain the above copyright
 20 *      notice, this list of conditions and the following disclaimer.
 21 *    * Redistributions in binary form must reproduce the above copyright
 22 *      notice, this list of conditions and the following disclaimer in the
 23 *      documentation and/or other materials provided with the distribution.
 24 *    * Neither the name of UNC Charlotte nor the names of its contributors
 25 *      may be used to endorse or promote products derived from this software
 26 *      without specific prior written permission.
 27 *      
 28 * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
 29 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 30 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 31 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
 32 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 33 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 37 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 38\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
 39
 40public class DataDimension {
 41
 42	// * The key (internal name) for this dimension
 43	private String dimensionKey;
 44
 45	private DataType type;
 46
 47	private String name;
 48
 49	private Map<String, String> categoryNames = new TreeMap<String, String>();
 50
 51	// * The map of category keys to integers, for faster lookup when parsing
 52	private Map<String, Integer> categoryMap = new TreeMap<String, Integer>();
 53
 54	private ArrayList<String> categoryKeys = new ArrayList<String>();
 55
 56	private Map<String, Integer> occurrenceCounts = new TreeMap<String, Integer>();
 57
 58	private List<String> values = new ArrayList<String>(100);
 59
 60//	private boolean hasMetaData = false;
 61
 62	private String dbHandle;
 63	
 64	public DataDimension(String key, DataType dataType) {
 65		dimensionKey = key;
 66		type = dataType;
 67	}
 68
 69	public void addCategory(String key, String name) {
 70		categoryNames.put(key, name);
 71		categoryKeys.add(key);
 72		categoryMap.put(key, categoryKeys.size());
 73	}
 74	
 75	public void addValue(String value) {
 76		if (values.size() < 100)
 77			values.add(value);
 78		Integer num = occurrenceCounts.get(value);
 79		if (num == null)
 80			num = Integer.valueOf(1);
 81		else
 82			num = Integer.valueOf(num+1);
 83		occurrenceCounts.put(value, num);
 84		switch(type) {
 85		case numerical:
 86			try {
 87				Float.parseFloat(value);
 88			} catch (Exception e) {
 89				type = DataType.categorical;
 90			}
 91			break;
 92		case categorical:
 93			if (occurrenceCounts.size() > 100)
 94				type = DataType.textual;
 95			break;
 96		}
 97		if (type != DataType.textual)
 98//			if (!hasMetaData)
 99				if (!categoryMap.containsKey(value))
100					addCategory(value, value);
101	}
102
103	public int getOccurrenceCount(String key) {
104		Integer count = occurrenceCounts.get(key);
105		if (count != null)
106			return count;
107		else {
108			PSLogging.logger.warn("No occurrance count found for '"+key+"'");
109			return 0;
110		}
111	}
112	
113	public List<String> getValues() {
114		return values;
115	}
116	
117	public String getName() {
118		if (name != null)
119			return name;
120		else
121			return dimensionKey;
122	}
123
124	public String getKey() {
125		return dimensionKey;
126	}
127
128	public String toString() {
129		return dimensionKey;
130	}
131
132	public String getCategoryName(int categoryValue) {
133		if (type != DataType.textual)
134			return categoryNames.get(categoryKeys.get(categoryValue));
135		else {
136			for (Map.Entry<String, Integer> e : categoryMap.entrySet()) {
137				if (e.getValue() == categoryValue)
138					return e.getKey();
139			}
140			return null;
141		}
142	}
143
144	public void setCategoryName(int categoryValue, String name) {
145		categoryNames.put(categoryKeys.get(categoryValue), name);
146	}
147
148	public String getCategoryKey(int categoryValue) {
149		return categoryKeys.get(categoryValue);
150	}
151
152	public int getNumCategories() {
153		return categoryKeys.size();
154	}
155
156	public DataType getDataType() {
157		return type;
158	}
159
160	public void setDataType(DataType type) {
161		this.type = type;
162	}
163	
164	public void setName(String newName) {
165		name = newName;
166//		hasMetaData = true;
167	}
168	
169	public int getNumForKey(String key) {
170		// TODO: Debug code that needs to go before release
171		if (categoryMap.containsKey(key))
172			return categoryMap.get(key);
173		else {
174			System.err.println("Can't find "+key+" in dimension "+getName());
175			return 0;
176		}
177	}
178
179	public void setHandle(String dimHandle) {
180		dbHandle = dimHandle;
181	}
182	
183	public String getHandle() {
184		return dbHandle;
185	}
186}