Example usage for com.google.common.collect Iterables find

List of usage examples for com.google.common.collect Iterables find

Introduction

In this page you can find the example usage for com.google.common.collect Iterables find.

Prototype

public static <T> T find(Iterable<T> iterable, Predicate<? super T> predicate) 

Source Link

Document

Returns the first element in iterable that satisfies the given predicate; use this method only when such an element is known to exist.

Usage

From source file:com.eviware.loadui.ui.fx.views.canvas.terminal.ConnectionView.java

public ConnectionView(final Connection connection, final CanvasObjectView outputComponentView,
        final CanvasObjectView inputComponentView) {
    super();//  ww  w.j ava 2s. c  o  m

    getStyleClass().add("connection-view");

    this.connection = connection;

    final OutputTerminal outputTerminal = connection.getOutputTerminal();
    outputTerminalView = Iterables.find(outputComponentView.getOutputTerminalViews(),
            new Predicate<OutputTerminalView>() {
                @Override
                public boolean apply(OutputTerminalView input) {
                    return input.getTerminal().equals(outputTerminal);
                }
            });

    final InputTerminal inputTerminal = connection.getInputTerminal();
    inputTerminalView = Iterables.find(inputComponentView.getInputTerminalViews(),
            new Predicate<InputTerminalView>() {
                @Override
                public boolean apply(InputTerminalView input) {
                    return input.getTerminal().equals(inputTerminal);
                }
            });

    Runnable redraw = new Runnable() {
        @Override
        public void run() {
            requestLayout();
        }
    };

    outputTerminalView.setOnLayout(redraw);
    inputTerminalView.setOnLayout(redraw);

    final EventHandler<DraggableEvent> eventHandler = new EventHandler<DraggableEvent>() {
        @Override
        public void handle(DraggableEvent arg0) {
            updateWire();
        }
    };
    outputComponentView.addEventHandler(DraggableEvent.DRAGGABLE_DRAGGED, eventHandler);
    inputComponentView.addEventHandler(DraggableEvent.DRAGGABLE_DRAGGED, eventHandler);

    //Remove listeners when the Connection is disconnected:
    outputTerminal.addEventListener(TerminalConnectionEvent.class,
            new com.eviware.loadui.api.events.EventHandler<TerminalConnectionEvent>() {
                @Override
                public void handleEvent(TerminalConnectionEvent event) {
                    if (Objects.equal(event.getConnection(), connection)
                            && event.getEvent() == TerminalConnectionEvent.Event.DISCONNECT) {
                        outputComponentView.removeEventHandler(DraggableEvent.DRAGGABLE_DRAGGED, eventHandler);
                        inputComponentView.removeEventHandler(DraggableEvent.DRAGGABLE_DRAGGED, eventHandler);
                        outputTerminal.removeEventListener(TerminalConnectionEvent.class, this);
                    }
                }
            });
}

From source file:org.apache.brooklyn.entity.messaging.rabbit.RabbitDestination.java

@Override
public void onManagementStarting() {
    super.onManagementStarting();

    exchange = (getConfig(EXCHANGE_NAME) != null) ? getConfig(EXCHANGE_NAME) : getDefaultExchangeName();
    virtualHost = getConfig(RabbitBroker.VIRTUAL_HOST_NAME);
    sensors().set(RabbitBroker.VIRTUAL_HOST_NAME, virtualHost);

    machine = (SshMachineLocation) Iterables.find(getParent().getLocations(),
            Predicates.instanceOf(SshMachineLocation.class));
    shellEnvironment = getParent().getShellEnvironment();
}

From source file:com.bennavetta.vetinari.build.internal.phase.RenderPhase.java

