Skip to content
Snippets Groups Projects
Commit b8975e45 authored by tcarver's avatar tcarver
Browse files

use generics

parent 6332807b
No related branches found
No related tags found
No related merge requests found
...@@ -36,8 +36,10 @@ import java.util.Comparator; ...@@ -36,8 +36,10 @@ import java.util.Comparator;
* @version $Id: StringVector.java,v 1.5 2005-10-13 12:06:12 tjc Exp $ * @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. * Create a new vector of String objects.
**/ **/
...@@ -86,9 +88,9 @@ public class StringVector extends Vector ...@@ -86,9 +88,9 @@ public class StringVector extends Vector
*/ */
public void sort() 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) if(fst == null)
{ {
...@@ -102,7 +104,7 @@ public class StringVector extends Vector ...@@ -102,7 +104,7 @@ public class StringVector extends Vector
if(snd == null) if(snd == null)
return 1; return 1;
} }
return ((String)fst).compareTo((String) snd); return fst.compareTo(snd);
} }
}; };
...@@ -122,61 +124,59 @@ public class StringVector extends Vector ...@@ -122,61 +124,59 @@ public class StringVector extends Vector
* splitting using the given characters. If the argument String is zero * splitting using the given characters. If the argument String is zero
* length or it consists only of the characters used to split, the return * length or it consists only of the characters used to split, the return
* vector will be zero length. * vector will be zero length.
* @param keep_zero_char_tokens If true then zero width tokens will be * @param keep_zero_char_toks If true then zero width tokens will be
* returned. eg. when spliting on tabs if this parameter is true then * returned. eg. when splitting on tabs if this parameter is true then
* splitting this "\t\tfoo" will return "" and "foo". If this flag is * 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" * false then the split_characters will be treated as a block (and "foo"
* would be returned in the example. * would be returned in the example.
**/ **/
public static StringVector getStrings(final String argument, public static StringVector getStrings(final String argument,
String split_characters, final String delim,
final boolean keep_zero_char_tokens) final boolean keep_zero_char_toks)
{ {
final StringVector return_vector = new StringVector(); final StringVector strVector = new StringVector();
String value;
String last_value = null;
int ind1 = 0; String tok;
int ind2; String lastTok = null;
int argLen = argument.length(); int idx1 = 0;
int idx2;
final int argLen = argument.length();
while(ind1 < argLen) while(idx1 < argLen)
{ {
ind2 = argument.indexOf(split_characters,ind1); idx2 = argument.indexOf(delim,idx1);
if(ind2 == ind1) if(idx2 == idx1)
{ {
ind1++; idx1++;
continue; continue;
} }
if(ind2 < 0) if(idx2 < 0)
ind2 = argLen; idx2 = argLen;
value = argument.substring(ind1,ind2); tok = argument.substring(idx1,idx2);
ind1 = ind2+1; idx1 = idx2+1;
if(value.length() == 1 && if(tok.length() == 1 &&
split_characters.indexOf(value.charAt(0)) != -1) delim.indexOf(tok.charAt(0)) != -1)
{ {
// ignore the split characters // ignore the split characters
if(keep_zero_char_toks &&
if(keep_zero_char_tokens && (lastTok == null ||
(last_value == null || lastTok != null && lastTok.length () == 1 &&
last_value != null && last_value.length () == 1 && delim.indexOf (lastTok) != -1))
split_characters.indexOf (last_value) != -1))
{ {
// we need to add a space because of two split_characters in a row // add a space because of two split_characters in a row
return_vector.add(""); strVector.add("");
} }
} }
else 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 ...@@ -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"; 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(); long startTime = System.currentTimeMillis();
for(int i=0; i<10000000; i++) for(int i=0; i<10000000; i++)
getStrings(argument, " ", true); 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));
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment