Example usage for org.apache.commons.collections4 CollectionUtils find

List of usage examples for org.apache.commons.collections4 CollectionUtils find

Introduction

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

Prototype

@Deprecated
public static <T> T find(final Iterable<T> collection, final Predicate<? super T> predicate) 

Source Link

Document

Finds the first element in the given collection which matches the given predicate.

Usage

From source file:de.micromata.tpsb.doc.parser.ParserResult.java

public FileInfo getFileInfoForClassName(final String className) {
    return CollectionUtils.find(this, new Predicate<FileInfo>() {
        @Override/*from www .j av a 2  s. com*/
        public boolean evaluate(FileInfo fInfo) {
            return StringUtils.equals(className, fInfo.getClassName())
                    || StringUtils.endsWith(fInfo.getClassName(), className);
        }
    });
}

From source file:de.tor.tribes.ui.models.TAPTargetTableModel.java

public void increaseRowCount(final Village pVillage) {
    Object result = CollectionUtils.find(elements, new Predicate() {

        @Override//from   ww w.ja  va  2  s .  com
        public boolean evaluate(Object o) {
            return ((TAPAttackTargetElement) o).getVillage().equals(pVillage);
        }
    });

    if (result != null) {
        TAPAttackTargetElement resultElem = (TAPAttackTargetElement) result;
        resultElem.addAttack();
        fireTableDataChanged();
    }
}

From source file:com.github.rvesse.airline.parser.aliases.AliasResolver.java

public PeekingIterator<String> resolveAliases(PeekingIterator<String> tokens, ParseState<T> state) {
    Predicate<? super CommandGroupMetadata> findGroupPredicate;
    Predicate<? super CommandMetadata> findCommandPredicate;

    // Nothing to do if no further tokens
    if (!tokens.hasNext())
        return tokens;

    // Nothing to do if no aliases defined
    if (state.getParserConfiguration().getAliases().size() == 0)
        return tokens;

    Set<String> aliasesSeen = new TreeSet<String>();

    do {/*from   w w  w.  j a v  a2  s  . co  m*/
        // Try to find an alias
        AliasMetadata alias = CollectionUtils.find(state.getParserConfiguration().getAliases(),
                new AliasFinder(tokens.peek()));

        // Nothing further to do if no aliases found
        if (alias == null)
            return tokens;

        // Check for circular references
        if (!aliasesSeen.add(alias.getName())) {
            throw new ParseAliasCircularReferenceException(alias.getName(), aliasesSeen);
        }

        // Can we override built-ins?
        if (!state.getParserConfiguration().aliasesOverrideBuiltIns()) {
            // If not we must check we don't have a default
            // group/command with the same name as otherwise that
            // would take precedence
            if (state.getGlobal() != null) {
                GlobalMetadata<T> metadata = state.getGlobal();
                findGroupPredicate = new GroupFinder(tokens.peek());
                findCommandPredicate = new CommandFinder(tokens.peek());
                if (CollectionUtils.find(metadata.getCommandGroups(), findGroupPredicate) != null
                        || CollectionUtils.find(metadata.getDefaultGroupCommands(),
                                findCommandPredicate) != null)
                    return tokens;
            }
        }

        // Discard the alias token
        tokens.next();

        // Apply the alias
        List<String> newParams = new ArrayList<String>();
        List<String> remainingParams = new ArrayList<String>();
        while (tokens.hasNext()) {
            remainingParams.add(tokens.next());
        }

        // Process alias arguments
        Set<Integer> used = new TreeSet<Integer>();
        for (String arg : alias.getArguments()) {
            if (arg.startsWith("$")) {
                // May be a positional parameter
                try {
                    int num = Integer.parseInt(arg.substring(1));
                    num--;

                    if (num >= 0 && num < remainingParams.size()) {
                        // Valid positional parameter
                        newParams.add(remainingParams.get(num));
                        used.add(num);
                        continue;
                    }
                } catch (NumberFormatException e) {
                    // Ignore - the number was invalid so we'll
                    // treat it as an ordinary parameter
                }
            }

            // Some other parameter
            newParams.add(arg);
        }

        // Remove used positional parameters
        int removed = 0;
        for (int pos : used) {
            remainingParams.remove(pos - removed);
            removed++;
        }

        // Pass through any remaining parameters
        for (String arg : remainingParams) {
            newParams.add(arg);
        }

        // Prepare a new tokens iterator
        tokens = new PeekingIterator<String>(newParams.iterator());
    } while (state.getParserConfiguration().aliasesMayChain());

    return tokens;
}

From source file:de.tor.tribes.types.DefenseInformation.java

