Atom utility class


    May 12 2011 | 4:40 pm
    Hi all, here's a useful little class that combines Atom/Atom[] into new Atom[] arrays. Please feel free to use.
    Cycling folks: any chance of something like this finding its way into Atom's static methods?
    /*
    * Copyright Peter McCulloch, 2011
    */
    import com.cycling74.max.Atom;
    /**
    *
    * @author Peter McCulloch
    */
    public class AtomUtil {
    /**
    * Combines multiple arrays into a single array.
    * @param a
    * @return
    */public static Atom[] combine(Atom[]... a) {
    int length = 0;
    // Calculate the length of all values...
    for (Atom[] atoms : a) {
    length += atoms.length;
    }
    Atom[] out = new Atom[length];
    int k = 0;
    // Add elements
    for (Atom[] atoms : a) {
    for (Atom atom : atoms) {
    out[k] = atom;
    k++;
    }
    }
    return out;
    }
    /**
    *
    * @param a
    * @return
    */
    public static Atom[] combine(Atom... a) {
    return a; // Variable arguments are handy...
    }
    /**
    * Append one or more atoms to an array of atoms. (note that we can't call this
    * combine because that would cause a stack overflow)
    * @param in
    * @param a
    * @return
    */
    public static Atom[] append(Atom[] in, Atom... a) {
    return combine(in, a);
    }
    /**
    * Combine atoms into one array. Note that the variable arguments for a will all be
    * placed in front of in.
    * @param in
    * @param a
    * @return
    */
    public static Atom[] prepend(Atom[] in, Atom... a) {
    return combine(a, in);
    }
    public static void main(String args[]) {
    Atom[] a = Atom.newAtom(new int[]{1, 2, 3, 4, 5, 6, 7, 8});
    Atom[] b = Atom.newAtom(new int[]{10, 11, 12, 13, 14, 15, 16, 17, 18});
    Atom c = Atom.newAtom(20);
    Atom d = Atom.newAtom(30);
    Atom e = Atom.newAtom(40);
    Atom[] combineArrays = AtomUtil.combine(a, b);
    Atom[] combineAtoms = AtomUtil.combine(c, d, e);
    Atom[] combineArrayWithAtoms = AtomUtil.append(a, c, d, e);
    Atom[] prependArrayWithAtoms = AtomUtil.prepend(a, c, d, e);
    System.out.println("Combine Arrays: " + Atom.toOneString(combineArrays));
    System.out.println("Combine Atoms: " + Atom.toOneString(combineAtoms));
    System.out.println("Combine Array and Atoms: " + Atom.toOneString(combineArrayWithAtoms));
    System.out.println("Prepend Array with Atoms: " + Atom.toOneString(prependArrayWithAtoms));
    }
    }