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.tor.tribes.util.attack.StandardAttackManager.java

public StandardAttack getElementByIcon(final int pIcon) {
    Object result = CollectionUtils.find(getAllElements(), new Predicate() {

        @Override/*from  w  w  w .j  a va2  s .com*/
        public boolean evaluate(Object o) {
            return ((StandardAttack) o).getIcon() == pIcon;
        }
    });

    return (StandardAttack) result;
}

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

@Test
public void help_section_cli_builder_01() {
    //@formatter:off
    Cli<Object> cli = new CliBuilder<>("test").withHelpSection(new DiscussionSection(new String[] { "A", "B" }))
            .withCommand(Args1.class).build();
    //@formatter:on
    CommandFinder finder = new CommandFinder("Args1");
    CommandMetadata cmd = CollectionUtils.find(cli.getMetadata().getDefaultGroupCommands(), finder);
    Assert.assertNotNull(cmd);// w w w .j  av a  2 s  . c  o  m

    Assert.assertEquals(cmd.getHelpSections().size(), 1);
    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, 2);
    Assert.assertEquals(paragraphs[0], "A");
    Assert.assertEquals(paragraphs[1], "B");
}

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

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

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

    return result != null;
}

From source file:com.github.rvesse.airline.TestSubGroups.java

@Test
public void sub_groups_02() {
    //@formatter:off
    CliBuilder<Object> builder = Cli.<Object>builder("test");
    GroupBuilder<Object> fooBuilder = builder.withGroup("foo");
    fooBuilder.withSubGroup("bar").withDefaultCommand(Help.class);
    fooBuilder.withSubGroup("baz");
    //@formatter:on

    Cli<Object> cli = builder.build();
    GlobalMetadata<Object> global = cli.getMetadata();
    Assert.assertEquals(global.getDefaultGroupCommands().size(), 0);
    Assert.assertEquals(global.getCommandGroups().size(), 1);

    CommandGroupMetadata parentGroup = global.getCommandGroups().get(0);
    Assert.assertNull(parentGroup.getParent());
    Assert.assertEquals(parentGroup.getName(), "foo");
    Assert.assertEquals(parentGroup.getCommands().size(), 0);
    Assert.assertEquals(parentGroup.getSubGroups().size(), 2);

    CommandGroupMetadata subGroup = CollectionUtils.find(parentGroup.getSubGroups(), new GroupFinder("bar"));
    Assert.assertNotNull(subGroup);/* w  w  w.  jav  a2  s  . c o m*/
    Assert.assertEquals(parentGroup, subGroup.getParent());
    Assert.assertEquals(subGroup.getName(), "bar");
    Assert.assertEquals(subGroup.getCommands().size(), 1);
    Assert.assertEquals(subGroup.getDefaultCommand().getType(), Help.class);

    subGroup = CollectionUtils.find(parentGroup.getSubGroups(), new GroupFinder("baz"));
    Assert.assertNotNull(subGroup);
    Assert.assertEquals(parentGroup, subGroup.getParent());
    Assert.assertEquals(subGroup.getName(), "baz");
    Assert.assertEquals(subGroup.getCommands().size(), 0);
    Assert.assertNull(subGroup.getDefaultCommand());

    Object cmd = cli.parse("foo", "bar");
    Assert.assertTrue(cmd instanceof Help);
}

From source file:com.github.rvesse.airline.TestGalaxyCommandLineParser.java

@Test
public void test_metadata() {
    Cli<GalaxyCommand> cli = createParser();

    GlobalMetadata<GalaxyCommand> global = cli.getMetadata();
    Assert.assertEquals(global.getOptions().size(), 2);

    CommandMetadata show = CollectionUtils.find(global.getDefaultGroupCommands(), new CommandFinder("show"));
    Assert.assertNotNull(show);/*from   w w w .  jav  a  2  s.  c  o  m*/
    Assert.assertEquals(show.getCommandOptions().size(), 6);
    Assert.assertEquals(show.getAllOptions().size(), 8);
}

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

public boolean containsElementByIcon(final int pIcon) {
    Object result = CollectionUtils.find(getAllElements(), new Predicate() {

        @Override/* www.j a va2 s. c  o m*/
        public boolean evaluate(Object o) {
            return ((StandardAttack) o).getIcon() == pIcon;
        }
    });

    return result != null;
}

From source file:com.github.rvesse.airline.utils.AirlineUtils.java

public static <T> T find(Iterable<T> collection, Predicate<T> predicate, T defaultValue) {
    if (collection == null)
        return defaultValue;
    T value = CollectionUtils.find(collection, predicate);
    if (value == null)
        return defaultValue;
    return value;
}

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

@Test
public void help_section_cli_builder_02() {
    //@formatter:off
    Cli<Object> cli = new CliBuilder<>("test").withHelpSection(new DiscussionSection(new String[] { "A", "B" }))
            .withCommand(Args1HidesDiscussion.class).build();
    //@formatter:on
    CommandFinder finder = new CommandFinder("Args1");
    CommandMetadata cmd = CollectionUtils.find(cli.getMetadata().getDefaultGroupCommands(), finder);
    Assert.assertNotNull(cmd);/*  w w  w . j av a2 s . c o m*/
    Assert.assertEquals(cmd.getHelpSections().size(), 1);
    HelpSection section = CollectionUtils.find(cmd.getHelpSections(), new HelpSectionFinder("Discussion"));
    Assert.assertTrue(section instanceof BasicSection);
    BasicSection basic = (BasicSection) section;
    Assert.assertEquals(basic.getTitle(), "Discussion");
    Assert.assertEquals(basic.getFormat(), HelpFormat.NONE_PRINTABLE);
}

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

