Example usage for org.apache.commons.collections Transformer Transformer

List of usage examples for org.apache.commons.collections Transformer Transformer

Introduction

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

Prototype

Transformer

Source Link

Usage

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

@Test
public void testOrderAfter() throws RepositoryException, IOException {
    MockHierarchyManager hm = MockUtil.createHierarchyManager("/node/a\n" + "/node/b\n" + "/node/c\n");
    Content node = hm.getContent("/node");
    Content a = node.getContent("a");
    ContentUtil.orderAfter(a, "b");
    Collection<Content> result = node.getChildren();
    CollectionUtils.transform(result, new Transformer() {
        @Override/*from w  w w .j a v  a2 s .c  om*/
        public Object transform(Object childObj) {
            return ((Content) childObj).getName();
        }
    });
    assertEquals(Arrays.asList(new String[] { "b", "a", "c" }), result);
}

From source file:io.wcm.config.core.persistence.impl.ToolsConfigPagePersistenceProvider.java

@SuppressWarnings("unchecked")
private Iterator<Resource> getResourceInheritanceChainInternal(final String configName,
        final Iterator<String> paths, final ResourceResolver resourceResolver) {

    // find all matching items among all configured paths
    Iterator<Resource> matchingResources = IteratorUtils.transformedIterator(paths, new Transformer() {
        @Override/*from   w ww  . j  a v  a 2 s .  co  m*/
        public Object transform(Object input) {
            String configPath = buildResourcePath((String) input, configName);
            Resource resource = resourceResolver.getResource(configPath);
            if (resource != null) {
                log.trace("+ Found matching config resource for inheritance chain: {}", configPath);
            } else {
                log.trace("- No matching config resource for inheritance chain: {}", configPath);
            }
            return resource;
        }
    });
    Iterator<Resource> result = IteratorUtils.filteredIterator(matchingResources,
            PredicateUtils.notNullPredicate());
    if (result.hasNext()) {
        return result;
    } else {
        return null;
    }
}

From source file:com.base2.kagura.core.ExportHandler.java

/**
 * Takes the output and transforms it into a Excel file.
 * @param out Output stream.//  w ww  .ja  v a2  s  . c  o  m
 * @param rows Rows of data from reporting-core
 * @param columns Columns to list on report
 */
public void generateXls(OutputStream out, List<Map<String, Object>> rows, List<ColumnDef> columns) {
    try {
        Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook();
        String safeName = WorkbookUtil.createSafeSheetName("Report"); // returns " O'Brien's sales   "
        Sheet reportSheet = wb.createSheet(safeName);

        short rowc = 0;
        Row nrow = reportSheet.createRow(rowc++);
        short cellc = 0;
        if (rows == null)
            return;
        if (columns == null && rows.size() > 0) {
            columns = new ArrayList<ColumnDef>(CollectionUtils.collect(rows.get(0).keySet(), new Transformer() {
                @Override
                public Object transform(final Object input) {
                    return new ColumnDef() {
                        {
                            setName((String) input);
                        }
                    };
                }
            }));
        }
        if (columns != null) {
            for (ColumnDef column : columns) {
                Cell cell = nrow.createCell(cellc++);
                cell.setCellValue(column.getName());
            }
        }
        for (Map<String, Object> row : rows) {
            nrow = reportSheet.createRow(rowc++);
            cellc = 0;
            for (ColumnDef column : columns) {
                Cell cell = nrow.createCell(cellc++);
                cell.setCellValue(String.valueOf(row.get(column.getName())));
            }
        }
        wb.write(out);
    } catch (IOException e) {
        e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
    }

}

From source file:gov.nih.nci.caarray.web.fileupload.MonitoredMultiPartRequest.java

/**
 * {@inheritDoc}//w  ww  .ja  v  a2s .  c  o  m
 */
public String[] getContentType(String fieldName) {
    return transformFileItemsForField(fieldName, ArrayUtils.EMPTY_STRING_ARRAY, new Transformer() {
        public Object transform(Object o) {
            return ((FileItem) o).getContentType();
        }
    });
}

From source file:hudson.plugins.emailext.plugins.recipients.FailingTestSuspectsRecipientProviderTest.java

