Skip to content
Snippets Groups Projects
StringVector.java 6.14 KiB
Newer Older
tjc's avatar
tjc committed
/* StringVector.java
 *
 * created: Fri Jan  1 1999
 *
 * This file is part of Artemis
 *
 * Copyright (C) 1998,1999,2000  Genome Research Limited
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
tjc's avatar
tjc committed
 * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/util/StringVector.java,v 1.5 2005-10-13 12:06:12 tjc Exp $
tjc's avatar
tjc committed
 */

package uk.ac.sanger.artemis.util;

import java.util.Vector;
import java.util.Collections;
import java.util.Comparator;

/**
 *  This class implements a Vector of String objects.
 *
 *  @author Kim Rutherford
tjc's avatar
tjc committed
 *  @version $Id: StringVector.java,v 1.5 2005-10-13 12:06:12 tjc Exp $
tjc's avatar
tjc committed
 **/

tcarver's avatar
tcarver committed
public class StringVector extends Vector<String>
tjc's avatar
tjc committed
{
tcarver's avatar
tcarver committed
  private static final long serialVersionUID = 1L;

tjc's avatar
tjc committed
  /**
   *  Create a new vector of String objects.
   **/
tjc's avatar
tjc committed
  public StringVector()
  {
tjc's avatar
tjc committed
    super();
tjc's avatar
tjc committed
  }

  /**
   *  Create a new vector which contains the given Strings.
   **/
  public StringVector(final String[] new_strings)
tjc's avatar
tjc committed
  {
tjc's avatar
tjc committed
    super(new_strings.length);
tjc's avatar
tjc committed
    int len = new_strings.length;
    for(int i = 0; i < len; ++i)
tjc's avatar
tjc committed
      add(new_strings[i]);
tjc's avatar
tjc committed
  }

tjc's avatar
tjc committed
  public StringVector(final String new_string)
  {
    super();
    add(new_string);
  }

tjc's avatar
tjc committed
  /**
   *  Create a new vector which contains the given Strings.
   **/
  public StringVector(final StringVector new_strings)
tjc's avatar
tjc committed
  {
tjc's avatar
tjc committed
    super(new_strings);
tjc's avatar
tjc committed
  }

  /**
tjc's avatar
tjc committed
   *  Call add() on each of the String objects in the given StringVector.
tjc's avatar
tjc committed
   **/
  public void add(final StringVector new_strings)
tjc's avatar
tjc committed
  {
    for (int i = 0; i < new_strings.size(); ++i)
      add (new_strings.elementAt(i));
tjc's avatar
tjc committed
  }

  /**
   *  Sorts the elements of the vector using quicksort from the collections
   *  package.
   */
tjc's avatar
tjc committed
  public void sort()
tcarver's avatar
tcarver committed
    final Comparator<String> comparator = new Comparator<String>()
tjc's avatar
tjc committed
    {
      public int compare(String fst, String snd)
tjc's avatar
tjc committed
      {
tjc's avatar
tjc committed
        {
          if(snd == null)
            return 0;
          else
            return -1;
tjc's avatar
tjc committed
        {
          if(snd == null)
            return 1;
tjc's avatar
tjc committed
        }
tcarver's avatar
tcarver committed
        return fst.compareTo(snd);
tjc's avatar
tjc committed
      }
    };
tjc's avatar
tjc committed

tjc's avatar
tjc committed
    Collections.sort(this, comparator);
tjc's avatar
tjc committed
  }

  /**
   *  Return a new copy of this object.
   **/
  public StringVector copy()
tjc's avatar
tjc committed
  {
tjc's avatar
tjc committed
    return new StringVector(this);
tjc's avatar
tjc committed
  }

  /**
   *  Return a StringVector containing the values of the given String after
   *  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.
tcarver's avatar
tcarver committed
   *  @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
tjc's avatar
tjc committed
   *    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.
   **/
tjc's avatar
tjc committed
  public static StringVector getStrings(final String argument,
tcarver's avatar
tcarver committed
                                        final String delim,
                                        final boolean keep_zero_char_toks)
tjc's avatar
tjc committed
  {
tcarver's avatar
tcarver committed
    final StringVector strVector = new StringVector();
tjc's avatar
tjc committed

tcarver's avatar
tcarver committed
    String tok;
    String lastTok = null;
    int idx1 = 0;
    int idx2;
    final int argLen = argument.length();
tjc's avatar
tjc committed

tcarver's avatar
tcarver committed
    while(idx1 < argLen)
tjc's avatar
tjc committed
    {
tcarver's avatar
tcarver committed
      idx2 = argument.indexOf(delim,idx1);
      if(idx2 == idx1)
tjc's avatar
tjc committed
      {
tcarver's avatar
tcarver committed
        idx1++;
tjc's avatar
tjc committed
        continue;
      }

tcarver's avatar
tcarver committed
      if(idx2 < 0)
        idx2 = argLen;
tcarver's avatar
tcarver committed
      tok = argument.substring(idx1,idx2);
      idx1 = idx2+1;
tjc's avatar
tjc committed

tcarver's avatar
tcarver committed
      if(tok.length() == 1 &&
          delim.indexOf(tok.charAt(0)) != -1)
tjc's avatar
tjc committed
      {
tjc's avatar
tjc committed
        // ignore the split characters
tcarver's avatar
tcarver committed
        if(keep_zero_char_toks &&
           (lastTok == null ||
            lastTok != null && lastTok.length () == 1 &&
            delim.indexOf (lastTok) != -1))
tjc's avatar
tjc committed
        {
tcarver's avatar
tcarver committed
          // add a space because of two split_characters in a row
          strVector.add("");
tjc's avatar
tjc committed
        }
tjc's avatar
tjc committed
      else
tcarver's avatar
tcarver committed
        strVector.add(tok);
tjc's avatar
tjc committed

tcarver's avatar
tcarver committed
      lastTok = tok;
tjc's avatar
tjc committed
    }

tcarver's avatar
tcarver committed
    return strVector;
tjc's avatar
tjc committed
  }

  /**
   *  Return a StringVector containing the values of the given String after
   *  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.
   **/
tjc's avatar
tjc committed
  public static StringVector getStrings(final String argument,
                                        final String split_characters)
tjc's avatar
tjc committed
  {
    return getStrings(argument, split_characters, false);
tjc's avatar
tjc committed
  }

  /**
   *  Return a StringVector containing the values of the given String after
   *  splitting on whitespace.  The return object contains one String for each
   *  sequence of non-whitespace characters in the argument.  If the argument
   *  String is zero length or it consists only of whitespace, the return
   *  vector will be zero length.
   **/
  public static StringVector getStrings(final String argument)
tjc's avatar
tjc committed
  {
    return getStrings(argument, " ", false);
  }

  public static void main(String args[])
  {
tjc's avatar
tjc committed
    String argument = "a a g g g c a c g t c g c a t c g a c t c";
tjc's avatar
tjc committed
    long startTime = System.currentTimeMillis();
    for(int i=0; i<10000000; i++)
      getStrings(argument, " ", true);

tcarver's avatar
tcarver committed
    System.out.println("TIME TAKEN "+  Long.toString(System.currentTimeMillis()-startTime));
tjc's avatar
tjc committed

tcarver's avatar
tcarver committed
    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));
tjc's avatar
tjc committed
  }

}