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:foo.domaintest.util.QueryStringHelper.java

public static Multimap<String, String> parseQuery(String query) {
    ImmutableListMultimap.Builder<String, String> params = new ImmutableListMultimap.Builder<>();
    for (String piece : Splitter.on('&').omitEmptyStrings().split(nullToEmpty(query))) {
        List<String> keyAndValue = Splitter.on('=').limit(2).splitToList(piece);
        try {/*  w w  w.j a v a  2 s  . c om*/
            params.put(URLDecoder.decode(keyAndValue.get(0), UTF_8.name()),
                    URLDecoder.decode(Iterables.get(keyAndValue, 1, ""), UTF_8.name()));
        } catch (UnsupportedEncodingException e) {
            throw new AssertionError(e);
        }
    }
    return params.build();
}

From source file:com.eucalyptus.cassandra.CassandraCluster.java

/**
 * We can start if we are the first seed, or if the first seed is already up.
 *//*  w ww .  j av a 2  s .co  m*/
public static boolean canStart() {
    boolean canStart = false;
    if (Hosts.hasCoordinator() && !Databases.isVolatile()) {
        final Set<ServiceConfiguration> configurations = seedOrderedConfigurations();
        if (!configurations.isEmpty()) {
            final ServiceConfiguration configuration = Iterables.get(configurations, 0, null);
            if (configuration != null && configuration.isHostLocal()) {
                canStart = true;
            } else if (configuration != null && Component.State.ENABLED.apply(configuration)
                    && Topology.isEnabled(Cassandra.class)) {
                canStart = true; //TODO:STEVE: should be one node at a time?
            }
        }
    }
    return canStart;
}

From source file:org.apache.james.jmap.utils.DownloadPath.java

public static DownloadPath from(String path) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(path), "'path' is mandatory");

    // path =  /blobId/name
    // idx  = 0 1      2
    List<String> pathVariables = Splitter.on('/').splitToList(path);
    Preconditions.checkArgument(pathVariables.size() >= 1 && pathVariables.size() <= 3,
            "'blobId' is mandatory");

    String blobId = Iterables.get(pathVariables, 1, null);
    Preconditions.checkArgument(!Strings.isNullOrEmpty(blobId), "'blobId' is mandatory");

    return new DownloadPath(blobId, name(pathVariables));
}

From source file:org.apache.james.jmap.utils.DownloadPath.java

private static Optional<String> name(List<String> pathVariables) {
    return Optional.ofNullable(Strings.emptyToNull(Iterables.get(pathVariables, 2, null)));
}

From source file:org.apache.james.jmap.model.AttachmentAccessToken.java

public static AttachmentAccessToken from(String serializedAttachmentAccessToken, String blobId) {
    Preconditions.checkArgument(!Strings.isNullOrEmpty(serializedAttachmentAccessToken),
            "'AttachmentAccessToken' is mandatory");
    List<String> split = Splitter.on(SEPARATOR).splitToList(serializedAttachmentAccessToken);
    Preconditions.checkArgument(split.size() == 3, "Wrong 'AttachmentAccessToken'");

    String defaultValue = null;/* w  w  w  . j av  a 2  s  . co  m*/
    return builder().blobId(blobId).username(Iterables.get(split, 0, defaultValue))
            .expirationDate(ZonedDateTime.parse(Iterables.get(split, 1, defaultValue)))
            .signature(Iterables.get(split, 2, defaultValue)).build();
}

From source file:com.totsp.tom.util.NumberStatistics.java

public BigDecimal medianAveraged() {
    int index = (inner.size() - 1) / 2;
    if (inner.size() % 2 == 1) {
        T low = Iterables.get(inner, index, null);
        return new BigDecimal(low.toString(), context);
    } else {/*from   w ww .j ava 2s  .c om*/
        int i = 0;
        T low = null;
        T high = null;
        for (T t : inner) {
            if (index == i) {
                low = t;
            } else if (index + 1 == i) {
                high = t;
                break;
            }
            i++;
        }
        return new BigDecimal(high.toString(), context).add(new BigDecimal(low.toString(), context))
                .divide(new BigDecimal(2, context), context);
    }

}

