Example usage for org.apache.commons.collections CollectionUtils transform

List of usage examples for org.apache.commons.collections CollectionUtils transform

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils transform.

Prototype

public static void transform(Collection collection, Transformer transformer) 

Source Link

Document

Transform the collection by applying a Transformer to each element.

Usage

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLCollection.java

/**
 * Returns the element or elements that match the specified key. If it is the name
 * of a property, the property value is returned. If it is the id of an element in
 * the array, that element is returned. Finally, if it is the name of an element or
 * elements in the array, then all those elements are returned. Otherwise,
 * {@link #NOT_FOUND} is returned.// www  .  java  2 s  .com
 * {@inheritDoc}
 */
@Override
protected Object getWithPreemption(final String name) {
    // Test to see if we are trying to get the length of this collection?
    // If so return NOT_FOUND here to let the property be retrieved using the prototype
    if (xpath_ == null || "length".equals(name)) {
        return NOT_FOUND;
    }

    final List<Object> elements = getElements();
    CollectionUtils.transform(elements, transformer_);

    // See if there is an element in the element array with the specified id.
    for (final Object next : elements) {
        if (next instanceof DomElement) {
            final String id = ((DomElement) next).getAttribute("id");
            if (id != null && id.equals(name)) {
                if (getBrowserVersion().hasFeature(BrowserVersionFeatures.HTMLCOLLECTION_IDENTICAL_IDS)) {
                    int totalIDs = 0;
                    for (final Object o : elements) {
                        if (o instanceof DomElement && name.equals(((DomElement) o).getAttribute("id"))) {
                            totalIDs++;
                        }
                    }
                    if (totalIDs > 1) {
                        final HTMLCollectionTags collection = new HTMLCollectionTags(
                                (SimpleScriptable) getParentScope());
                        collection.setAvoidObjectDetection(
                                !getBrowserVersion().hasFeature(BrowserVersionFeatures.GENERATED_46));
                        collection.init(node_, ".//*[@id='" + id + "']");
                        return collection;
                    }
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Property \"" + name + "\" evaluated (by id) to " + next);
                }
                return getScriptableForElement(next);
            }
        } else if (next instanceof WebWindow) {
            final WebWindow window = (WebWindow) next;
            final String windowName = window.getName();
            if (windowName != null && windowName.equals(name)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Property \"" + name + "\" evaluated (by name) to " + window);
                }
                return getScriptableForElement(window);
            }
            if (getBrowserVersion().hasFeature(BrowserVersionFeatures.GENERATED_47)
                    && window instanceof FrameWindow
                    && ((FrameWindow) window).getFrameElement().getAttribute("id").equals(name)) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Property \"" + name + "\" evaluated (by id) to " + window);
                }
                return getScriptableForElement(window);
            }
        } else {
            LOG.warn("Unrecognized type in collection: " + next + " (" + next.getClass().getName() + ")");
        }
    }

    // See if there are any elements in the element array with the specified name.
    final HTMLCollection array = new HTMLCollection(this);
    final String newCondition = "@name = '" + name.replaceAll("\\$", "\\\\\\$") + "'";
    final String xpathExpr;
    if (xpath_.endsWith("]")) {
        xpathExpr = xpath_.replaceAll("\\[([^\\]]*)\\]$", "[($1) and " + newCondition + "]");
    } else {
        xpathExpr = xpath_ + "[" + newCondition + "]";
    }
    array.init(node_, xpathExpr);

    final List<Object> subElements = array.getElements();
    if (subElements.size() > 1) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Property \"" + name + "\" evaluated (by name) to " + array + " with "
                    + subElements.size() + " elements");
        }
        return array;
    } else if (subElements.size() == 1) {
        final Scriptable singleResult = getScriptableForElement(subElements.get(0));
        if (LOG.isDebugEnabled()) {
            LOG.debug("Property \"" + name + "\" evaluated (by name) to " + singleResult);
        }
        return singleResult;
    }

    // Nothing was found.
    return NOT_FOUND;
}

From source file:info.magnolia.cms.util.ContentUtilTest.java

@Test
public void testOrderBeforeFirstNodeVariation2() throws RepositoryException, IOException {
    MockHierarchyManager hm = MockUtil.createHierarchyManager(
            "/node/a\n" + "/node/b\n" + "/node/c\n" + "/node/d\n" + "/node/e\n" + "/node/f\n");
    Content node = hm.getContent("/node");
    Content c = node.getContent("b");
    ContentUtil.orderBefore(c, "a");
    Collection<Content> result = node.getChildren();
    CollectionUtils.transform(result, new Transformer() {
        @Override//from  w  ww.  ja va2  s  . com
        public Object transform(Object childObj) {
            return ((Content) childObj).getName();
        }
    });
    assertEquals(asList("b", "a", "c", "d", "e", "f"), result);
}

From source file:info.magnolia.cms.util.ContentUtilTest.java

@Test
public void testOrderBeforeFirstNodeVariation3() throws RepositoryException, IOException {
    MockHierarchyManager hm = MockUtil.createHierarchyManager(
            "/node/a\n" + "/node/b\n" + "/node/c\n" + "/node/d\n" + "/node/e\n" + "/node/f\n");
    Content node = hm.getContent("/node");
    Content c = node.getContent("a");
    ContentUtil.orderBefore(c, "b");
    Collection<Content> result = node.getChildren();
    CollectionUtils.transform(result, new Transformer() {
        @Override/*from ww w.  j a  v  a  2s  .c om*/
        public Object transform(Object childObj) {
            return ((Content) childObj).getName();
        }
    });
    assertEquals(asList("a", "b", "c", "d", "e", "f"), result);
}

