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

@Nullable
public static <T> T find(Iterable<? extends T> iterable, Predicate<? super T> predicate,
        @Nullable T defaultValue) 

Source Link

Document

Returns the first element in iterable that satisfies the given predicate, or defaultValue if none found.

Usage

From source file:org.eclipse.emf.compare.uml2.internal.postprocessor.extension.stereotype.UMLStereotypeAttributeChangeFactory.java

/**
 * {@inheritDoc}// www.  ja v a  2 s .  co  m
 * 
 * @see org.eclipse.emf.compare.uml2.internal.postprocessor.AbstractUMLChangeFactory#getDiscriminant(org.eclipse
 *      .emf.compare.Diff)
 */
@Override
protected EObject getDiscriminant(Diff input) {
    final EObject discriminant;
    if (input instanceof AttributeChange) {
        discriminant = ((AttributeChange) input).getAttribute();
    } else {
        discriminant = Iterables.find(getDiscriminants(input), instanceOf(EObject.class), null);
    }
    return discriminant;
}

From source file:org.eclipse.emf.compare.uml2.internal.postprocessor.extension.stereotype.UMLStereotypeReferenceChangeFactory.java

/**
 * {@inheritDoc}//from  ww  w .  j av a  2 s.com
 * 
 * @see org.eclipse.emf.compare.uml2.internal.postprocessor.AbstractUMLChangeFactory#getDiscriminant(org.eclipse
 *      .emf.compare.Diff)
 */
@Override
protected EObject getDiscriminant(Diff input) {
    final EObject discriminant;
    if (input instanceof ReferenceChange) {
        discriminant = ((ReferenceChange) input).getReference();
    } else {
        discriminant = Iterables.find(getDiscriminants(input), instanceOf(EObject.class), null);
    }
    return discriminant;
}

From source file:org.eclipse.emf.compare.uml2.internal.postprocessor.extension.stereotype.UMLStereotypeApplicationChangeFactory.java

/**
 * {@inheritDoc}/*from  ww w  . ja  va  2  s . co m*/
 * 
 * @see org.eclipse.emf.compare.uml2.internal.postprocessor.AbstractUMLChangeFactory#getDiscriminant(org.eclipse.emf.compare.Diff)
 */
@Override
protected EObject getDiscriminant(Diff input) {
    return Iterables.find(getDiscriminants(input), instanceOf(EObject.class), null);
}

From source file:org.onebusaway.nyc.vehicle_tracking.impl.inference.ObservationCache.java

@SuppressWarnings("unchecked")
public <T> T getValueForObservation(Observation observation, EObservationCacheKey key) {
    final NycRawLocationRecord record = observation.getRecord();
    final MinMaxPriorityQueue<ObservationContents> contentsCache = _contentsByVehicleId
            .getUnchecked(record.getVehicleId());
    /*//  w  ww  .  j a va2 s .c  om
     * Synchronize for integration tests, which can do crazy things.
     */
    final ObservationContents contents;
    synchronized (contentsCache) {
        contents = Iterables.find(contentsCache, new ObsSearch(observation), null);

        if (contents == null) {
            /*
             *  Add this new observation content, which, if it's truly a new obs, 
             *  will be added later
             */
            if (!contentsCache.offer(new ObservationContents(observation))) {
                return null;
            }
            return null;
        }
    }

    return (T) contents.getValueForValueType(key);
}

From source file:com.demigodsrpg.chitchat.bungee.BungeeChitchat.java

@EventHandler(priority = EventPriority.HIGHEST)
public void onMessage(final PluginMessageEvent event) {
    if (event.getTag().equals("BungeeCord")) {
        // Get origin server
        ServerInfo origin = Iterables.find(getProxy().getServers().values(), new Predicate<ServerInfo>() {
            @Override/* w  w  w . ja v a 2s  .  c om*/
            public boolean apply(ServerInfo serverInfo) {
                return serverInfo.getAddress().equals(event.getSender().getAddress());
            }
        }, null);

        // If origin doesn't exist (impossible?) throw an exception
        if (origin == null) {
            throw new NullPointerException("Origin server for a chat message was null.");
        }

        // Get the data
        ByteArrayDataInput in = ByteStreams.newDataInput(event.getData());

        // Needed for it to work because whynot
        String messageType = in.readUTF();
        String targetServer = in.readUTF();

        // Ignore messages that aren't the correct type
        if (!"Forward".equals(messageType)) {
            return;
        }

        // Get the chat channel and speaking player
        String chatChannelRaw = in.readUTF();

        // Don't continue if this message isn't from chitchat
        if (!chatChannelRaw.startsWith("chitchat")) {
            return;
        }

        // Grab the speaking player and clean up the chat channel name
        String speakingPlayerName = chatChannelRaw.split("\\$")[1];
        String chatChannel = chatChannelRaw.split("\\$")[2];

        // Is this a mute update?
        if (chatChannelRaw.startsWith("chatchatmute$")) {
            MUTED_PLAYERS.put(chatChannel, speakingPlayerName.toLowerCase());
            return;
        }

        // Is this an unmute update?
        if (chatChannelRaw.startsWith("chatchatunmute$")) {
            MUTED_PLAYERS.remove(chatChannel, speakingPlayerName.toLowerCase());
            return;
        }

        // Ignore muted players
        if (MUTED_PLAYERS.containsEntry(chatChannel, speakingPlayerName.toLowerCase())) {
            return;
        }

        // Is this a valid chat channel?
        if (!CHAT_CHANNELS.containsEntry(chatChannel, origin.getName())) {
            CHAT_CHANNELS.put(chatChannel, origin.getName());
        }

        // Process the data
        short len = in.readShort();
        byte[] msgbytes = new byte[len];
        in.readFully(msgbytes);

        DataInputStream msgin = new DataInputStream(new ByteArrayInputStream(msgbytes));

        try {
            // Finally get the message
            String message = msgin.readUTF();

            // Send the message to appropriate servers
            for (ServerInfo server : getProxy().getServers().values()) {
                // Continue from invalid targets
                if (origin.getName().equals(server.getName())
                        || !CHAT_CHANNELS.containsEntry(chatChannel, server.getName())) {
                    continue;
                }

                // Send the message to the appropriate players
                for (ProxiedPlayer player : server.getPlayers()) {
                    player.sendMessage(message);
                }
            }
        } catch (IOException oops) {
            oops.printStackTrace();
        }
    }
}

