ArrayUtil.java :  » Database-Client » SQLMinus » util » Java Open Source

Java Open Source » Database Client » SQLMinus 
SQLMinus » util » ArrayUtil.java
package util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Enumeration;
import java.util.StringTokenizer;
/** contains static methods that operate on arrays or return arrays.
 * @author Rahul Kumar June 2001.
 * $Author: rahul_kumar $    $Id: ArrayUtil.java,v 1.1 2004/01/01 06:49:54 rahul_kumar Exp rahul $
 */
public class ArrayUtil {

    /** prepend a string to each element in an array
     */
    public static String[] prependString (String[] sarr, String text) {
        int len = sarr.length;
        for (int i=0; i< len; i++){
            sarr[i] = text + sarr[i];
        }
        return sarr;
    }
    public static List prependString (List sarr, String text) {
        int len = sarr.size();
        List v = new ArrayList(len);
        for (int i=0; i< len; i++){
            v.add(text + sarr.get(i).toString());
        }
        return v;
    }

    /** append a string to each element in an array
     */
    public static String[] appendString (String[] sarr, String text) {
        int len = sarr.length;
        for (int i=0; i< len; i++){
            sarr[i] += text;
        }
        return sarr;
    }
    /** enclose each element in an array with given strings.
     * Useful when surrounding a string with tags
     */
    public static String[] enclose (String[] sarr, String before, String after) {
        int len = sarr.length;
        for (int i=0; i< len; i++){
            sarr[i] = before + sarr[i] + after;
        }
        return sarr;
    }
    public static String enclose (String sarr, String before, String after) {
            return (before + sarr + after);
    }

    /** remove zero length entries from the given array - principally created for
     * string arrays coming from JSP's. For other rare cases, i am also trimming the string 
     * and then checking its length.
     */
    public static String[] trim (String[] sarr) {
        int len = sarr.length;
        ArrayList alist = new ArrayList();
        for (int i=0; i< len; i++){
            if (sarr[i].length() == 0 || sarr[i].trim().length() == 0)
                continue;
            alist.add(sarr[i]);
        }

        // Return list as an array of strings
        String[] ret = new String[alist.size()];
        alist.toArray(ret);
        return ret;
    }

    /** joins values of a string array with the given separator
     * into a string and returns the string.
     * Does not delimit the last value, obviously.
     */
    public static String join (String[] sarr, char csep) {
        int len = sarr.length;
        StringBuffer sbuf = new StringBuffer(len*10);
        for(int i=0; i< len; i++) { 
            if (i == len-1)
                sbuf.append(sarr[i]);
            else
                sbuf.append(sarr[i]).append(csep);
        }
        return sbuf.toString();
    }

    public static String join (Object[] sarr, char csep) {
        int len = sarr.length;
        StringBuffer sbuf = new StringBuffer(len*10);
        for (int i=0; i< len; i++) { 
            if (i == len-1)
                sbuf.append(sarr[i].toString());
            else
                sbuf.append(sarr[i].toString()).append(csep);
        }
        return sbuf.toString();
    }

    /** joins an arraylist. this should go into a new ListUtil
     */
    public static String join (List sarr, char csep) {
        String[] sa = toStringArray(sarr);
        return join(sa, csep);
    }


// i had earlier put this in BTSLUtil but then it vanished from there.
// it may be safer here, although it doesnt take an array
    /** splits a given string on given character separator returning
     * an array of strings.
     * THis would be more efficient than using string tokenizer which
     * takes a String not char as argument.
     * @author Rahul Kumar June 17, 2001
     * How to deal with consecutive delimiters - i am returning blank
     * strings.
     */
    public static String[] split (String st, char sep) {

        ArrayList alist = new ArrayList();

        int len = st.length();
        int pos = 0;
        int fin = 0;

        // while not end of string, and you can find a match
        while (pos < len && (fin = st.indexOf(sep, pos)) != -1){
            alist.add(st.substring(pos, fin ));
            pos = fin + 1;
        }
        // Push remainder if it's not empty
        String remainder = st.substring(pos);
        if (remainder.length() != 0)
        {
            alist.add(remainder);
        }

        // Return list as an array of strings
        String[] ret = new String[alist.size()];
        alist.toArray(ret);
        return ret;
    } // end of split