public boolean addSupport(final Village pSource, UnitHolder pUnit, boolean pPrimary, boolean pMultiUse) {
    long runtime = DSCalculator.calculateMoveTimeInMillis(pSource, target, pUnit.getSpeed());
    boolean allowed = false;
    if (getFirstAttack().getTime() - runtime > System.currentTimeMillis() + DateUtils.MILLIS_PER_MINUTE) {
        //high priority
        allowed = true;//from   w w  w  .j a  v  a 2  s . c o m
    } else if (getLastAttack().getTime() - runtime > System.currentTimeMillis() + DateUtils.MILLIS_PER_MINUTE) {
        //low priority
        allowed = !pPrimary;
    } else {// if (getLastAttack().getTime() - runtime < System.currentTimeMillis() - DateUtils.MILLIS_PER_MINUTE) {
        //impossible
    }
    if (allowed) {
        Object result = CollectionUtils.find(defenses, new Predicate() {

            @Override
            public boolean evaluate(Object o) {
                return ((Defense) o).getSupporter().equals(pSource);
            }
        });
        if (result == null || pMultiUse) {
            defenses.add(new Defense(this, pSource, pUnit));
            return true;
        }
    }
    return false;
}

From source file:com.github.rvesse.airline.help.sections.TestHelpSectionDetection.java

@Test
public void help_section_cli_02() {
    Cli<Object> cli = new Cli<>(CliWithSections.class);
    CommandFinder finder = new CommandFinder("remove");
    CommandMetadata cmd = CollectionUtils.find(cli.getMetadata().getDefaultGroupCommands(), finder);
    Assert.assertNotNull(cmd);//from ww w .  j av  a  2 s  .c  o  m

    Assert.assertEquals(cmd.getHelpSections().size(), 2);
    HelpSection section = CollectionUtils.find(cmd.getHelpSections(), new HelpSectionFinder("Discussion"));
    Assert.assertTrue(section instanceof DiscussionSection);
    DiscussionSection discussion = (DiscussionSection) section;
    String[] paragraphs = discussion.getContentBlock(0);
    Assert.assertEquals(paragraphs.length, 1);
}

From source file:de.tor.tribes.ui.models.DefenseToolModel.java

public DefenseInformation findElement(final Village pTarget) {
    return (DefenseInformation) CollectionUtils.find(entries, new Predicate() {

        @Override/*  w w w . j a va2  s  .  co m*/
        public boolean evaluate(Object o) {
            return ((DefenseInformation) o).getTarget().equals(pTarget);
        }
    });
}

From source file:com.link_intersystems.lang.reflect.ChooseMostSpecificMemberStrategy.java

protected Member2<?> chooseMostSpecificImpl(List<? extends Member2<?>> potentiallyApplicable) {
    boolean containsVarargsInvokable = CollectionUtils.exists(potentiallyApplicable,
            VarargInvokablePredicate.INSTANCE);
    Member2<?> mostSpecific = null;
    if (containsVarargsInvokable) {
        Member2<?> noVarargsMethod = (Member2<?>) CollectionUtils.find(potentiallyApplicable,
                NotPredicate.notPredicate(VarargInvokablePredicate.INSTANCE));
        mostSpecific = noVarargsMethod;//from   ww  w . j a  v  a2s  .  com
    } else {
        MostSpecificInvokableClosure mostSpecificInvokableClosure = new MostSpecificInvokableClosure();
        CollectionUtils.forAllDo(potentiallyApplicable, mostSpecificInvokableClosure);
        mostSpecific = mostSpecificInvokableClosure.getMostSpecific();
    }
    return mostSpecific;
}

From source file:de.tor.tribes.ui.models.TAPTargetTableModel.java

public void decreaseRowCount(final Village pVillage) {
    Object result = CollectionUtils.find(elements, new Predicate() {

        @Override/*from  ww  w .  ja  v a2  s . com*/
        public boolean evaluate(Object o) {
            return ((TAPAttackTargetElement) o).getVillage().equals(pVillage);
        }
    });

    if (result != null) {
        TAPAttackTargetElement resultElem = (TAPAttackTargetElement) result;
        resultElem.removeAttack();
        fireTableDataChanged();
    }
}

From source file:de.tor.tribes.util.attack.StandardAttackManager.java

public StandardAttack getElementByName(final String pName) {
    Object result = CollectionUtils.find(getAllElements(), new Predicate() {

        @Override//from w w w.  j  a  va  2  s .  c  om
        public boolean evaluate(Object o) {
            return ((StandardAttack) o).getName().equals(pName);
        }
    });

    return (StandardAttack) result;
}

From source file:de.tor.tribes.util.sos.SOSManager.java

public SOSRequest getRequest(final Tribe pTribe) {
    if (pTribe == null) {
        return null;
    }/*w  w  w.  j a v a  2  s . c o m*/

    Object result = CollectionUtils.find(getAllElements(), new Predicate() {

        @Override
        public boolean evaluate(Object o) {
            return ((SOSRequest) o).getDefender().equals(pTribe);
        }
    });

    return (SOSRequest) result;
}