public boolean addAttack(final Village pSource, final Date pArrive, UnitHolder pUnit, boolean fake,
        boolean snob) {
    List<TimedAttack> attacksFromSource = timedAttacks.get(pSource);
    if (attacksFromSource == null) {
        attacksFromSource = new LinkedList<>();
        timedAttacks.put(pSource, attacksFromSource);
    }//from  ww  w  .j  av a 2s. co  m

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

        @Override
        public boolean evaluate(Object o) {
            TimedAttack t = (TimedAttack) o;
            return t.getSource().equals(pSource) && t.getlArriveTime().equals(pArrive.getTime());
        }
    });

    if (result == null) {
        TimedAttack a = new TimedAttack(pSource, pArrive);
        a.setPossibleFake(fake);
        a.setPossibleSnob(snob);
        a.setUnit(pUnit);
        attacksFromSource.add(a);
        Collections.sort(attacksFromSource, SOSRequest.ARRIVE_TIME_COMPARATOR);
        updateAttackInfo();
        return true;
    }
    return false;
}

From source file:com.github.rvesse.airline.help.Help.java

/**
 * Displays plain text format program help to the given output stream
 * /*from w w w  .j ava2  s  .c om*/
 * @param global
 *            Program meta-data
 * @param commandNames
 *            Command Names
 * @param out
 *            Output Stream
 * @throws IOException
 */
public static <T> void help(GlobalMetadata<T> global, List<String> commandNames, boolean includeHidden,
        OutputStream out) throws IOException {
    if (commandNames.isEmpty()) {
        new CliGlobalUsageSummaryGenerator<T>(includeHidden).usage(global, out);
        return;
    }

    String name = commandNames.get(0);

    // Main program?
    if (name.equals(global.getName())) {
        // Main program help
        new CliGlobalUsageGenerator<T>(includeHidden).usage(global, out);
        return;
    }

    // Predicates we may need
    Predicate<CommandGroupMetadata> findGroupPredicate;
    Predicate<CommandMetadata> findCommandPredicate;
    //@formatter:off
    findGroupPredicate = global.getParserConfiguration().allowsAbbreviatedCommands()
            ? new AbbreviatedGroupFinder(name, global.getCommandGroups())
            : new GroupFinder(name);
    //@formatter:on

    // A command in a group?
    CommandMetadata command;
    CommandGroupMetadata group = CollectionUtils.find(global.getCommandGroups(), findGroupPredicate);
    if (group != null) {
        List<CommandGroupMetadata> groupPath = new ArrayList<CommandGroupMetadata>();
        groupPath.add(group);

        // General group help or specific group command help?
        if (commandNames.size() == 1) {
            // General group help
            new CliCommandGroupUsageGenerator<T>(includeHidden).usage(global,
                    groupPath.toArray(new CommandGroupMetadata[0]), out);
            return;
        } else {
            // Group/Sub-Group command help
            int i = 1;
            String commandOrSubGroupName = commandNames.get(i);

            while (group.getSubGroups().size() > 0 && i < commandNames.size()) {
                commandOrSubGroupName = commandNames.get(i);

                //@formatter:off
                findGroupPredicate = global.getParserConfiguration().allowsAbbreviatedCommands()
                        ? new AbbreviatedGroupFinder(commandOrSubGroupName, group.getSubGroups())
                        : new GroupFinder(commandOrSubGroupName);
                //@formatter:on
                CommandGroupMetadata subGroup = CollectionUtils.find(group.getSubGroups(), findGroupPredicate);
                if (subGroup != null) {
                    // Found a valid sub-group
                    groupPath.add(subGroup);
                    group = subGroup;
                    i++;
                    if (i == commandNames.size()) {
                        // General sub-group help
                        new CliCommandGroupUsageGenerator<T>(includeHidden).usage(global,
                                groupPath.toArray(new CommandGroupMetadata[0]), out);
                        return;
                    }
                } else {
                    // No relevant sub-group found
                    break;
                }
            }

            // Look for a command in the current group/sub-group
            commandOrSubGroupName = commandNames.get(i);

            //@formatter:off
            findCommandPredicate = global.getParserConfiguration().allowsAbbreviatedCommands()
                    ? new AbbreviatedCommandFinder(commandOrSubGroupName, group.getCommands())
                    : new CommandFinder(commandOrSubGroupName);
            //@formatter:on
            command = CollectionUtils.find(group.getCommands(), findCommandPredicate);
            if (command != null) {
                new CliCommandUsageGenerator().usage(global.getName(), UsageHelper.toGroupNames(groupPath),
                        command.getName(), command, global.getParserConfiguration(), out);
                return;
            }

            // Didn't find an appropriate command
            if (global.getParserConfiguration().allowsAbbreviatedCommands()) {
                System.out.println("Unknown command " + name + " " + commandOrSubGroupName
                        + " or an ambiguous abbreviation");
            } else {
                System.out.println("Unknown command " + name + " " + commandOrSubGroupName);
            }
        }
    }

    // A command in the default group?
    //@formatter:off
    findCommandPredicate = global.getParserConfiguration().allowsAbbreviatedCommands()
            ? new AbbreviatedCommandFinder(name, global.getDefaultGroupCommands())
            : new CommandFinder(name);
    //@formatter:on
    command = CollectionUtils.find(global.getDefaultGroupCommands(), findCommandPredicate);
    if (command != null) {
        // Command in default group help
        new CliCommandUsageGenerator(includeHidden).usage(global.getName(), null, command.getName(), command,
                global.getParserConfiguration(), out);
        return;
    }

    // Didn't find an appropriate group
    if (global.getParserConfiguration().allowsAbbreviatedCommands()) {
        System.out.println("Unknown command " + name + " or an ambiguous abbreviation");
    } else {
        System.out.println("Unknown command " + name);
    }
}