|
From: <pat...@us...> - 2010-03-03 21:47:48
|
Revision: 1041
http://cishell.svn.sourceforge.net/cishell/?rev=1041&view=rev
Author: pataphil
Date: 2010-03-03 21:47:35 +0000 (Wed, 03 Mar 2010)
Log Message:
-----------
* Added NumberUtilities.roundToNDecimalPlaces().
* Cleaned up/refactored TableUtilities and added TableUtilities.extractDoubleFromCell().
* Finally had LogMessageHandler reviewed and refactored to be more straightforward.
* Reviewed by Joseph.
Modified Paths:
--------------
trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java
trunk/core/org.cishell.utilities/src/org/cishell/utilities/TableUtilities.java
trunk/core/org.cishell.utilities/src/org/cishell/utilities/osgi/logging/LogMessageHandler.java
Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java
===================================================================
--- trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java 2010-02-23 18:33:04 UTC (rev 1040)
+++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/NumberUtilities.java 2010-03-03 21:47:35 UTC (rev 1041)
@@ -133,6 +133,13 @@
}
}
+ public static double roundToNDecimalPlaces(double original, int decimalPlaceCount) {
+ String formatString = "#." + StringUtilities.multiply("#", decimalPlaceCount);
+ DecimalFormat format = new DecimalFormat(formatString);
+
+ return Double.valueOf(format.format(original));
+ }
+
public static boolean isEven(long number) {
return ((number % 2) == 0);
}
Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/TableUtilities.java
===================================================================
--- trunk/core/org.cishell.utilities/src/org/cishell/utilities/TableUtilities.java 2010-02-23 18:33:04 UTC (rev 1040)
+++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/TableUtilities.java 2010-03-03 21:47:35 UTC (rev 1041)
@@ -11,125 +11,145 @@
import prefuse.util.collections.IntIterator;
public class TableUtilities {
- public static String[] filterSchemaColumnNamesByClass
- (Schema schema, Class objectClass) throws ColumnNotFoundException
- {
- ArrayList workingColumnNames = new ArrayList();
+ public static Table copyTable(Table oldTable) {
+ Schema oldSchema = oldTable.getSchema();
+ Table newTable = oldSchema.instantiate();
+
+ for (Iterator rowIt = oldTable.tuples(); rowIt.hasNext();) {
+ Tuple row = (Tuple) rowIt.next();
+ newTable.addTuple(row);
+ }
+
+ return newTable;
+ }
- for (int ii = 0; ii < schema.getColumnCount(); ii++) {
- if (objectClass.isAssignableFrom(schema.getColumnType(ii)))
- workingColumnNames.add(schema.getColumnName(ii));
+ public static Table copyNRowsFromTableUsingIntIterator(
+ Table originalTable, IntIterator iterator, int topN, boolean isDescending)
+ {
+ // TODO: Add a couple comments in this method
+
+ Schema tableSchema = originalTable.getSchema();
+ final int numTableRows = originalTable.getRowCount();
+ Table newTable = createTableUsingSchema(tableSchema);
+ final int numRowsToCopy = Math.min(numTableRows, topN);
+ int[] originalTableRowsToCopy = new int [numTableRows];
+
+ newTable.addRows(numRowsToCopy);
+
+ for (int ii = 0; ii < numTableRows; ii++) {
+ originalTableRowsToCopy[ii] = iterator.nextInt();
}
+
+ // TODO: Comment the side-effects here
- if (workingColumnNames.size() == 0) {
- throw new ColumnNotFoundException
- ("No column of type " + objectClass.getName() + " was found.");
+ if (!isDescending) {
+ for (int ii = 0; ii < numRowsToCopy; ii++) {
+ copyTableRow(ii, originalTableRowsToCopy[ii], newTable, originalTable);
+ }
}
+ else {
+ for (int ii = 0; ii < numRowsToCopy; ii++) {
+ copyTableRow(
+ ii, originalTableRowsToCopy[numTableRows - ii - 1], newTable, originalTable);
+ }
+ }
- String[] finalColumnNames = new String [workingColumnNames.size()];
+ return newTable;
+ }
- return (String[])workingColumnNames.toArray(finalColumnNames);
+ public static void copyTableRow(
+ int newTableRow, int originalTableRow, Table newTable, Table originalTable)
+ {
+ final int numTableColumns = originalTable.getColumnCount();
+
+ for (int ii = 0; ii < numTableColumns; ii++)
+ newTable.set(newTableRow, ii, originalTable.get(originalTableRow, ii));
}
-
- public static List getAllColumnNames(Schema schema)
- throws ColumnNotFoundException {
- List workingColumnNames = new ArrayList();
-
+
+ public static List<String> getAllColumnNames(Schema schema) throws ColumnNotFoundException {
+ List<String> workingColumnNames = new ArrayList<String>();
+
for (int ii = 0; ii < schema.getColumnCount(); ii++) {
workingColumnNames.add(schema.getColumnName(ii));
}
if (workingColumnNames.size() == 0) {
- throw new ColumnNotFoundException
- ("No columns found in the schema.");
+ throw new ColumnNotFoundException("No columns found in the schema.");
}
return workingColumnNames;
}
-
- public static String formNonConflictingNewColumnName(
- Schema schema, String suggestedColumnName)
+
+ public static String[] getValidStringColumnNamesInTable(Table table)
throws ColumnNotFoundException {
- List workingColumnNames = getAllColumnNames(schema);
-
- if(!workingColumnNames.contains(suggestedColumnName)) {
- return suggestedColumnName;
- }
- else {
- int columnNameSuffix = 1;
- while(true) {
- String newColumnName =
- suggestedColumnName.concat("_" + columnNameSuffix);
- if(!workingColumnNames.contains(newColumnName)) {
- return newColumnName;
- }
- columnNameSuffix++;
- }
- }
+ return filterSchemaColumnNamesByClass(table.getSchema(), String.class);
+ }
+
+ public static String[] getValidNumberColumnNamesInTable(Table table)
+ throws ColumnNotFoundException {
+ Class<?>[] possibleNumberClasses = {
+ byte.class,
+ byte[].class,
+ Byte.class,
+ Byte[].class,
+ short.class,
+ short[].class,
+ Short.class,
+ Short[].class,
+ int.class,
+ int[].class,
+ Integer.class,
+ Integer[].class,
+ long.class,
+ long[].class,
+ Long.class,
+ Long[].class,
+ float.class,
+ float[].class,
+ Float.class,
+ Float[].class,
+ double.class,
+ double[].class,
+ Double.class,
+ Double[].class
+ };
+
+ return filterSchemaColumnNamesByClasses(table.getSchema(), possibleNumberClasses);
}
-
- public static String formNonConflictingNewColumnName(
- Schema schema, String[] suggestedColumnNames)
+
+ public static String[] getValidIntegerColumnNamesInTable(Table table)
throws ColumnNotFoundException {
- List workingColumnNames = getAllColumnNames(schema);
+ Class<?>[] possibleIntegerClasses = {
+ int.class,
+ Integer.class,
+ int[].class,
+ Integer[].class
+ };
+
+ return filterSchemaColumnNamesByClasses(table.getSchema(), possibleIntegerClasses);
+ }
- boolean suggestedNameFound = false;
- for(int suggestedNameIndex = 0; suggestedNameIndex < suggestedColumnNames.length; suggestedNameIndex++) {
- for(int i = 0; i < workingColumnNames.size(); i++) {
- if(workingColumnNames.get(i).toString().equalsIgnoreCase(suggestedColumnNames[suggestedNameIndex])) {
- suggestedNameFound = true;
- break;
- }
- }
- /*
- * To ensure that whenever a suggested name is found in the original column schema, create a name.
- * */
- if(suggestedNameFound) {
- break;
- }
- }
+ public static String[] getValidDateColumnNamesInTable(Table table)
+ throws ColumnNotFoundException {
+ Class<?>[] possibleDateClasses = {
+ Date.class,
+ int.class,
+ Integer.class,
+ String.class,
+ int[].class,
+ Integer[].class,
+ String[].class,
+ };
- /*
- * If none of the suggested names are conflicting then return the first suggested name.
- * */
- if(!suggestedNameFound) {
- return suggestedColumnNames[0];
- }
+ return filterSchemaColumnNamesByClasses(table.getSchema(), possibleDateClasses);
+ }
- /*
- * This part of code will be executed only if the suggested names are already present in the
- * column schema.
- * */
- boolean newColumnNameFound = false;
- int columnNameSuffix = 2;
- while(true) {
- /*
- * The pattern for new names will be taken from the first suggested column name.
- * */
- String newColumnName =
- suggestedColumnNames[0].concat("_" + columnNameSuffix);
- for(int i = 0; i < workingColumnNames.size(); i++) {
- if(workingColumnNames.get(i).toString().equalsIgnoreCase(newColumnName)) {
- newColumnNameFound = true;
- break;
- }
- }
- if(!newColumnNameFound) {
- return newColumnName;
- }
- columnNameSuffix++;
- }
- }
-
- public static String[] filterSchemaColumnNamesByClasses
- (Schema schema, Class[] objectClasses) throws ColumnNotFoundException
- {
- ArrayList workingColumnNames = new ArrayList();
+ public static String[] filterSchemaColumnNamesByClasses(
+ Schema schema, Class<?>[] objectClasses) throws ColumnNotFoundException {
+ ArrayList<String> workingColumnNames = new ArrayList<String>();
for (int ii = 0; ii < schema.getColumnCount(); ii++) {
- for (int jj = 0; jj < objectClasses.length; jj++) {
- Class objectClass = objectClasses[jj];
-
+ for (Class<?> objectClass : objectClasses) {
if (objectClass.isAssignableFrom(schema.getColumnType(ii))) {
workingColumnNames.add(schema.getColumnName(ii));
@@ -142,9 +162,8 @@
String[] finalColumnNames = new String [workingColumnNames.size()];
return (String[])workingColumnNames.toArray(finalColumnNames);
- }
- // An exception is thrown if there is not at least 1 column name.
- else {
+ } else {
+ // An exception is thrown if there is not at least 1 column name.
StringBuffer objectClassesString = new StringBuffer();
objectClassesString.append("[");
@@ -158,79 +177,108 @@
objectClassesString.append("]");
- throw new ColumnNotFoundException
- ("No column of types " + objectClassesString + " was found.");
+ throw new ColumnNotFoundException(
+ "No column of types " + objectClassesString + " was found.");
}
}
-
- public static String[] getValidStringColumnNamesInTable(Table table)
- throws ColumnNotFoundException
- {
- return filterSchemaColumnNamesByClass(table.getSchema(), String.class);
- }
-
- public static String[] getValidDateColumnNamesInTable(Table table)
- throws ColumnNotFoundException
- {
- Class[] possibleDateClasses = {
- Date.class,
- int.class,
- Integer.class,
- String.class,
- int[].class,
- Integer[].class,
- String[].class,
- };
+
+ public static String[] filterSchemaColumnNamesByClass(Schema schema, Class<?> objectClass)
+ throws ColumnNotFoundException {
+ ArrayList<String> workingColumnNames = new ArrayList<String>();
+
+ for (int ii = 0; ii < schema.getColumnCount(); ii++) {
+ if (objectClass.isAssignableFrom(schema.getColumnType(ii)))
+ workingColumnNames.add(schema.getColumnName(ii));
+ }
- return filterSchemaColumnNamesByClasses(table.getSchema(),
- possibleDateClasses);
- }
-
- public static String[] getValidIntegerColumnNamesInTable(Table table)
- throws ColumnNotFoundException
- {
- Class[] possibleIntegerClasses = {
- int.class,
- Integer.class,
- int[].class,
- Integer[].class
- };
-
- return filterSchemaColumnNamesByClasses(table.getSchema(),
- possibleIntegerClasses);
- }
+ if (workingColumnNames.size() == 0) {
+ throw new ColumnNotFoundException(
+ "No column of type " + objectClass.getName() + " was found.");
+ }
+
+ String[] finalColumnNames = new String [workingColumnNames.size()];
+
+ return (String[])workingColumnNames.toArray(finalColumnNames);
+ }
- public static String[] getValidNumberColumnNamesInTable(Table table)
+ public static String formNonConflictingNewColumnName(Schema schema, String suggestedColumnName)
throws ColumnNotFoundException {
- Class[] possibleNumberClasses = {
- byte.class,
- byte[].class,
- Byte.class,
- Byte[].class,
- short.class,
- short[].class,
- Short.class,
- Short[].class,
- int.class,
- int[].class,
- Integer.class,
- Integer[].class,
- long.class,
- long[].class,
- Long.class,
- Long[].class,
- float.class,
- float[].class,
- Float.class,
- Float[].class,
- double.class,
- double[].class,
- Double.class,
- Double[].class
- };
+ List<String> workingColumnNames = getAllColumnNames(schema);
+
+ if (!workingColumnNames.contains(suggestedColumnName)) {
+ return suggestedColumnName;
+ } else {
+ int columnNameSuffix = 1;
+
+ while(true) {
+ String newColumnName = suggestedColumnName.concat("_" + columnNameSuffix);
+
+ if (!workingColumnNames.contains(newColumnName)) {
+ return newColumnName;
+ }
+
+ columnNameSuffix++;
+ }
+ }
+ }
+
+ public static String formNonConflictingNewColumnName(
+ Schema schema, String[] suggestedColumnNames) throws ColumnNotFoundException {
+ List<String> workingColumnNames = getAllColumnNames(schema);
+ boolean suggestedNameFound = false;
+
+ for (String suggestedName : suggestedColumnNames) {
+ for (String workingColumnName : workingColumnNames) {
+ if (workingColumnName.equalsIgnoreCase(suggestedName)) {
+ suggestedNameFound = true;
+
+ break;
+ }
+ }
+
+ /*
+ * To ensure that whenever a suggested name is found in the original column schema,
+ * create a name.
+ */
+ if (suggestedNameFound) {
+ break;
+ }
+ }
- return filterSchemaColumnNamesByClasses(table.getSchema(),
- possibleNumberClasses);
+ /*
+ * If none of the suggested names are conflicting then return the first suggested name.
+ */
+ if(!suggestedNameFound) {
+ return suggestedColumnNames[0];
+ }
+
+ /*
+ * This part of code will be executed only if the suggested names are already present in
+ * the column schema.
+ */
+ boolean newColumnNameFound = false;
+ int columnNameSuffix = 2;
+
+ while(true) {
+ /*
+ * The pattern for new names will be taken from the first suggested column name.
+ */
+ String newColumnName = suggestedColumnNames[0].concat("_" + columnNameSuffix);
+
+ for (String workingColumnName : workingColumnNames) {
+ if (workingColumnName.equalsIgnoreCase(newColumnName)) {
+ newColumnNameFound = true;
+
+ break;
+ }
+ }
+
+ if (!newColumnNameFound) {
+ return newColumnName;
+ }
+
+ columnNameSuffix++;
+ }
}
/**
@@ -241,73 +289,16 @@
Table table = new Table();
for (int ii = 0; ii < numTableColumns; ii++) {
- table.addColumn(tableSchema.getColumnName(ii),
- tableSchema.getColumnType(ii));
+ table.addColumn(tableSchema.getColumnName(ii), tableSchema.getColumnType(ii));
}
return table;
}
-
- public static void copyTableRow(int newTableRow,
- int originalTableRow,
- Table newTable,
- Table originalTable)
- {
- final int numTableColumns = originalTable.getColumnCount();
-
- for (int ii = 0; ii < numTableColumns; ii++)
- newTable.set(newTableRow, ii, originalTable.get(originalTableRow, ii));
- }
-
- public static Table
- copyNRowsFromTableUsingIntIterator(Table originalTable,
- IntIterator iterator,
- int topN,
- boolean isDescending)
- {
- // TODO: Add a couple comments in this method
-
- Schema tableSchema = originalTable.getSchema();
- final int numTableRows = originalTable.getRowCount();
- Table newTable = createTableUsingSchema(tableSchema);
- final int numRowsToCopy = Math.min(numTableRows, topN);
- int[] originalTableRowsToCopy = new int [numTableRows];
-
- newTable.addRows(numRowsToCopy);
-
- for (int ii = 0; ii < numTableRows; ii++)
- originalTableRowsToCopy[ii] = iterator.nextInt();
- // TODO: Comment the side-effects here
-
- if (!isDescending) {
- for (int ii = 0; ii < numRowsToCopy; ii++) {
- copyTableRow
- (ii, originalTableRowsToCopy[ii], newTable, originalTable);
- }
- }
- else {
- for (int ii = 0; ii < numRowsToCopy; ii++)
- {
- copyTableRow(ii,
- originalTableRowsToCopy[numTableRows - ii - 1],
- newTable,
- originalTable);
- }
- }
+ public static double extractDoubleFromCell(Tuple row, String columnName)
+ throws NumberFormatException {
+ double value = NumberUtilities.interpretObjectAsDouble(row.get(columnName)).doubleValue();
- return newTable;
+ return value;
}
-
- public static Table copyTable(Table oldTable) {
- Schema oldSchema = oldTable.getSchema();
- Table newTable = oldSchema.instantiate();
-
- for (Iterator rowIt = oldTable.tuples(); rowIt.hasNext();) {
- Tuple row = (Tuple) rowIt.next();
- newTable.addTuple(row);
- }
-
- return newTable;
- }
}
\ No newline at end of file
Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/osgi/logging/LogMessageHandler.java
===================================================================
--- trunk/core/org.cishell.utilities/src/org/cishell/utilities/osgi/logging/LogMessageHandler.java 2010-02-23 18:33:04 UTC (rev 1040)
+++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/osgi/logging/LogMessageHandler.java 2010-03-03 21:47:35 UTC (rev 1041)
@@ -5,12 +5,10 @@
import org.osgi.service.log.LogService;
-// TODO: Get reviewed.
-// TODO: Make 1.4 compatible and move to cishell utilities?
public class LogMessageHandler {
private LogService logger;
- private Map<MessageTypeIndicator, MessageType> messageTypes =
- new HashMap<MessageTypeIndicator, MessageType>();
+ private Map<MessageTypeDescriptor, MessageType> messageTypes =
+ new HashMap<MessageTypeDescriptor, MessageType>();
public LogMessageHandler(LogService logger) {
this.logger = logger;
@@ -20,82 +18,65 @@
* If typeIndicator is already an added message type, its count will be
* reset and maximum count overridden.
*/
- public MessageTypeIndicator addMessageType(
- String description, int maximumCount) {
- MessageTypeIndicator typeIndicator = new MessageTypeIndicator();
- this.messageTypes.put(
- typeIndicator, new MessageType(description, maximumCount));
+ public MessageTypeDescriptor addMessageType(String description, int maximumCount) {
+ MessageTypeDescriptor typeIndicator = new MessageTypeDescriptor();
+ this.messageTypes.put(typeIndicator, new MessageType(description, maximumCount));
return typeIndicator;
}
- /**
- * logMessage will always be logged if typeIndicator has not been added
- * prior to calling this.
- */
- public void logMessage(
- MessageTypeIndicator typeIndicator,
- int logLevel,
- String logMessage) {
+ /// message will always be logged if typeIndicator has not been added prior to calling this.
+ public void handleMessage(MessageTypeDescriptor typeIndicator, int logLevel, String message) {
MessageType messageType = this.messageTypes.get(typeIndicator);
if (messageType != null) {
- if (messageType.messageLogged()) {
- this.logger.log(logLevel, logMessage);
- }
+ messageType.logMessage(logLevel, message, this.logger);
} else {
- this.logger.log(logLevel, logMessage);
+ this.logger.log(logLevel, message);
}
}
public void printOverloadedMessageTypes(int logLevel) {
for (MessageType messageType : this.messageTypes.values()) {
- if (messageType.wasOverloaded()) {
- this.logger.log(logLevel, messageType.toString());
+ if (messageType.isOverloaded()) {
+ this.logger.log(logLevel, messageType.reportOverloads());
}
}
}
- public class MessageTypeIndicator {}
+ // TODO: Javadoc what this is all about. (I will later.)
+ public class MessageTypeDescriptor {}
- private class MessageType {
+ private static class MessageType {
private String description;
private int maximumCount;
private int foundCount = 0;
- private int overLoadedCount = 0;
+ private int overloadedCount = 0;
public MessageType(String description, int maximumCount) {
this.description = description;
this.maximumCount = maximumCount;
}
- public boolean hasAnyLeft() {
- return this.foundCount != this.maximumCount;
+ public void logMessage(int logLevel, String message, LogService logger) {
+ if (shouldStillLog()) {
+ logger.log(logLevel, message);
+ this.foundCount++;
+ } else {
+ this.overloadedCount++;
+ }
}
- public boolean wasOverloaded() {
- return this.overLoadedCount > 0;
+ public String reportOverloads() {
+ return "Found " + this.overloadedCount + " more " + this.description + ".";
}
- public boolean messageLogged() {
- if (hasAnyLeft()) {
- this.foundCount++;
-
- return true;
- } else {
- this.overLoadedCount++;
-
- return false;
- }
+ private boolean shouldStillLog() {
+ return this.foundCount < this.maximumCount;
}
- public String toString() {
- return
- "Found " +
- this.overLoadedCount +
- " more " +
- this.description +
- ".";
+ private boolean isOverloaded() {
+ return this.overloadedCount > 0;
}
}
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|