From: <pat...@us...> - 2010-01-25 16:58:14
|
Revision: 1021 http://cishell.svn.sourceforge.net/cishell/?rev=1021&view=rev Author: pataphil Date: 2010-01-25 16:57:58 +0000 (Mon, 25 Jan 2010) Log Message: ----------- * Added TableUtilities.allAreNull() and TableUtilities.allAreNotNull(). * Added the package org.cishell.utilities.dictionary. * Did some refactoring in StringUtilities. * Added various utilities in StringUtilities. * Reviewed by Joseph. Modified Paths: -------------- trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java trunk/core/org.cishell.utilities/src/org/cishell/utilities/IntegerParserWithDefault.java trunk/core/org.cishell.utilities/src/org/cishell/utilities/StringUtilities.java Added Paths: ----------- trunk/core/org.cishell.utilities/src/org/cishell/utilities/Pair.java trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/ trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryEntry.java trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryIterator.java trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryUtilities.java Modified: trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF =================================================================== --- trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF 2010-01-21 20:31:28 UTC (rev 1020) +++ trunk/core/org.cishell.utilities/META-INF/MANIFEST.MF 2010-01-25 16:57:58 UTC (rev 1021) @@ -19,8 +19,9 @@ prefuse.data.util, prefuse.util, prefuse.util.collections -Export-Package: org.cishell.utilities, - org.cishell.utilities.database, +Export-Package: org.cishell.utilities, + org.cishell.utilities.dictionary, + org.cishell.utilities.database, org.cishell.utilities.mutateParameter, org.cishell.utilities.mutateParameter.defaultvalue, org.cishell.utilities.mutateParameter.dropdown, Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java 2010-01-21 20:31:28 UTC (rev 1020) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/ArrayUtilities.java 2010-01-25 16:57:58 UTC (rev 1021) @@ -42,4 +42,26 @@ return clone; } + + // TODO: Find a better place to put this? + public static <T> boolean allAreNull(T... objects) { + for (T object : objects) { + if (object != null) { + return false; + } + } + + return true; + } + + // TODO: Find a better place to put this? + public static <T> boolean allAreNotNull(T... objects) { + for (T object : objects) { + if (object == null) { + return false; + } + } + + return true; + } } Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/IntegerParserWithDefault.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/IntegerParserWithDefault.java 2010-01-21 20:31:28 UTC (rev 1020) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/IntegerParserWithDefault.java 2010-01-25 16:57:58 UTC (rev 1021) @@ -1,9 +1,9 @@ package org.cishell.utilities; public class IntegerParserWithDefault { - public static final int DEFAULT = -1; + public static final Integer DEFAULT = null; - public static int parse(String target) { + public static Integer parse(String target) { try { return Integer.parseInt(target); } catch (Exception e) { Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/Pair.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/Pair.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/Pair.java 2010-01-25 16:57:58 UTC (rev 1021) @@ -0,0 +1,19 @@ +package org.cishell.utilities; + +public class Pair<S, T> { + S firstObject; + T secondObject; + + public Pair(S firstObject, T secondObject) { + this.firstObject = firstObject; + this.secondObject = secondObject; + } + + public S getFirstObject() { + return this.firstObject; + } + + public T getSecondObject() { + return this.secondObject; + } +} \ No newline at end of file Modified: trunk/core/org.cishell.utilities/src/org/cishell/utilities/StringUtilities.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/StringUtilities.java 2010-01-21 20:31:28 UTC (rev 1020) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/StringUtilities.java 2010-01-25 16:57:58 UTC (rev 1021) @@ -93,21 +93,59 @@ } // TODO Think about instead using a Pattern, "\s*". Don't have to though. - public static boolean isEmptyOrWhiteSpace(String test) { - String trimmed = test.trim(); + public static boolean isEmptyOrWhitespace(String string) { + String trimmed = string.trim(); return (trimmed.length() == 0); } - public static boolean allAreEmptyOrWhiteSpace(String... tests) { - for (String test : tests) { - if (!isEmptyOrWhiteSpace(test)) { + public static boolean allAreEmptyOrWhitespace(String... strings) { + for (String string : strings) { + if (!isEmptyOrWhitespace(string)) { return false; } } return true; } + + public static boolean allAreNeitherEmptyNorWhitespace(String... strings) { + for (String string : strings) { + if (isEmptyOrWhitespace(string)) { + return false; + } + } + + return true; + } + + public static boolean isNull_Empty_OrWhitespace(String string) { + if (string == null) { + return true; + } + + return isEmptyOrWhitespace(string); + } + + public static boolean allAreNull_Empty_OrWhitespace(String... strings) { + for (String string : strings) { + if (!isNull_Empty_OrWhitespace(string)) { + return false; + } + } + + return true; + } + + public static boolean allAreNeitherNullNorEmptyNorWhitespace(String... strings) { + for (String string : strings) { + if (isNull_Empty_OrWhitespace(string)) { + return false; + } + } + + return true; + } public static int countOccurrencesOfChar( CharSequence characters, char target) { @@ -167,6 +205,14 @@ return (String[])cleanedStrings.toArray(new String[0]); } + public static String trimIfNotNull(String string) { + if (string == null) { + return null; + } + + return string.trim(); + } + public static String toSentenceCase(String word) { String cleanedWord = simpleClean(word); @@ -193,18 +239,25 @@ return -1; } - public static boolean validAndEquivalent(String string1, String string2) { - return (!isEmptyOrWhiteSpace(string1) && (string1.compareTo(string2) == 0)); + /* TODO Perhaps make a "hasContent" method in here and apply that terminology throughout. */ + public static boolean areValidAndEqual(String string1, String string2) { + return ( + !isNull_Empty_OrWhitespace(string1) && + !isNull_Empty_OrWhitespace(string2) && + (string1.equals(string2))); } - public static boolean validAndEquivalentIgnoreCase(String string1, String string2) { - return (!isEmptyOrWhiteSpace(string1) && (string1.compareToIgnoreCase(string2) == 0)); + public static boolean areValidAndEqualIgnoreCase(String string1, String string2) { + return ( + !isNull_Empty_OrWhitespace(string1) && + !isNull_Empty_OrWhitespace(string2) && + string1.equalsIgnoreCase(string2)); } // TODO: New Name. public static String simpleMerge(String string1, String string2) { - if (!isEmptyOrWhiteSpace(string1)) { - if (!isEmptyOrWhiteSpace(string2)) { + if (!isNull_Empty_OrWhitespace(string1)) { + if (!isNull_Empty_OrWhitespace(string2)) { if (string1.length() >= string2.length()) { return string1; } else { @@ -214,7 +267,7 @@ return string1; } } - else if (!isEmptyOrWhiteSpace(string2)) { + else if (!isNull_Empty_OrWhitespace(string2)) { return string2; } Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryEntry.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryEntry.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryEntry.java 2010-01-25 16:57:58 UTC (rev 1021) @@ -0,0 +1,19 @@ +package org.cishell.utilities.dictionary; + +public class DictionaryEntry<K, V> { + private K key; + private V value; + + public DictionaryEntry(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return this.key; + } + + public V getValue() { + return this.value; + } +} \ No newline at end of file Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryIterator.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryIterator.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryIterator.java 2010-01-25 16:57:58 UTC (rev 1021) @@ -0,0 +1,37 @@ +package org.cishell.utilities.dictionary; + +import java.util.Dictionary; +import java.util.Enumeration; +import java.util.Iterator; + +public class DictionaryIterator<K, V> + implements Iterator<DictionaryEntry<K, V>>, Iterable<DictionaryEntry<K, V>> { + private Dictionary<K, V> dictionary; + Enumeration<K> keys; + + public DictionaryIterator(Dictionary<K, V> dictionary) { + this.dictionary = dictionary; + this.keys = dictionary.keys(); + } + + public boolean hasNext() { + return this.keys.hasMoreElements(); + } + + public DictionaryEntry<K, V> next() { + K nextKey = this.keys.nextElement(); + V nextValue = this.dictionary.get(nextKey); + + return new DictionaryEntry<K, V>(nextKey, nextValue); + } + + public void remove() { + String exceptionMessage = "remove() cannot be called on a DictionaryIterator."; + + throw new UnsupportedOperationException(exceptionMessage); + } + + public Iterator<DictionaryEntry<K, V>> iterator() { + return this; + } +} \ No newline at end of file Added: trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryUtilities.java =================================================================== --- trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryUtilities.java (rev 0) +++ trunk/core/org.cishell.utilities/src/org/cishell/utilities/dictionary/DictionaryUtilities.java 2010-01-25 16:57:58 UTC (rev 1021) @@ -0,0 +1,41 @@ +package org.cishell.utilities.dictionary; + +import java.util.Dictionary; +import java.util.Hashtable; + +public class DictionaryUtilities { + /// Side-effects the provided Dictionary. + public static <K, V> void addIfNotNull(Dictionary<K, V> dictionary, K key, V value) { + if ((key != null) && (value != null)) { + dictionary.put(key, value); + } + } + + /// Side-effects the provided Dictionary. + public static <K, V> void addIfNotNull( + Dictionary<K, V> dictionary, DictionaryEntry<K, V>... entries) { + for (DictionaryEntry<K, V> entry : entries) { + addIfNotNull(dictionary, entry.getKey(), entry.getValue()); + } + } + + public static <K, V> Dictionary<K, V> copyWithValuesThatAreNotNull( + Dictionary<K, V> originalDictionary, DictionaryEntry<K, V>... entries) { + Dictionary<K, V> newDictionary = copy(originalDictionary); + + addIfNotNull(newDictionary, entries); + + return newDictionary; + } + + public static <K, V> Dictionary<K, V> copy(Dictionary<K, V> originalDictionary) { + Dictionary<K, V> newDictionary = new Hashtable<K, V>(); + + for (DictionaryEntry<K, V> originalEntry : + new DictionaryIterator<K, V>(originalDictionary)) { + newDictionary.put(originalEntry.getKey(), originalEntry.getValue()); + } + + return newDictionary; + } +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |