package org.javarosa.core.util;

import java.util.Vector;

/**
 * @author ctsims
 */
public class DataUtil {
    static final int offset = 10;
    static final int low = -10;
    static final int high = 400;
    static Integer[] iarray;

    static UnionLambda unionLambda = new UnionLambda();
    static StringSplitter stringSplitter = new StringSplitter();

    /**
     * Get Integer object that corresponds to int argument from a
     * pre-computed table, or build a new instance.
     *
     * @return Cached or new Integer instance that correpsonds to ivalue argument
     */
    public static Integer integer(int ivalue) {
        // lazily populate Integer cache
        if (iarray == null) {
            iarray = new Integer[high - low];
            for (int i = 0; i < iarray.length; ++i) {
                iarray[i] = new Integer(i + low);
            }
        }

        if (ivalue < high && ivalue >= low) {
            return iarray[ivalue + offset];
        } else {
            return new Integer(ivalue);
        }
    }

    public static <T> Vector<T> union(Vector<T> a, Vector<T> b) {
        return unionLambda.union(a, b);
    }

    public static void setUnionLambda(UnionLambda newUnionLambda) {
        unionLambda = newUnionLambda;
    }

    public static class UnionLambda {
        public <T> Vector<T> union(Vector<T> a, Vector<T> b) {
            Vector<T> u = new Vector<>();
            //Efficiency?
            for (T i : a) {
                if (b.contains(i)) {
                    u.addElement(i);
                }
            }
            return u;
        }
    }

    public static String[] splitOnSpaces(String s) {
        return stringSplitter.splitOnSpaces(s);
    }

    public static String[] splitOnDash(String s) {
        return stringSplitter.splitOnDash(s);
    }

    public static String[] splitOnColon(String s) {
        return stringSplitter.splitOnColon(s);
    }

    public static String[] splitOnPlus(String s) {
        return stringSplitter.splitOnPlus(s);
    }

    public static void setStringSplitter(StringSplitter newStringSplitter) {
        stringSplitter = newStringSplitter;
    }

    public static class StringSplitter {

        public String[] splitOnSpaces(String s) {
            Vector<String> vectorSplit = split(s, " ", true);
            return stringVectorToArray(vectorSplit);
        }

        public String[] splitOnDash(String s) {
            Vector<String> vectorSplit = split(s, "-", false);
            return stringVectorToArray(vectorSplit);
        }

        public String[] splitOnColon(String s) {
            Vector<String> vectorSplit = split(s, ":", false);
            return stringVectorToArray(vectorSplit);
        }

        public String[] splitOnPlus(String s) {
            Vector<String> vectorSplit = split(s, "+", false);
            return stringVectorToArray(vectorSplit);
        }

        private static String[] stringVectorToArray(Vector<String> v) {
            String [] arr = new String[v.size()];
            for (int i = 0; i < v.size(); i++) {
                arr[i] = v.elementAt(i);
            }
            return arr;
        }
    }



    /**
     * Custom implementation of tokenizing a string based on a delimiter, for use in j2me
     * (Java's String.split() method was not available until Java 1.4)
     *
     * @param str                       The string to be split
     * @param delimiter                 The delimeter to be used
     * @param combineMultipleDelimiters If two delimiters occur in a row,
     *                                  remove the empty strings created by their split
     * @return A vector of strings contained in original which were separated by the delimiter
     */
    public static Vector<String> split(String str, String delimiter, boolean combineMultipleDelimiters) {
        Vector<String> pieces = new Vector<>();

        int index = str.indexOf(delimiter);
        // add all substrings, split by delimiter, to pieces.
        while (index >= 0) {
            pieces.addElement(str.substring(0, index));
            str = str.substring(index + delimiter.length());
            index = str.indexOf(delimiter);
        }
        pieces.addElement(str);

        // remove all pieces that are empty string
        if (combineMultipleDelimiters) {
            for (int i = 0; i < pieces.size(); i++) {
                if (pieces.elementAt(i).length() == 0) {
                    pieces.removeElementAt(i);
                    i--;
                }
            }
        }

        return pieces;
    }

}

