Example usage for java.util List toArray

List of usage examples for java.util List toArray

Introduction

In this page you can find the example usage for java.util List toArray.

Prototype

<T> T[] toArray(T[] a);

Source Link

Document

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

Usage

From source file:Main.java

public static Method[] toMethodArray(List<Method> list) {
    if (isEmpty(list)) {
        return new Method[0];
    }/*from w  w w.j  av  a  2  s.  c o m*/
    Method[] array = new Method[list.size()];
    return list.toArray(array);
}

From source file:org.apache.deltaspike.test.servlet.impl.util.ArchiveUtils.java

public static JavaArchive[] getDeltaSpikeCoreAndServletModuleArchive() {
    JavaArchive extensionsJar = ShrinkWrap.create(JavaArchive.class, "dsCoreTest.jar")
            .addClass(ArchiveUtils.class).addPackages(true, "org.apache.http")
            .addPackages(true, "org.jboss.shrinkwrap.api") //TODO needed by the setup for tomee -> re-visit it
            .addPackage(WebProfileCategory.class.getPackage());

    JavaArchive[] coreArchives = ShrinkWrapArchiveUtil.getArchives(
            null, "META-INF/beans.xml", new String[] { "org.apache.deltaspike.core",
                    "org.apache.deltaspike.test.category", "org.apache.deltaspike.servlet" },
            null, "ds-core_and_servlet");

    List<JavaArchive> archives = new ArrayList<JavaArchive>(Arrays.asList(coreArchives));
    archives.add(extensionsJar);/*  w  w w  .  ja v  a  2  s  . c o m*/
    return archives.toArray(new JavaArchive[archives.size()]);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T[] listToArray(List<T> list) {
    if (list == null) {
        return null;
    }/*  w  w w  . j  a  v a 2 s  .c o m*/

    T[] tArr = (T[]) Array.newInstance(list.get(0).getClass(), list.size());
    return list.toArray(tArr);
}

From source file:Main.java

public static Class<?>[] toClassArray(List<Class<?>> list) {
    if (isEmpty(list)) {
        return new Class<?>[0];
    }//from   w  w  w .  java  2 s. c o m
    Class<?>[] array = new Class<?>[list.size()];
    return list.toArray(array);
}

From source file:Main.java

public static String[] getStackTrace(long threadID) {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadID, Integer.MAX_VALUE);

    List<String> buf = new ArrayList<String>();

    if (threadInfo == null) {
        return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
    }/*  w w  w.  ja va  2  s .c  om*/

    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement ste = stackTrace[i];
        buf.add("\t" + ste.toString());

        if (i == 0 && threadInfo.getLockInfo() != null) {
            Thread.State ts = threadInfo.getThreadState();
            switch (ts) {
            case BLOCKED:
                buf.add("\t-  blocked on " + threadInfo.getLockInfo());
                break;
            case WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            case TIMED_WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            default:
            }
        }

        for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                buf.add("\t-  locked " + mi);
            }
        }
    }

    LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0) {
        buf.add("\n\tNumber of locked synchronizers = " + locks.length);
        for (LockInfo li : locks) {
            buf.add("\t- " + li);
        }
    }

    return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static String[] joinStringArray(String[]... arrays) {
    int size = 0;
    for (String[] array : arrays) {
        size += array.length;/*from w  w  w .j  a v a2  s  . com*/
    }
    java.util.List list = new java.util.ArrayList(size);
    for (String[] array : arrays) {
        list.addAll(java.util.Arrays.asList(array));
    }
    return (String[]) list.toArray(new String[size]);
}

From source file:com.github.stagirs.lingvo.build.MorphStateMachineBuilder.java

private static Collection<RuleMapping> suf2mapping() throws IOException {
    Set<String> suffixes = getSuffixes();
    Map<String, Struct> sufs = new HashMap<String, Struct>();
    for (Map.Entry<String, List<WordForm[]>> raw2WordForms : getRaw2WordForms().entrySet()) {
        String raw = raw2WordForms.getKey();
        int[] index = getCommon(suffixes, raw, raw2WordForms.getValue());
        String common = index[0] < index[1] ? raw.substring(index[0], index[1]) : "";
        String rawPref = raw.substring(0, Math.min(index[0], index[1]));
        String rawSuf = raw.substring(index[1]);
        Map<String, List<RuleItem>> map = new HashMap();
        for (WordForm[] wordForm : raw2WordForms.getValue()) {
            String normSuf = wordForm[1].getWord().substring(common.length());
            if (!map.containsKey(normSuf)) {
                map.put(normSuf, new ArrayList<RuleItem>());
            }/*from  w  ww . j a  v a  2 s .  c o  m*/
            map.get(normSuf).add(new RuleItem(normSuf, wordForm[1].getForm(), wordForm[0].getForm()));
        }
        RuleItem[][] items = new RuleItem[map.size()][];
        int i = 0;
        for (List<RuleItem> item : map.values()) {
            items[i++] = item.toArray(new RuleItem[item.size()]);
        }
        Rule rule = new Rule(rawPref, rawSuf, items);
        String ruleId = Rule.serialize(rule);
        if (!sufs.containsKey(rawSuf)) {
            sufs.put(rawSuf, new Struct());
        }
        Struct struct = sufs.get(rawSuf);
        if (!struct.rule2id.containsKey(ruleId)) {
            struct.rule2id.put(ruleId, struct.rule2id.size());
            struct.rules.add(rule);
        }
        struct.prefcommons.put(rawPref + common, struct.rule2id.get(ruleId));
    }
    List<RuleMapping> list = new ArrayList<RuleMapping>();
    for (Map.Entry<String, Struct> entrySet : sufs.entrySet()) {
        list.add(new RuleMapping(entrySet.getKey(), entrySet.getValue().prefcommons,
                entrySet.getValue().rules.toArray(new Rule[entrySet.getValue().rules.size()])));
    }
    return list;
}

From source file:com.omnigon.aem.common.utils.JcrPropertyUtil.java

private static void updateTagProperty(final Node node, final List<String> tags) throws RepositoryException {
    node.setProperty(TagConstants.PN_TAGS, tags.toArray(new String[tags.size()]));
}

From source file:Main.java

public static String[] split(String str, String delimiters) {
    final StringTokenizer st = new StringTokenizer(str, delimiters);
    final List<String> tokens = new ArrayList<String>();
    while (st.hasMoreTokens()) {
        final String token = st.nextToken();
        tokens.add(token);//from   w  w  w . j  av a  2  s  . c om
    }
    return tokens.toArray(new String[tokens.size()]);
}

From source file:Main.java

public static Integer[] toIntegerArray(int[] array) {
    if (array == null) {
        return null;
    }//from  w w w .ja v a2 s . c  o  m

    List<Integer> newArray = new ArrayList<Integer>();

    final int N = array.length;

    for (int i = 0; i < N; i++) {
        newArray.add(array[i]);
    }

    return newArray.toArray(new Integer[0]);
}