Java Array Concatenate concat(T[] a, T[] b, T[] c, T[] d)

Here you can find the source of concat(T[] a, T[] b, T[] c, T[] d)

Description

concat

License

Creative Commons License

Declaration

public static <T> T[] concat(T[] a, T[] b, T[] c, T[] d) 

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

import java.util.ArrayList;
import java.util.Arrays;

import java.util.Iterator;
import java.util.List;

public class Main {
    public static <T> T[] concat(T[] a, T[] b, T[] c, T[] d) {
        // Someone tell us how to make this better.

        return list(chain(list(a), list(b), list(c), list(d))).toArray(a);
    }/* w w  w . j  ava 2  s  .c  o m*/

    public static <T> T[] concat(T[] a, T[] b, T[] c) {
        return list(chain(list(a), list(b), list(c))).toArray(a);
    }

    /**
     * Use chain instead for arguments of different classes.
     */
    public static <T> T[] concat(T[] a, T[] b) {
        return list(chain(list(a), list(b))).toArray(a);
    }

    public static <T> List<T> list(Iterable<T> input) {
        List<T> ret = new ArrayList<>();

        for (T item : input) {
            ret.add(item);
        }

        return ret;
    }

    public static <T> List<T> list(T[] input) {
        return Arrays.asList(input);
    }

    public static <T> List<T> list(Iterator<T> input) {
        List<T> ret = new ArrayList<>();

        while (input.hasNext()) {
            ret.add(input.next());
        }

        return ret;
    }

    /**
     * This can be used to chain objects of different types (unlike concat). The returned
     * Iterable will be of the first common superclass.
     */
    @SafeVarargs
    public static <T> Iterable<T> chain(final Iterable<? extends T>... itArray) {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                class MyIt implements Iterator<T> {
                    /** Iterator of Iterators */
                    Iterator<Iterator<? extends T>> iI;

                    /** The current sub-Iterator */
                    Iterator<? extends T> i;

                    public MyIt(Iterator<Iterator<? extends T>> iI) {
                        this.iI = iI;
                        i = iI.next();
                    }

                    @Override
                    public boolean hasNext() {
                        if (i.hasNext()) {
                            return true;
                        } else {
                            if (iI.hasNext()) {
                                i = iI.next();
                                return this.hasNext();
                            } else {
                                return false;
                            }
                        }
                    }

                    @Override
                    public T next() {
                        if (i.hasNext()) {
                            return i.next();
                        } else {
                            i = iI.next();
                            return this.next();
                        }
                    }

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

                }

                List<Iterator<? extends T>> newIL = new ArrayList<Iterator<? extends T>>();
                for (Iterable<? extends T> it : itArray) {
                    newIL.add(it.iterator());
                }

                return new MyIt(newIL.iterator());
            }
        };
    }
}

Related

  1. concat(String[] parts)
  2. concat(String[] strs)
  3. concat(String[]... arrays)
  4. concat(T val, T[] vals)
  5. concat(T[] a, T[] b)
  6. concat(T[] arr0, @SuppressWarnings("unchecked") T[]... more)
  7. concat(T[] array, Collection collection)
  8. concat(T[] array1, T[] array2)
  9. concat(T[] array1, T[] array2)