From source file:com.totsp.tom.util.Statistics.java

public T median() {
    int index = (inner.size() - 1) / 2;

    return Iterables.get(inner, index, null);
}

From source file:brooklyn.entity.monitoring.monit.MonitNodeImpl.java

@Override
protected void connectSensors() {
    super.connectSensors();
    Location machine = Iterables.get(getLocations(), 0, null);

    if (machine instanceof SshMachineLocation) {
        String cmd = getDriver().getStatusCmd();
        feed = SshFeed.builder().entity(this).period(Duration.FIVE_SECONDS)
                .machine((SshMachineLocation) machine)
                .poll(new SshPollConfig<Boolean>(SERVICE_UP).command(cmd).setOnSuccess(true)
                        .setOnFailureOrException(false))
                .poll(new SshPollConfig<String>(MONIT_TARGET_PROCESS_NAME).command(cmd)
                        .onSuccess(new Function<SshPollValue, String>() {
                            @Override
                            public String apply(SshPollValue input) {
                                String process = Strings.getFirstWordAfter(input.getStdout(), "Process");
                                return process;
                            }//from w w w.  ja  va2s .c o m
                        }).setOnFailureOrException(null))
                .poll(new SshPollConfig<String>(MONIT_TARGET_PROCESS_STATUS).command(cmd)
                        .onSuccess(new Function<SshPollValue, String>() {
                            @Override
                            public String apply(SshPollValue input) {
                                return Strings
                                        .trim(Strings.getRemainderOfLineAfter(input.getStdout(), "status"));
                            }
                        }).setOnFailureOrException(null))
                .build();
    } else {
        LOG.warn(
                "Location(s) {} not an ssh-machine location, so not polling for status; setting serviceUp immediately",
                getLocations());
        setAttribute(SERVICE_UP, true);
    }
}

From source file:org.apache.brooklyn.entity.monitoring.monit.MonitNodeImpl.java

@Override
protected void connectSensors() {
    super.connectSensors();
    Location machine = Iterables.get(getLocations(), 0, null);

    if (machine instanceof SshMachineLocation) {
        String cmd = getDriver().getStatusCmd();
        feed = SshFeed.builder().entity(this).period(Duration.FIVE_SECONDS)
                .machine((SshMachineLocation) machine)
                .poll(new SshPollConfig<Boolean>(SERVICE_UP).command(cmd).setOnSuccess(true)
                        .setOnFailureOrException(false))
                .poll(new SshPollConfig<String>(MONIT_TARGET_PROCESS_NAME).command(cmd)
                        .onSuccess(new Function<SshPollValue, String>() {
                            @Override
                            public String apply(SshPollValue input) {
                                String process = Strings.getFirstWordAfter(input.getStdout(), "Process");
                                return process;
                            }//www . j ava  2  s.  co  m
                        }).setOnFailureOrException(null))
                .poll(new SshPollConfig<String>(MONIT_TARGET_PROCESS_STATUS).command(cmd)
                        .onSuccess(new Function<SshPollValue, String>() {
                            @Override
                            public String apply(SshPollValue input) {
                                return Strings
                                        .trim(Strings.getRemainderOfLineAfter(input.getStdout(), "status"));
                            }
                        }).setOnFailureOrException(null))
                .build();
    } else {
        LOG.warn(
                "Location(s) {} not an ssh-machine location, so not polling for status; setting serviceUp immediately",
                getLocations());
        sensors().set(SERVICE_UP, true);
    }
}

From source file:org.spongepowered.common.registry.type.scoreboard.DisplaySlotRegistryModule.java

public Optional<DisplaySlot> getForIndex(int id) {
    return Optional.ofNullable(Iterables.get(this.displaySlotMappings.values(), id, null));
}