private static void addMockChangeSet(final AbstractBuild build, final String... inAuthors) {
    PowerMockito.when(build.getChangeSet()).thenReturn(new ChangeLogSet(build) {

        final String[] authors = inAuthors;

        @Override//from   ww w  .jav  a  2 s .c o m
        public boolean isEmptySet() {
            return authors.length == 0;
        }

        public Iterator iterator() {
            return new TransformIterator(Arrays.asList(authors).iterator(), new Transformer() {
                @Override
                public Object transform(final Object inAuthor) {
                    return new ChangeLogSet.Entry() {
                        @Override
                        public String getMsg() {
                            return "COMMIT MESSAGE";
                        }

                        @Override
                        public User getAuthor() {
                            return getMockUser((String) inAuthor);
                        }

                        @Override
                        public Collection<String> getAffectedPaths() {
                            return Collections.EMPTY_SET;
                        }

                        @Override
                        public String getMsgAnnotated() {
                            return getMsg();
                        }

                        @Override
                        public Collection<? extends ChangeLogSet.AffectedFile> getAffectedFiles() {
                            return Collections.EMPTY_SET;
                        }
                    };
                }

            });
        }
    });
}

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

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

From source file:jp.ikedam.jenkins.plugins.extensible_choice_parameter.GlobalTextareaChoiceListProviderJenkinsTest.java

@SuppressWarnings("unchecked") // CollectionUtils.transformedCollection is not generic.
@Test//from  w ww . j  a v a 2s.  c  o  m
public void testDescriptorDoFillNameItems() {
    GlobalTextareaChoiceListProvider.DescriptorImpl descriptor = getDescriptor();
    // Easy case
    {
        List<String> nameList = Arrays.asList("entry1", "entry2", "entry3");
        @SuppressWarnings("rawtypes")
        List choiceListEntry = new ArrayList(nameList);
        CollectionUtils.transform(choiceListEntry, new Transformer() {
            @Override
            public Object transform(Object input) {
                return new GlobalTextareaChoiceListEntry((String) input, "value1\nvalue2\n", false);
            }
        });
        descriptor.setChoiceListEntryList(choiceListEntry);

        ListBoxModel fillList = descriptor.doFillNameItems();

        @SuppressWarnings("rawtypes")
        List optionList = new ArrayList(nameList);
        CollectionUtils.transform(optionList, new Transformer() {
            @Override
            public Object transform(Object input) {
                String name = (String) input;
                return new ListBoxModel.Option(name, name);
            }
        });
        ListBoxModel expected = new ListBoxModel(optionList);

        assertListBoxEquals("Easy case", expected, fillList);
    }

    // Empty
    {
        descriptor.setChoiceListEntryList(new ArrayList<GlobalTextareaChoiceListEntry>(0));

        ListBoxModel fillList = descriptor.doFillNameItems();

        ListBoxModel expected = new ListBoxModel();

        assertListBoxEquals("Empty", expected, fillList);
    }

    // null
    {
        descriptor.setChoiceListEntryList(null);

        ListBoxModel fillList = descriptor.doFillNameItems();

        ListBoxModel expected = new ListBoxModel();

        assertListBoxEquals("null", expected, fillList);
    }
}

From source file:gov.nih.nci.caarray.web.fileupload.MonitoredMultiPartRequest.java

/**
 * {@inheritDoc}//from  www . j  av a 2  s.c  om
 */
public File[] getFile(String fieldName) {
    return transformFileItemsForField(fieldName, new File[0], new Transformer() {
        public Object transform(Object o) {
            return ((DiskFileItem) o).getStoreLocation();
        }
    });
}

From source file:com.alkacon.opencms.survey.CmsFormReportingBean.java

/**
 * Returns a lazy initialized map that provides the color of the label for each background color used as a key in the Map.<p> 
 * /*from  w ww . j a  va  2  s  .  c  o  m*/
 * Dark background color returns white.<p>
 * Light background color returns black.<p>
 * 
 * @return a lazy initialized map
 */
public Map getTextColor() {

    if (m_color == null) {
        m_color = LazyMap.decorate(new HashMap(), new Transformer() {

            /**
             * @see org.apache.commons.collections.Transformer#transform(java.lang.Object)
             */
            public Object transform(Object input) {

                try {
                    // get the color from the given value
                    String value = String.valueOf(input);
                    Color color = Color.decode(value);

                    // look if its a dark color or a light
                    int dezColor = color.getBlue() + color.getGreen() + color.getRed();
                    if (dezColor < SEP_DARK_LIGHT) {
                        return "#FFF";
                    }

                } catch (Exception e) {
                    // NOOP
                }
                return "#000";
            }
        });
    }
    return m_color;
}

From source file:gov.nih.nci.caarray.web.fileupload.MonitoredMultiPartRequest.java

/**
 * {@inheritDoc}//from   w w w. java2  s  . com
 */
public String[] getFileNames(String fieldName) {
    return transformFileItemsForField(fieldName, ArrayUtils.EMPTY_STRING_ARRAY, new Transformer() {
        public Object transform(Object o) {
            return getCanonicalName(((DiskFileItem) o).getName());
        }
    });
}