From source file:com.algoTrader.entity.marketData.BarDaoBase.java

/**
 * {@inheritDoc}//  www .  j a v  a  2s  .  c  o m
 */
@Override
public final void barVOToEntityCollection(Collection<?> instances) {
    if (instances != null) {
        for (final Iterator<?> iterator = instances.iterator(); iterator.hasNext();) {
            // - remove an objects that are null or not of the correct instance
            if (!(iterator.next() instanceof BarVO)) {
                iterator.remove();
            }
        }
        CollectionUtils.transform(instances, this.BarVOToEntityTransformer);
    }
}

From source file:info.magnolia.cms.util.ContentUtilTest.java

@Test
public void testOrderBeforeFirstNodeVariation4() throws RepositoryException, IOException {
    MockHierarchyManager hm = MockUtil.createHierarchyManager(
            "/node/a\n" + "/node/b\n" + "/node/c\n" + "/node/d\n" + "/node/e\n" + "/node/f\n");
    Content node = hm.getContent("/node");
    Content c = node.getContent("f");
    ContentUtil.orderBefore(c, "a");
    Collection<Content> result = node.getChildren();
    CollectionUtils.transform(result, new Transformer() {
        @Override/*from w  w w .j a  v  a2s  .co m*/
        public Object transform(Object childObj) {
            return ((Content) childObj).getName();
        }
    });
    assertEquals(asList("f", "a", "b", "c", "d", "e"), result);
}

From source file:com.algoTrader.entity.marketData.TickDaoBase.java

/**
 * {@inheritDoc}/*from w w w .j  a  v  a2 s  . c  om*/
 */
@Override
public final Collection toRawTickVOCollection(Collection<?> entities) {
    Collection result = new ArrayList<RawTickVO>();
    if (entities != null) {
        CollectionUtils.transform(entities, this.RAWTICKVO_TRANSFORMER);
        result.addAll((Collection) entities);
    }
    return result;
}

From source file:info.magnolia.cms.util.ContentUtilTest.java

@Test
public void testOrderBeforeLastNodeVariation1() throws RepositoryException, IOException {
    MockHierarchyManager hm = MockUtil.createHierarchyManager(
            "/node/a\n" + "/node/b\n" + "/node/c\n" + "/node/d\n" + "/node/e\n" + "/node/f\n");
    Content node = hm.getContent("/node");
    Content c = node.getContent("a");
    ContentUtil.orderBefore(c, "f");
    Collection<Content> result = node.getChildren();
    CollectionUtils.transform(result, new Transformer() {
        @Override//from  ww  w .  j  av  a2  s  .c  o  m
        public Object transform(Object childObj) {
            return ((Content) childObj).getName();
        }
    });
    assertEquals(asList("b", "c", "d", "e", "a", "f"), result);
}

From source file:info.magnolia.cms.util.ContentUtilTest.java

@Test
public void testOrderBeforeLastNodeVariation2() throws RepositoryException, IOException {
    MockHierarchyManager hm = MockUtil.createHierarchyManager(
            "/node/a\n" + "/node/b\n" + "/node/c\n" + "/node/d\n" + "/node/e\n" + "/node/f\n");
    Content node = hm.getContent("/node");
    Content c = node.getContent("c");
    ContentUtil.orderBefore(c, "f");
    Collection<Content> result = node.getChildren();
    CollectionUtils.transform(result, new Transformer() {
        @Override/*  w ww.  j a va  2s . c  o m*/
        public Object transform(Object childObj) {
            return ((Content) childObj).getName();
        }
    });
    assertEquals(asList("a", "b", "d", "e", "c", "f"), result);
}

From source file:info.magnolia.cms.util.ContentUtilTest.java

@Test
public void testOrderBeforeLastNodeVariation3() throws RepositoryException, IOException {
    MockHierarchyManager hm = MockUtil.createHierarchyManager(
            "/node/a\n" + "/node/b\n" + "/node/c\n" + "/node/d\n" + "/node/e\n" + "/node/f\n");
    Content node = hm.getContent("/node");
    Content c = node.getContent("e");
    ContentUtil.orderBefore(c, "f");
    Collection<Content> result = node.getChildren();
    CollectionUtils.transform(result, new Transformer() {
        @Override//from w w w. j  a v a 2  s .c  om
        public Object transform(Object childObj) {
            return ((Content) childObj).getName();
        }
    });
    assertEquals(asList("a", "b", "c", "d", "e", "f"), result);
}

From source file:com.algoTrader.entity.marketData.TickDaoBase.java

/**
 * {@inheritDoc}/*from   www  .ja  va  2s  .c  om*/
 */
@Override
public final void rawTickVOToEntityCollection(Collection<?> instances) {
    if (instances != null) {
        for (final Iterator<?> iterator = instances.iterator(); iterator.hasNext();) {
            // - remove an objects that are null or not of the correct instance
            if (!(iterator.next() instanceof RawTickVO)) {
                iterator.remove();
            }
        }
        CollectionUtils.transform(instances, this.RawTickVOToEntityTransformer);
    }
}