Java Collection Element Get getCollection(final String iText, final int iStartPosition, final Collection iCollection)

Here you can find the source of getCollection(final String iText, final int iStartPosition, final Collection iCollection)

Description

get Collection

License

Apache License

Declaration

public static int getCollection(final String iText,
            final int iStartPosition, final Collection<String> iCollection) 

Method Source Code

//package com.java2s;
/*//  ww w  .  jav a 2 s  .  c o  m
 * Copyright 1999-2010 Luca Garulli (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collection;

public class Main {
    public static final char COLLECTION_BEGIN = '[';
    public static final char COLLECTION_END = ']';
    public static final char COLLECTION_SEPARATOR = ',';

    public static int getCollection(final String iText,
            final int iStartPosition, final Collection<String> iCollection) {
        final StringBuilder buffer = new StringBuilder();

        int openPos = iText.indexOf(COLLECTION_BEGIN, iStartPosition);
        if (openPos == -1)
            return -1;

        int currentPos, deep;
        for (currentPos = openPos + 1, deep = 1; deep > 0; currentPos++) {
            if (currentPos >= iText.length()) {
                return -1;
            }

            char c = iText.charAt(currentPos);

            if (buffer.length() == 0 && c == ' ') {
                continue;
            }

            switch (c) {
            case COLLECTION_BEGIN:
                buffer.append(c);
                deep++;
                break;

            case COLLECTION_END:
                if (deep > 1)
                    buffer.append(c);
                deep--;
                break;

            case COLLECTION_SEPARATOR:
                if (deep > 1) {
                    buffer.append(c);
                } else {
                    iCollection.add(buffer.toString().trim());
                    buffer.setLength(0);
                }
                break;

            default:
                buffer.append(c);
            }
        }

        iCollection.add(buffer.toString().trim());

        return --currentPos;
    }

    public static int indexOf(final String iSource, final int iBegin,
            char... iChars) {
        if (iChars.length == 1)
            // ONE CHAR: USE JAVA INDEXOF
            return iSource.indexOf(iChars[0], iBegin);

        final int len = iSource.length();
        for (int i = iBegin; i < len; ++i) {
            for (int k = 0; k < iChars.length; ++k) {
                final char c = iSource.charAt(i);
                if (c == iChars[k])
                    return i;
            }
        }
        return -1;
    }
}

Related

  1. getCardinalityMap(final Collection coll)
  2. getCaseInsensitive(Collection data, String needle)
  3. getClassFromCollection(Collection collection)
  4. getClosest(final String pattern, Collection targets)
  5. getCollection(final Iterable iterable)
  6. getCollection(Object entity)
  7. getCollection(T... items)
  8. getCollectionClass(TT tt)
  9. getCollectionClosingSymbol(Collection col)