diff --git a/uk/ac/sanger/artemis/util/StringVector.java b/uk/ac/sanger/artemis/util/StringVector.java index 6baecc33ec338f263ca6d01ee44ab6694088d61b..95f560d579a90ea74c2f1060d6a6e1bb08440016 100644 --- a/uk/ac/sanger/artemis/util/StringVector.java +++ b/uk/ac/sanger/artemis/util/StringVector.java @@ -36,8 +36,10 @@ import java.util.Comparator; * @version $Id: StringVector.java,v 1.5 2005-10-13 12:06:12 tjc Exp $ **/ -public class StringVector extends Vector +public class StringVector extends Vector<String> { + private static final long serialVersionUID = 1L; + /** * Create a new vector of String objects. **/ @@ -85,10 +87,10 @@ public class StringVector extends Vector * package. */ public void sort() - { - final Comparator comparator = new Comparator() + { + final Comparator<String> comparator = new Comparator<String>() { - public int compare(Object fst, Object snd) + public int compare(String fst, String snd) { if(fst == null) { @@ -102,7 +104,7 @@ public class StringVector extends Vector if(snd == null) return 1; } - return ((String)fst).compareTo((String) snd); + return fst.compareTo(snd); } }; @@ -122,61 +124,59 @@ public class StringVector extends Vector * splitting using the given characters. If the argument String is zero * length or it consists only of the characters used to split, the return * vector will be zero length. - * @param keep_zero_char_tokens If true then zero width tokens will be - * returned. eg. when spliting on tabs if this parameter is true then + * @param keep_zero_char_toks If true then zero width tokens will be + * returned. eg. when splitting on tabs if this parameter is true then * splitting this "\t\tfoo" will return "" and "foo". If this flag is * false then the split_characters will be treated as a block (and "foo" * would be returned in the example. **/ public static StringVector getStrings(final String argument, - String split_characters, - final boolean keep_zero_char_tokens) + final String delim, + final boolean keep_zero_char_toks) { - final StringVector return_vector = new StringVector(); + final StringVector strVector = new StringVector(); - String value; - String last_value = null; + String tok; + String lastTok = null; + int idx1 = 0; + int idx2; + final int argLen = argument.length(); - int ind1 = 0; - int ind2; - int argLen = argument.length(); - - while(ind1 < argLen) + while(idx1 < argLen) { - ind2 = argument.indexOf(split_characters,ind1); - if(ind2 == ind1) + idx2 = argument.indexOf(delim,idx1); + if(idx2 == idx1) { - ind1++; + idx1++; continue; } - if(ind2 < 0) - ind2 = argLen; + if(idx2 < 0) + idx2 = argLen; - value = argument.substring(ind1,ind2); - ind1 = ind2+1; + tok = argument.substring(idx1,idx2); + idx1 = idx2+1; - if(value.length() == 1 && - split_characters.indexOf(value.charAt(0)) != -1) + if(tok.length() == 1 && + delim.indexOf(tok.charAt(0)) != -1) { // ignore the split characters - - if(keep_zero_char_tokens && - (last_value == null || - last_value != null && last_value.length () == 1 && - split_characters.indexOf (last_value) != -1)) + if(keep_zero_char_toks && + (lastTok == null || + lastTok != null && lastTok.length () == 1 && + delim.indexOf (lastTok) != -1)) { - // we need to add a space because of two split_characters in a row - return_vector.add(""); + // add a space because of two split_characters in a row + strVector.add(""); } } else - return_vector.add(value); + strVector.add(tok); - last_value = value; + lastTok = tok; } - return return_vector; + return strVector; } /** @@ -207,13 +207,19 @@ public class StringVector extends Vector { String argument = "a a g g g c a c g t c g c a t c g a c t c"; long startTime = System.currentTimeMillis(); - for(int i=0; i<10000000; i++) getStrings(argument, " ", true); - long endTime = System.currentTimeMillis(); + System.out.println("TIME TAKEN "+ Long.toString(System.currentTimeMillis()-startTime)); - System.out.println("TIME TAKEN "+ Long.toString(endTime-startTime)); + startTime = System.currentTimeMillis(); + for(int i=0; i<10000000; i++) + { + java.util.StringTokenizer st = new java.util.StringTokenizer(argument, " ", true); + while(st.hasMoreTokens()) + st.nextToken(); + } + System.out.println("TIME TAKEN "+ Long.toString(System.currentTimeMillis()-startTime)); } }