All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.eclipse.mat.util.SimpleStringTokenizer Maven / Gradle / Ivy

There is a newer version: 2.1
Show newest version
/**
 * ****************************************************************************
 * Copyright (c) 2008 SAP AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * SAP AG - initial API and implementation
 * *****************************************************************************
 */
package org.eclipse.mat.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

public final class SimpleStringTokenizer implements Iterable {
  private String subject;
  private char delim;

  public SimpleStringTokenizer(String subject, char delim) {
    this.subject = subject;
    this.delim = delim;
  }

  public Iterator iterator() {
    return new Iterator() {
      int position = 0;
      int maxPosition = subject.length();

      public boolean hasNext() {
        return position < maxPosition;
      }

      public String next() {
        if (position >= maxPosition) throw new NoSuchElementException();

        String answer;

        int p = subject.indexOf(delim, position);

        if (p < 0) {
          answer = subject.substring(position);
          position = maxPosition;
          return answer;
        } else {
          answer = subject.substring(position, p);
          position = p + 1;
        }

        return answer;
      }

      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
  }

  public static String[] split(String subject, char delim) {
    List answer = new ArrayList();
    for (String s : new SimpleStringTokenizer(subject, delim))
      answer.add(s.trim());
    return answer.toArray(new String[0]);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy