Example usage for org.apache.commons.collections IteratorUtils toArray

List of usage examples for org.apache.commons.collections IteratorUtils toArray

Introduction

In this page you can find the example usage for org.apache.commons.collections IteratorUtils toArray.

Prototype

public static Object[] toArray(Iterator iterator, Class arrayClass) 

Source Link

Document

Gets an array based on an iterator.

Usage

From source file:de.tudarmstadt.lt.nlkg.EvaluateArgs.java

static boolean predictEntailingContainedInNTopEntries(DT dt, String arg_l, String arg_r) {
    DT.Entry e = dt.get(arg_l, ntop);/*from  w w w . j  a  v  a  2  s  . c o m*/
    @SuppressWarnings("unchecked")
    Iterator<String> string_iter = IteratorUtils.transformedIterator(e.dtwords, new Transformer() {
        @Override
        public Object transform(Object input) {
            return ((Word) input).word;
        }
    });

    Set<String> dtwords = new HashSet<String>(
            Arrays.asList((String[]) IteratorUtils.toArray(string_iter, String.class)));
    return dtwords.contains(arg_r);
}

From source file:org.seasar.karrta.jcr.intercepter.OcmInterceptor.java

@SuppressWarnings("unchecked")
public Object invoke(MethodInvocation invocation) throws Throwable {
    long start, end;
    start = System.currentTimeMillis();

    String className = invocation.getClass().getName();
    className = className.substring(0, className.indexOf("$$"));

    logger_.debug("::: classname:[" + invocation.getThis().getClass().getName() + "]");

    Ocm ocmAnnotation = Class.forName(className).getAnnotation(Ocm.class);
    Class<?> bean = ocmAnnotation.bean();

    boolean canInvoke = false;
    int methodType = this.checkMethod(invocation.getMethod().getName());

    Object[] args = invocation.getArguments();
    if (args != null && args.length == 1) {
        for (Object arg : args) {
            if (this.checkArgument(methodType, bean, arg)) {
                canInvoke = true;/*from   ww  w  .ja  v a2 s . c om*/
                break;
            }
        }
    }
    if (methodType == TYPE_ERROR_CODE) {
        throw new InvalidMethodNameException("");
    }
    if (!canInvoke) {
        throw new InvalidArgumentException("");
    }
    ObjectContentManager ocm = this.ocmFactory_.getObjectContentManager();
    Object result = null;

    switch (methodType) {
    case TYPE_CREATE_CODE:
        ocm.insert(args[0]);
        ocm.save();
        break;
    case TYPE_UPDATE_CODE:
        ocm.update(args[0]);
        ocm.save();
        break;
    case TYPE_REMOVE_CODE:
        ocm.remove(args[0]);
        ocm.save();
        break;
    case TYPE_FIND_CODE:
        List collection = new ArrayList();
        Iterator<?> iterator = ocm.getObjectIterator((Query) args[0]);

        boolean isArray = invocation.getMethod().getReturnType().isArray();
        if (!isArray) {
            while (iterator.hasNext()) {
                collection.add(iterator.next());
            }
        }
        result = isArray ? IteratorUtils.toArray(iterator, bean)
                : collection.size() == 0 ? null : collection.get(0);
        break;
    case TYPE_GET_BY_UUID_CODE:
        result = ocm.getObjectByUuid((String) args[0]);
        break;
    }
    end = System.currentTimeMillis();
    logger_.debug("::::: [" + "processing time:[" + (end - start) + "ms] :::::");

    return result;
}

From source file:servletunit.struts.Common.java

/**
 * Common method to verify action errors and action messages.
 *///from ww w .  java  2  s  .  co m
protected static void verifyActionMessages(HttpServletRequest request, String[] messageNames, String key,
        String messageLabel) {
    if (logger.isTraceEnabled())
        logger.trace("Entering - request = " + request + ", messageNames = " + messageNames + ", key = " + key
                + ", messageLabel = " + messageLabel);

    ActionMessages messages = (ActionMessages) request.getAttribute(key);
    if (logger.isDebugEnabled()) {
        logger.debug("retrieved ActionMessages = " + messages);
    }

    if (messages == null) {
        fail("was expecting some " + messageLabel + " messages, but received none.");
    }
    /* check length of messages as optimization */
    else if (messages.size() != messageNames.length) {
        fail("was expecting " + messageNames.length + " " + messageLabel + " message(s), but received "
                + messages.size() + " " + messageLabel + " message(s)");
    } else {
        /* alphabetize the two lists of message keys and compare them */

        Iterator iter = new TransformIterator(messages.get(), new Transformer() {
            public Object transform(Object input) {
                return ((ActionMessage) input).getKey();
            }
        });

        String[] messageKeys = (String[]) IteratorUtils.toArray(iter, String.class);

        Arrays.sort(messageKeys);
        Arrays.sort(messageNames);

        for (int i = 0; i < messageNames.length; i++) {
            if (!messageNames[i].equals(messageKeys[i])) {
                StringBuffer mks = new StringBuffer();
                StringBuffer mns = new StringBuffer();

                for (int j = 0; j < messageKeys.length; j++)
                    mks.append(messageKeys[j] + " ");
                for (int k = 0; k < messageNames.length; k++)
                    mns.append(messageNames[k] + " ");

                fail("received " + messageLabel + " messages: (" + mks + ") but expected (" + mns + ")");
            }
        }
    }
    if (logger.isTraceEnabled())
        logger.trace("verifyActionMessages()");
}