private Renderer getRenderer(Page page, Site site) {
    Renderer renderer = null;/*  ww  w  . j a  v  a 2 s.co  m*/
    // The last file extension is always the renderer, if anything.
    final String extension = getFileExtension(page.getPath().toString());
    Optional<Renderer> rendererFromExtension = Iterables.tryFind(renderers,
            r -> Iterables.contains(r.getFileExtensions(), extension));
    if (rendererFromExtension.isPresent()) {
        renderer = rendererFromExtension.get();
    } else if (page.getMetadata().hasPath("renderer")) {
        final String rendererName = page.getMetadata().getString("renderer");
        renderer = Iterables.find(renderers, r -> rendererName.equals(r.getName()));
    } else {
        renderer = Iterables.find(renderers, r -> site.getDefaultRenderer().equals(r.getName()));
    }
    return renderer;
}

From source file:org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement.java

protected final <S extends DeclaredStatement<?>> S firstDeclared(final Class<S> type) {
    return type.cast(Iterables.find(substatements, Predicates.instanceOf(type)));
}

From source file:org.jclouds.cloudstack.suppliers.GetCurrentUser.java

@Override
public User get() {
    Iterable<User> users = Iterables.concat(client.getAccountClient().listAccounts());
    Predicate<User> apiKeyMatches = UserPredicates.apiKeyEquals(creds.get().identity);
    User currentUser = null;/*from w ww.java 2  s  .c o  m*/
    try {
        currentUser = Iterables.find(users, apiKeyMatches);
    } catch (NoSuchElementException e) {
        throw new NoSuchElementException(
                String.format("none of the following users match %s: %s", apiKeyMatches, users));
    }

    return currentUser;
}

From source file:com.android.build.gradle.internal.test.fixture.app.AbstractAndroidTestApp.java

@Override
public TestSourceFile getFile(String filename, final String path) {
    Collection<TestSourceFile> files = sourceFiles.get(filename);
    return Iterables.find(files, new Predicate<TestSourceFile>() {
        @Override/*w w  w.j av  a  2s  .co m*/
        public boolean apply(TestSourceFile testSourceFile) {
            return path.equals(testSourceFile.getPath());
        }
    });
}

From source file:com.atlassian.jira.rest.client.api.domain.EntityHelper.java

@SuppressWarnings("unused")
public static <T extends IdentifiableEntity<K>, K> T findEntityById(Iterable<T> entities, final K id) {
    try {/*from w w  w . j  av a2  s  . c o  m*/
        return Iterables.find(entities, HasIdPredicate.forId(id));
    } catch (NoSuchElementException ex) {
        throw new NoSuchElementException(
                String.format("Entity with id \"%s\" not found. Entities: %s", id, entities.toString()));
    }
}

From source file:org.jclouds.rimuhosting.miro.location.RimuHostingDefaultLocationSupplier.java

@Override
public Location get() {
    return Iterables.find(locations.get(), new Predicate<Location>() {

        @Override//w  w  w .  j a v  a 2 s .  c om
        public boolean apply(Location input) {
            return input.getId().equals(defaultDC);
        }

    });
}

From source file:org.onos.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement.java

protected final <S extends DeclaredStatement<?>> S firstDeclared(Class<S> type) {
    return type.cast(Iterables.find(substatements, Predicates.instanceOf(type)));
}

From source file:org.fusesource.ide.fabric8.ui.actions.FabricDetailsEditAction.java

protected void editCloud(final FabricDetails cloudDetails) {
    cloudDetails.flush();/*from   www  . j  a  v a2  s  . c om*/
    try {
        FabricDetails.reloadDetailList();
    } catch (Exception e) {
        FabricPlugin.getLogger().error(e);
    }

    // now lets select the one with this id
    Object found = Iterables.find(FabricDetails.getDetailList(), new Predicate<FabricDetails>() {

        @Override
        public boolean apply(FabricDetails details) {
            return Objects.equal(cloudDetails.getId(), details.getId());
        }
    });

    if (found instanceof FabricDetails) {
        onFabricDetailsEdited((FabricDetails) found);
    }
}