    /** THIS WONT WORK AS EXPECTED SINCE ssep actually means each
     * character in ssep is a delim
     */
    public static String[] split (String st, String ssep){
        StringTokenizer stok = new StringTokenizer(ssep);
        ArrayList v = new ArrayList(16);
        while (stok.hasMoreTokens()){
            v.add(stok.nextToken());
        }
        String[] sarr = new String[ v.size() ];
        v.toArray(sarr);
        return (String[])sarr;
    }
    
    /** returns the value at an index, a negative index denotes
     * indexes taken from end backwards (-1 means last but one),
     * An index higher than array size returns highest element,
     * A negative index larger than size will return element at 0.
     */
    public static Object get( Object a[], int index ) {
        if (index > a.length-1 )
            index = a.length -1;
        if (index < 0) {
            index = a.length + index;
            if (index < 0) index = 0;
        }
        return a[index];
    }
    /** identical to get (for those used to ArrayList class methods).
     */
    public static Object elementAt( Object a[], int index ) {
        return get(a, index);
    }

    /** sequential search of array returning index if found, else -1.
     * Return values start from 0. 
     * For repeated searches on a large array, please use Arrays.sort() once
     * and then use binarySearch. However, that would work only if index
     * does not matter.
     * */
    public static int indexOf (Object a[], Object key) {
        for (int i=0; i< a.length; i++) {
            if (a[i].equals(key))
                return i;
        }
        return -1;
    }
    /** searches an array of a String that starts with the given key 
     * returning its index.
     */
    public static int indexOfStartsWith (String a[], String key) {
        for (int i=0; i< a.length; i++) {
            if (a[i].startsWith(key))
                return i;
        }
        return -1;
    }
    /** searches an array of Strings that ends with the given key 
     * returning its index.
     * e.g. EMPID, ['MGREMPID','NAME']
     */
    public static int indexOfEndsWith (String a[], String key) {
        for (int i=0; i< a.length; i++) {
            if (a[i].endsWith(key))
                return i;
        }
        return -1;
    }
    /** checks whether the key, ends with a string in the array given.
     * This is a reverse of the above, returning its index.
     * e.g. MGREMPID, ['EMPID','NAME']
     * RK added on 20031226 20:54:41
     */
    public static int indexOfEndsWith (String key, String a[]) {
        for (int i=0; i< a.length; i++) {
            if (key.endsWith(a[i]))
                return i;
        }
        return -1;
    }
    /** searches an array of Strings that includes the given key 
     * returning its index.
     */
    public static int indexOfIndexOf (String a[], String key) {
        for (int i=0; i< a.length; i++) {
            if (a[i].indexOf(key) != -1)
                return i;
        }
        return -1;
    }
    public static int indexOf (int a[], int key) {
        for (int i=0; i< a.length; i++) {
            if (a[i]==key)
                return i;
        }
        return -1;
    }

    /** for those bad days when you have to deal with an enumeration and you
     * sorely need to treat is as an array or Collection.
     */
    public static Object[] toArray (Enumeration e) {
        List v = new ArrayList();
        while (e.hasMoreElements()) {
            v.add(e.nextElement());
        }
        return v.toArray();
    }
    /** If you want to put the value into an array of some particular
     * type, pass that as a parameter. On the lines of ArrayList.toArray(Object[])
     */
    public static Object[] toArray (Enumeration e, Object[] a) {
        List v = new ArrayList();
        while (e.hasMoreElements()) {
            v.add(e.nextElement());
        }
        return v.toArray(a);
    }
    public static Object[] toSortedArray (Enumeration e) {
        Object[] result = (Object[]) ArrayUtil.toArray(e);
        Arrays.sort( result );
        return result;
    }
    /** If you want to put the value into an array of some particular
     * type, pass that as a parameter. On the lines of ArrayList.toArray(Object[])
     */
    public static Object[] toSortedArray (Enumeration e, Object[] a) {
        Object[] result = (Object[]) ArrayUtil.toArray(e, a);
        Arrays.sort( result );
        return result;
    }
    public static String[] toStringArray( List l){
        String arr[] = new String[ l.size() ];
        return (String[]) l.toArray(arr);
    }
    /** find a regex in the array and return its index, else -1. 
     * pattern should be enclosed in // with possible modifiers at end
     * e.g. /pattern/ OR /pattern/i  .
     */
    public static int indexOfRegex(String s[], String pattern){
        for( int i = 0; i < s.length; i++){
            if (PerlWrapper.isMatchingRE(pattern, s[i]))
                return i;
        }
        return -1;
    }


} // end of class
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.