Java Iterator createPathIterator(String path)

Here you can find the source of createPathIterator(String path)

Description

Returns an iterator that iterates over the sub nodes of a path.

License

Open Source License

Declaration

static public Iterator createPathIterator(String path) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2003, 2006 IBM Corporation and others.
 * 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:/*from w  ww .java2s  .c  om*/
 * IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.util.Iterator;

public class Main {
    /**
     * Returns an iterator that iterates over the sub nodes of a path.
     */
    static public Iterator createPathIterator(String path) {
        String tPath = path.startsWith("/") ? path.substring(1) : path; //$NON-NLS-1$
        if (tPath.length() == 0)
            tPath = null;
        final String aPath = tPath;

        return new Iterator() {
            int prevIndex = 0;
            int curIndex = 0;
            String pathString = aPath;

            public boolean hasNext() {
                return pathString != null && prevIndex != -1;
            }

            public Object next() {
                curIndex = pathString.indexOf('/', prevIndex);
                String nodeString = null;
                if (curIndex != -1)
                    nodeString = pathString
                            .substring(prevIndex, curIndex++);
                else
                    nodeString = pathString.substring(prevIndex);
                prevIndex = curIndex;
                return nodeString;
            }

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

Related

  1. copyIterator(Iterator iter)
  2. copyOf(Iterator elements)
  3. count(Iterator thingsToCount)
  4. countIterator(Iterator it)
  5. createMap(Iterator iter)
  6. createSkipIterator(Iterable source, int count)
  7. dump(Iterator it)
  8. endOfData(Iterator inputIter, Iterator resultIter)
  9. equalsIterator(Iterator it, Iterator it2)