@@ -290,73 +290,8 @@
 a();
-%0A    static StringSplitter stringSplitter = new StringSplitter();
 %0A%0A  
@@ -1602,45 +1602,101 @@
     
-return stringSplitter.splitOnSpaces(s
+if (%22%22.equals(s)) %7B%0A            return new String%5B0%5D;%0A        %7D%0A        return s.split(%22%5B %5D+%22
 );%0A 
@@ -1772,35 +1772,18 @@
 rn s
-tringSplitter.splitOnDash(s
+.split(%22-%22
 );%0A 
@@ -1860,36 +1860,18 @@
 rn s
-tringSplitter.splitOnColon(s
+.split(%22:%22
 );%0A 
@@ -1947,2595 +1947,27 @@
 rn s
-tringSplitter.splitOnPlus(s);%0A    %7D%0A%0A    public static void setStringSplitter(StringSplitter newStringSplitter) %7B%0A        stringSplitter = newStringSplitter;%0A    %7D%0A%0A    public static class StringSplitter %7B%0A%0A        public String%5B%5D splitOnSpaces(String s) %7B%0A            Vector%3CString%3E vectorSplit = split(s, %22 %22, true);%0A            return stringVectorToArray(vectorSplit);%0A        %7D%0A%0A        public String%5B%5D splitOnDash(String s) %7B%0A            Vector%3CString%3E vectorSplit = split(s, %22-%22, false);%0A            return stringVectorToArray(vectorSplit);%0A        %7D%0A%0A        public String%5B%5D splitOnColon(String s) %7B%0A            Vector%3CString%3E vectorSplit = split(s, %22:%22, false);%0A            return stringVectorToArray(vectorSplit);%0A        %7D%0A%0A        public String%5B%5D splitOnPlus(String s) %7B%0A            Vector%3CString%3E vectorSplit = split(s, %22+%22, false);%0A            return stringVectorToArray(vectorSplit);%0A        %7D%0A%0A        private static String%5B%5D stringVectorToArray(Vector%3CString%3E v) %7B%0A            String %5B%5D arr = new String%5Bv.size()%5D;%0A            for (int i = 0; i %3C v.size(); i++) %7B%0A                arr%5Bi%5D = v.elementAt(i);%0A            %7D%0A            return arr;%0A        %7D%0A    %7D%0A%0A%0A%0A    /**%0A     * Custom implementation of tokenizing a string based on a delimiter, for use in j2me%0A     * (Java's String.split() method was not available until Java 1.4)%0A     *%0A     * @param str                       The string to be split%0A     * @param delimiter                 The delimeter to be used%0A     * @param combineMultipleDelimiters If two delimiters occur in a row,%0A     *                                  remove the empty strings created by their split%0A     * @return A vector of strings contained in original which were separated by the delimiter%0A     */%0A    public static Vector%3CString%3E split(String str, String delimiter, boolean combineMultipleDelimiters) %7B%0A        Vector%3CString%3E pieces = new Vector%3C%3E();%0A%0A        int index = str.indexOf(delimiter);%0A        // add all substrings, split by delimiter, to pieces.%0A        while (index %3E= 0) %7B%0A            pieces.addElement(str.substring(0, index));%0A            str = str.substring(index + delimiter.length());%0A            index = str.indexOf(delimiter);%0A        %7D%0A        pieces.addElement(str);%0A%0A        // remove all pieces that are empty string%0A        if (combineMultipleDelimiters) %7B%0A            for (int i = 0; i %3C pieces.size(); i++) %7B%0A                if (pieces.elementAt(i).length() == 0) %7B%0A                    pieces.removeElementAt(i);%0A                    i--;%0A                %7D%0A            %7D%0A        %7D%0A%0A        return pieces;%0A    %7D%0A
+.split(%22%5B+%5D%22);%0A    %7D
 %0A%7D%0A

Remove J2ME-compat split