From 51e9da7c9b7cce946cd92476be3b6a09175d1f67 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Wed, 27 Oct 2010 13:49:46 +0000 Subject: [PATCH] --- .classpath | 2 +- src/zutil/StringUtil.java | 49 ++++++- src/zutil/db/bean/DBBean.java | 17 ++- src/zutil/db/bean/DBBeanSQLResultHandler.java | 10 +- src/zutil/log/LogUtil.java | 19 ++- src/zutil/test/QuickSortTest.java | 137 ------------------ src/zutil/test/SortTestSimple.java | 3 +- src/zutil/test/StringUtilTest.java | 37 +++++ 8 files changed, 118 insertions(+), 156 deletions(-) delete mode 100644 src/zutil/test/QuickSortTest.java create mode 100644 src/zutil/test/StringUtilTest.java diff --git a/.classpath b/.classpath index 8a93da0..318bf68 100644 --- a/.classpath +++ b/.classpath @@ -3,7 +3,6 @@ - @@ -11,5 +10,6 @@ + diff --git a/src/zutil/StringUtil.java b/src/zutil/StringUtil.java index dcea12a..c57e566 100644 --- a/src/zutil/StringUtil.java +++ b/src/zutil/StringUtil.java @@ -7,7 +7,7 @@ package zutil; */ public class StringUtil { public static final String[] sizes = new String[]{"YB", "ZB", "EB", "PB", "TB", "GB", "MB", "kB", "B"}; - + /** * Present a size (in bytes) as a human-readable value * @@ -26,5 +26,52 @@ public class StringUtil { return value+" "+sizes[total]; } + /** + * Trims the given char and whitespace at the beginning and the end + * + * @param str is the string to trim + * @param trim is the char to trim + * @return a trimmed String + */ + public static String trim(String str, char trim){ + if( str == null || str.isEmpty() ) + return str; + int start=0, stop=str.length(); + // The beginning + for(int i=0; istart ;i--){ + char c = str.charAt( i ); + if( c <= ' ' || c == trim ) + stop = i; + else + break; + } + if( start >= str.length() ) + return ""; + //System.out.println("str: \""+str+"\" start: "+start+" stop: "+stop); + return str.substring(start, stop); + } + /** + * Trims the whitespace and quotes if the string starts and ends with one + * + * @param str is the string to trim + * @return + */ + public static String trimQuotes(String str){ + if( str == null ) + return null; + str = str.trim(); + if( str.length() >= 2 && str.charAt(0)=='\"' && str.charAt(str.length()-1)=='\"'){ + str = str.substring(1, str.length()-1); + } + return str; + } } diff --git a/src/zutil/db/bean/DBBean.java b/src/zutil/db/bean/DBBean.java index bf54bde..4f754f0 100644 --- a/src/zutil/db/bean/DBBean.java +++ b/src/zutil/db/bean/DBBean.java @@ -14,9 +14,11 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.NoSuchElementException; +import java.util.logging.Level; import java.util.logging.Logger; import zutil.db.DBConnection; +import zutil.log.CompactLogFormatter; import zutil.log.LogUtil; /** @@ -195,19 +197,19 @@ public abstract class DBBean { query.append( " SET" ); query.append( params ); if( id != null ) - query.append( " id=?" ); + query.append( ", "+config.id_field.getName()+"=?" ); } logger.fine("Save query: "+query.toString()); PreparedStatement stmt = db.getPreparedStatement( query.toString() ); // Put in the variables in the SQL for(int i=0; i c = this.getClass(); DBBeanConfig config = beanConfigs.get(c); if( config.id_field == null ) - throw new NoSuchElementException("DBTableID annotation missing in bean!"); + throw new NoSuchElementException("ID field is null!"); try { String sql = "DELETE FROM "+config.tableName+" WHERE "+ config.id_field +"=?"; logger.fine("Load query: "+sql); diff --git a/src/zutil/db/bean/DBBeanSQLResultHandler.java b/src/zutil/db/bean/DBBeanSQLResultHandler.java index 9b7b17e..debec7f 100644 --- a/src/zutil/db/bean/DBBeanSQLResultHandler.java +++ b/src/zutil/db/bean/DBBeanSQLResultHandler.java @@ -131,8 +131,14 @@ public class DBBeanSQLResultHandler implements SQLResultHandler{ return obj; obj = bean_class.newInstance(); cacheDBBean(obj, id); - - for( Field field : bean_config.fields ){ + + // Get id field + obj.setFieldValue(bean_config.id_field, result.getLong(bean_config.id_field.getName())); + // Get the rest + for( Field field : bean_config.fields ){ + // Skip id field (its already loaded) + if( field.equals(bean_config.id_field) ) + continue; String name = field.getName(); // Another DBBean class diff --git a/src/zutil/log/LogUtil.java b/src/zutil/log/LogUtil.java index 97639e5..00a1382 100644 --- a/src/zutil/log/LogUtil.java +++ b/src/zutil/log/LogUtil.java @@ -49,18 +49,25 @@ public class LogUtil { /** * Sets the global log level */ - public static void setGlobalLogLevel(Level level){ + public static void setGlobalLevel(Level level){ setLevel("", level); } + /** + * Sets the log level for a specified class + */ + public static void setLevel(Class c, Level level){ + setLevel(c.getName(), level); + } + /** * Sets the log level for a specified logger */ public static void setLevel(String name, Level level){ - Logger root = Logger.getLogger(""); - root.setLevel(level); - for (Handler handler : root.getHandlers()) { - handler.setLevel(level); - } + Logger logger = Logger.getLogger(name); + logger.setLevel(level); + //for (Handler handler : logger.getHandlers()) { + // handler.setLevel(level); + //} } } diff --git a/src/zutil/test/QuickSortTest.java b/src/zutil/test/QuickSortTest.java deleted file mode 100644 index d73e4d2..0000000 --- a/src/zutil/test/QuickSortTest.java +++ /dev/null @@ -1,137 +0,0 @@ -package zutil.test; -import zutil.algo.sort.QuickSort; -import zutil.algo.sort.sortable.SortableIntArray; -import junit.framework.*; - -public class QuickSortTest extends TestCase { - public static boolean debug = false; - //the size of the arrays to be tested - private static final int[] cases = new int[]{10000,100000,1000000,10000000}; - //the type of array to be tested - // 0 = random - // 1 = mirror - // 2 = sorted - private static final int[] types = new int[]{0,1,2}; - //the strings for the diffrent arrays - private static final String[] typesS = new String[]{"Random array","Mirrored array","Sorted array"}; - //the pivots that will be tested - // 0 = random - // 1 = median - // 2 = middle - private static final int[] pivots = new int[]{0,1,2}; - //the strings for the pivots - private static final String[] pivotsS = new String[]{"Random pivot","Median pivot","Half pivot"}; - //the current array size index of cases - private static int currentCase = 0; - //the current type of arrays - private static int typeCase = 0; - //the current pivot to use - private static int pivotCase = 0; - //the current state of using insertionsort in quicksort - private static boolean insertSort; - //the temp array that will be sorted - private int[] array; - - - /** - *The main method to run the test. was going to use junit but did - *no find a way to loop the test like in this method. - */ - public static void main(String[] args){ - QuickSortTest test = new QuickSortTest("Test"); - insertSort = true; - //the insertion sort tests loop - for(int z=0; z<2 ; insertSort=false,z++){ - System.out.println("**************** Whit insertionSort: "+insertSort+" *****************"); - // the pivots tests loop - for(pivotCase=0; pivotCase array[i]){ - fail("Array not sorted!! ("+array[i-1]+" > "+array[i]+")"); - } - } - } -} \ No newline at end of file diff --git a/src/zutil/test/SortTestSimple.java b/src/zutil/test/SortTestSimple.java index 94e1848..4f95ada 100644 --- a/src/zutil/test/SortTestSimple.java +++ b/src/zutil/test/SortTestSimple.java @@ -3,10 +3,9 @@ import zutil.algo.sort.MergeSort; import zutil.algo.sort.QuickSort; import zutil.algo.sort.SimpleSort; import zutil.algo.sort.sortable.SortableIntArray; -import junit.framework.*; @SuppressWarnings("unused") -public class SortTestSimple extends TestCase { +public class SortTestSimple { public static final int SIZE = 10000; public static final int MAX_INT = 10000; diff --git a/src/zutil/test/StringUtilTest.java b/src/zutil/test/StringUtilTest.java new file mode 100644 index 0000000..ad86f72 --- /dev/null +++ b/src/zutil/test/StringUtilTest.java @@ -0,0 +1,37 @@ +package zutil.test; + +import org.junit.*; +import static org.junit.Assert.*; + +import zutil.StringUtil; + +public class StringUtilTest { + + @Test + public void formatBytesToStringTest() { + assertEquals( StringUtil.formatBytesToString( 100 ), "100.0 B" ); + assertEquals( StringUtil.formatBytesToString( 10000 ), "9.7 kB" ); + } + + @Test + public void trimTest() { + assertEquals( StringUtil.trim("", ' '), "" ); + assertEquals( StringUtil.trim(" aa ", ' '), "aa" ); + assertEquals( StringUtil.trim("aa ", ' '), "aa" ); + assertEquals( StringUtil.trim(" aa", ' '), "aa" ); + assertEquals( StringUtil.trim(" aa ", 'a'), "" ); + assertEquals( StringUtil.trim("\u0010 aa ", ' '), "aa" ); + assertEquals( StringUtil.trim("\n\naa\n\t", ' '), "aa" ); + assertEquals( StringUtil.trim("\"aa\"", '\"'), "aa" ); + } + + @Test + public void trimQuotesTest() { + assertEquals( StringUtil.trimQuotes(""), "" ); + assertEquals( StringUtil.trimQuotes("\""), "\"" ); + assertEquals( StringUtil.trimQuotes("\"\""), "" ); + assertEquals( StringUtil.trimQuotes("\"aa"), "\"aa" ); + assertEquals( StringUtil.trimQuotes("aa\""), "aa\"" ); + assertEquals( StringUtil.trimQuotes("\"aa\""), "aa" ); + } +}