Example usage for org.apache.commons.collections4 IterableUtils toList

List of usage examples for org.apache.commons.collections4 IterableUtils toList

Introduction

In this page you can find the example usage for org.apache.commons.collections4 IterableUtils toList.

Prototype

public static <E> List<E> toList(final Iterable<E> iterable) 

Source Link

Document

Gets a new list with the contents of the provided iterable.

Usage

From source file:fr.landel.utils.assertor.utils.AssertorIterable.java

private static <I extends Iterable<T>, T> boolean hasInOrder(final I iterable1, final Iterable<T> iterable2,
        final boolean not, final EnumAnalysisMode analysisMode) {

    long found = 0;
    final int size1 = IterableUtils.size(iterable1);
    final int size2 = IterableUtils.size(iterable2);

    if (size1 < size2) {
        return not;
    } else if (size1 == size2) {
        return not ^ iterable1.equals(iterable2);
    }/*from w  ww.  j a  v  a2 s  .c  o  m*/

    if (EnumAnalysisMode.STANDARD.equals(analysisMode)) {
        final Iterator<T> iterator1 = iterable1.iterator();
        Iterator<T> iterator2 = iterable2.iterator();

        // not empty pre-check, so we call next directly
        T value2 = iterator2.next();
        while (iterator1.hasNext() && found < size2) {
            if (Objects.equals(iterator1.next(), value2)) {
                ++found;
                if (iterator2.hasNext()) {
                    value2 = iterator2.next();
                }
            } else if (found > 0) {
                found = 0;
                iterator2 = iterable2.iterator();
                value2 = iterator2.next();
            }
        }
    } else {
        final AtomicInteger count = new AtomicInteger(0);

        final List<T> list2 = IterableUtils.toList(iterable2);

        StreamSupport.stream(iterable1.spliterator(), EnumAnalysisMode.PARALLEL.equals(analysisMode))
                .forEachOrdered(o -> {
                    int inc = count.get();
                    if (inc < size2) {
                        if (Objects.equals(o, list2.get(inc))) {
                            count.incrementAndGet();
                        } else if (inc > 0) {
                            count.set(0);
                        }
                    }
                });

        found = count.get();
    }

    return not ^ (found == size2);
}

From source file:fr.landel.utils.assertor.utils.AssertorMap.java

/**
 * Prepare the next step to validate if the {@link Map} contains the entries
 * in a specified order. To work correctly, the map must be sorted or
 * linked.//from   ww w . jav a 2  s .  c om
 * 
 * <p>
 * precondition: {@link Map} cannot be {@code null} or empty
 * </p>
 * 
 * @param step
 *            the current step
 * @param map
 *            the map entries to find
 * @param message
 *            the message if invalid
 * @param <M>
 *            the {@link Map} type
 * @param <K>
 *            the {@link Map} key elements type
 * @param <V>
 *            the {@link Map} value elements type
 * @return the next step
 */
public static <M extends Map<K, V>, K, V> StepAssertor<M> containsInOrder(final StepAssertor<M> step,
        final Map<K, V> map, final MessageAssertor message) {

    final Predicate<M> preChecker = map1 -> MapUtils.isNotEmpty(map1) && MapUtils.isNotEmpty(map);

    final BiPredicate<M, Boolean> checker = (map1, not) -> AssertorMap.hasInOrder(map1,
            IterableUtils.toList(map.entrySet()), not, step.getAnalysisMode(), MapUtils2::areEntriesEqual,
            CastUtils.cast(Entry.class));

    return new StepAssertor<>(step, preChecker, checker, true, message, MSG.MAP.CONTAINS_MAP_IN_ORDER, false,
            new ParameterAssertor<>(map, EnumType.MAP));
}

From source file:fr.landel.utils.assertor.utils.AssertorMap.java

private static <M extends Map<K, V>, K, V, T> boolean hasInOrder(final M map, final Iterable<T> objects,
        final boolean not, final EnumAnalysisMode analysisMode,
        final BiPredicate<Entry<K, V>, T> entriesEqualChecker, final Class<T> objectsClass) {

    int found = 0;

    final int size1 = map.size();
    final int size2 = IterableUtils.size(objects);

    if (size1 < size2) {
        return not;
    }//from   w  w  w.jav a 2  s. co m

    final Set<Entry<K, V>> entries1 = map.entrySet();
    final List<T> entries2 = IterableUtils.toList(objects);

    if (EnumAnalysisMode.STANDARD.equals(analysisMode)) {
        for (Entry<K, V> entry1 : entries1) {
            if (found < size2) {
                if (entriesEqualChecker.test(entry1, entries2.get(found))) {
                    ++found;
                } else if (found > 0) {
                    found = 0;
                }
            }
        }
    } else {
        final AtomicInteger count = new AtomicInteger(0);

        final Stream<Entry<K, V>> stream;
        if (EnumAnalysisMode.PARALLEL.equals(analysisMode)) {
            stream = entries1.parallelStream();
        } else {
            stream = entries1.stream();
        }

        stream.forEachOrdered(o -> {
            int inc = count.get();
            if (inc < size2) {
                if (entriesEqualChecker.test(o, entries2.get(inc))) {
                    count.incrementAndGet();
                } else if (inc > 0) {
                    count.set(0);
                }
            }
        });

        found = count.get();
    }

    return not ^ (found == size2);
}