From source file:org.jclouds.vagrant.internal.VagrantCliFacade.java

@Override
public Box getBox(final String boxName) {
    return Iterables.find(listBoxes(), new Predicate<Box>() {
        @Override/*from   ww  w.ja va2  s. co m*/
        public boolean apply(Box input) {
            return boxName.equals(input.getName());
        }
    }, null);
}

From source file:com.xebialabs.overthere.cifs.PathMapper.java

/**
 * Attempts to use provided path-to-share mappings to convert the given local path to a remotely accessible path,
 * using the longest matching prefix if available.
 * <p/>/*from ww w  . j  ava 2s. c  om*/
 * Falls back to using administrative shares if none of the explicit mappings applies to the path to convert.
 *
 * @param path the local path to convert
 * @return the remotely accessible path (using shares) at which the local path can be accessed using SMB
 */
@VisibleForTesting
String toSharedPath(String path) {
    final String lowerCasePath = path.toLowerCase();
    // assumes correct format drive: or drive:\path
    String mappedPathPrefix = Iterables.find(sharesForPaths.keySet(), new Predicate<String>() {
        @Override
        public boolean apply(String input) {
            return lowerCasePath.startsWith(input);
        }
    }, null);
    // the share + the remainder of the path if found, otherwise the path with ':' replaced by '$'
    return ((mappedPathPrefix != null)
            ? sharesForPaths.get(mappedPathPrefix) + path.substring(mappedPathPrefix.length())
            : path.substring(0, 1) + ADMIN_SHARE_DESIGNATOR + path.substring(2));
}

From source file:org.sonar.server.plugins.ws.InstallAction.java

private PluginUpdate findAvailablePluginByKey(String key) {
    PluginUpdate pluginUpdate = MISSING_PLUGIN;

    Optional<UpdateCenter> updateCenter = updateCenterFactory.getUpdateCenter(false);
    if (updateCenter.isPresent()) {
        pluginUpdate = Iterables.find(updateCenter.get().findAvailablePlugins(), hasKey(key), MISSING_PLUGIN);
    }//from ww w  .  jav a  2 s .co  m

    if (pluginUpdate == MISSING_PLUGIN) {
        throw new IllegalArgumentException(format(
                "No plugin with key '%s' or plugin '%s' is already installed in latest version", key, key));
    }

    return pluginUpdate;
}

From source file:org.sonar.core.test.DefaultTestable.java

public CoverageBlock coverageBlock(final TestCase testCase) {
    return Iterables.find(getEdges(DefaultCoverageBlock.class, Direction.IN, "covers"),
            new Predicate<CoverageBlock>() {
                public boolean apply(CoverageBlock input) {
                    return input.testCase().name().equals(testCase.name());
                }//  w w w .  j a v  a2  s  . c  o  m
            }, null);
}

From source file:org.eclipse.emf.compare.uml2.internal.postprocessor.util.UMLCompareUtil.java

/**
 * From the given EReference, it returns the list of EReference which are superset.
 * //from   w ww .j a v a  2  s .c  om
 * @param reference
 *            The EReference subset from which is requested the superset.
 * @return The list of EReference superset.
 */
private static Iterable<EReference> getSupersetReferences(EReference reference) {
    EAnnotation subsetsAnnotation = Iterables.find(reference.getEAnnotations(),
            UMLUtilForCompare.isSubsetsAnnotation(), null);
    if (subsetsAnnotation != null) {
        return Iterables.filter(subsetsAnnotation.getReferences(), EReference.class);
    }
    return Collections.emptyList();
}