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

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

Introduction

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

Prototype

@Nullable
public static <T> T get(Iterable<? extends T> iterable, int position, @Nullable T defaultValue) 

Source Link

Document

Returns the element at the specified position in an iterable or a default value otherwise.

Usage

From source file:org.apache.metron.elasticsearch.client.ElasticsearchClient.java

/**
 * Gets ALL Elasticsearch indices, or null if status code returned is not OK 200.
 */// www  .j  a  v  a 2 s  . c om
public String[] getIndices() throws IOException {
    Response response = lowLevelClient.performRequest("GET", "/_cat/indices");
    if (response.getStatusLine().getStatusCode() == 200) {
        String responseStr = IOUtils.toString(response.getEntity().getContent());
        List<String> indices = new ArrayList<>();
        for (String line : Splitter.on("\n").split(responseStr)) {
            Iterable<String> splits = Splitter.on(" ").split(line.replaceAll("\\s+", " ").trim());
            if (Iterables.size(splits) > 3) {
                String index = Iterables.get(splits, 2, "");
                if (!StringUtils.isEmpty(index)) {
                    indices.add(index.trim());
                }
            }
        }
        String[] ret = new String[indices.size()];
        ret = indices.toArray(ret);
        return ret;
    }
    return null;
}

From source file:com.turn.ttorrent.tracker.simple.SimpleTrackerService.java

/**
 * Parse the query parameters using our defined BYTE_ENCODING.
 *
 * <p>/*from   www.  ja  v  a2s  .c  o m*/
 * Because we're expecting byte-encoded strings as query parameters, we
 * can't rely on SimpleHTTP's QueryParser which uses the wrong encoding for
 * the job and returns us unparsable byte data. We thus have to implement
 * our own little parsing method that uses BYTE_ENCODING to decode
 * parameters from the URI.
 * </p>
 *
 * <p>
 * <b>Note:</b> array parameters are not supported. If a key is present
 * multiple times in the URI, the latest value prevails. We don't really
 * need to implement this functionality as this never happens in the
 * Tracker HTTP protocol.
 * </p>
 *
 * @param request The Request object.
 * @return The {@link AnnounceRequestMessage} representing the client's
 * announce request.
 */
@Nonnull
@VisibleForTesting
/* pp */ static HTTPAnnounceRequestMessage parseRequest(Request request) throws MessageValidationException {
    String uri = request.getAddress().toString();
    Iterable<String> it = Splitter.on('?').limit(2).split(uri);
    String query = Iterables.get(it, 1, null);
    if (query == null)
        throw new MessageValidationException("No query string.");
    Multimap<String, String> params = TrackerUtils.parseQuery(query);
    return HTTPAnnounceRequestMessage.fromParams(params);
}

From source file:com.facebook.presto.orc.reader.StreamSources.java

@SuppressWarnings("ConstantConditions")
public static int getPosition(List<Integer> offsetPositions, int position) {
    return Iterables.get(offsetPositions, position, 0);
}

From source file:com.hubspot.jinjava.interpret.JinjavaInterpreter.java

private void resolveBlockStubs(OutputList output, Stack<String> blockNames) {
    for (BlockPlaceholderOutputNode blockPlaceholder : output.getBlocks()) {

        if (!blockNames.contains(blockPlaceholder.getBlockName())) {
            Collection<List<? extends Node>> blockChain = blocks.get(blockPlaceholder.getBlockName());
            List<? extends Node> block = Iterables.getFirst(blockChain, null);

            if (block != null) {
                List<? extends Node> superBlock = Iterables.get(blockChain, 1, null);
                context.setSuperBlock(superBlock);

                OutputList blockValueBuilder = new OutputList();

                for (Node child : block) {
                    blockValueBuilder.addNode(child.render(this));
                }//from   w ww .ja  v a  2 s . co m

                blockNames.push(blockPlaceholder.getBlockName());
                resolveBlockStubs(blockValueBuilder, blockNames);
                blockNames.pop();

                context.removeSuperBlock();

                blockPlaceholder.resolve(blockValueBuilder.getValue());
            }
        }

        if (!blockPlaceholder.isResolved()) {
            blockPlaceholder.resolve("");
        }
    }
}

From source file:com.facebook.buck.testutil.FakeProjectFilesystem.java

@Override
public Optional<String> readFirstLine(Path path) {
    List<String> lines;
    try {//from   www.  jav  a 2s .c o  m
        lines = readLines(path);
    } catch (IOException e) {
        return Optional.absent();
    }

    return Optional.fromNullable(Iterables.get(lines, 0, null));
}

From source file:foo.domaintest.http.HttpApiModule.java

private Map<String, String> parseMap(Collection<String> values) {
    ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>();
    for (String value : values) {
        List<String> parts = Splitter.on('=').limit(2).splitToList(value);
        // Note that this puts "" in the map if there's no '=' in the value.
        builder.put(parts.get(0), Iterables.get(parts, 1, ""));
    }/* ww w . j  a v  a2 s  .c  o  m*/
    return builder.build();
}