From source file:org.apache.syncope.client.console.panels.ResourceMappingPanel.java

private List<String> getSchemaNames(final Long connectorId, final Set<ConnConfProperty> conf) {
    final ConnInstanceTO connInstanceTO = new ConnInstanceTO();
    connInstanceTO.setKey(connectorId);/*from w  w  w .j a  v  a2  s  .co  m*/
    connInstanceTO.getConf().addAll(conf);

    // SYNCOPE-156: use provided info to give schema names (and type!) by ObjectClass
    ConnIdObjectClassTO clazz = IterableUtils.find(connRestClient.buildObjectClassInfo(connInstanceTO, true),
            new Predicate<ConnIdObjectClassTO>() {

                @Override
                public boolean evaluate(final ConnIdObjectClassTO object) {
                    return object.getType()
                            .equalsIgnoreCase(ResourceMappingPanel.this.provisionTO.getObjectClass());
                }
            });

    return clazz == null ? new ArrayList<String>()
            : IterableUtils
                    .toList(IterableUtils.filteredIterable(clazz.getAttributes(), new Predicate<String>() {

                        @Override
                        public boolean evaluate(final String object) {
                            return !("__NAME__".equals(object) || "__ENABLE__".equals(object)
                                    || "__PASSWORD__".equals(object));
                        }
                    }));
}

From source file:org.craftercms.commons.git.impl.GitRepositoryImplTest.java

@Test
public void testCommit() throws Exception {
    Git git = Git.init().setDirectory(tmpDir.getRoot()).call();
    GitRepositoryImpl repository = new GitRepositoryImpl(git);

    File testFile = new File(tmpDir.getRoot(), "test");
    testFile.createNewFile();/*from ww w.  j a  v  a 2 s .  c  o m*/

    git.add().addFilepattern("test").call();

    repository.commit("Test message");

    List<RevCommit> commits = IterableUtils.toList(git.log().all().call());

    assertNotNull(commits);
    assertEquals(1, commits.size());
    assertEquals("Test message", commits.get(0).getFullMessage());
}

From source file:org.craftercms.commons.git.impl.GitRepositoryImplTest.java

@Test
public void testPush() throws Exception {
    File masterRepoDir = tmpDir.newFolder("master.git");
    File cloneRepoDir = tmpDir.newFolder("clone");

    Git masterGit = Git.init().setDirectory(masterRepoDir).setBare(true).call();
    Git cloneGit = Git.cloneRepository().setURI(masterRepoDir.getCanonicalPath()).setDirectory(cloneRepoDir)
            .call();//w w  w. java 2 s. c  om
    GitRepositoryImpl cloneRepo = new GitRepositoryImpl(cloneGit);

    File testFile = new File(cloneRepoDir, "test");
    testFile.createNewFile();

    cloneGit.add().addFilepattern("test").call();
    cloneGit.commit().setMessage("Test message").call();

    cloneRepo.push();

    List<RevCommit> commits = IterableUtils.toList(masterGit.log().all().call());

    assertNotNull(commits);
    assertEquals(1, commits.size());
    assertEquals("Test message", commits.get(0).getFullMessage());

    List<String> committedFiles = getCommittedFiles(masterGit.getRepository(), commits.get(0));

    assertNotNull(committedFiles);
    assertEquals(1, committedFiles.size());
    assertEquals("test", committedFiles.get(0));
}

From source file:org.craftercms.commons.git.impl.GitRepositoryImplTest.java

@Test
public void testPull() throws Exception {
    File masterRepoDir = tmpDir.newFolder("master");
    File cloneRepoDir = tmpDir.newFolder("clone");

    Git masterGit = Git.init().setDirectory(masterRepoDir).call();
    Git cloneGit = Git.cloneRepository().setURI(masterRepoDir.getCanonicalPath()).setDirectory(cloneRepoDir)
            .call();/*from  w w w.j av  a  2s . co m*/
    GitRepositoryImpl cloneRepo = new GitRepositoryImpl(cloneGit);

    File testFile = new File(masterRepoDir, "test");
    testFile.createNewFile();

    masterGit.add().addFilepattern("test").call();
    masterGit.commit().setMessage("Test message").call();

    cloneRepo.pull();

    List<RevCommit> commits = IterableUtils.toList(cloneGit.log().all().call());

    assertNotNull(commits);
    assertEquals(1, commits.size());
    assertEquals("Test message", commits.get(0).getFullMessage());

    List<String> committedFiles = getCommittedFiles(cloneGit.getRepository(), commits.get(0));

    assertNotNull(committedFiles);
    assertEquals(1, committedFiles.size());
    assertEquals("test", committedFiles.get(0));
}