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

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

Introduction

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

Prototype

@Nullable
public static <T> T getFirst(Iterable<? extends T> iterable, @Nullable T defaultValue) 

Source Link

Document

Returns the first element in iterable or defaultValue if the iterable is empty.

Usage

From source file:com.basho.riak.spark.examples.SimpleJavaRiakTSExample.java

public static void main(String[] args) {
    SparkConf sparkConf = new SparkConf().setAppName("Simple Java Riak Demo");

    setSparkOpt(sparkConf, "spark.master", "local");
    setSparkOpt(sparkConf, "spark.riak.connection.host", "127.0.0.1:8087");

    long from = beginingOfQuantumMillis(Iterables.getFirst(DEMO_DATA, null).getTime());
    long to = endOfQuantumMillis(Iterables.getLast(DEMO_DATA).getTime());

    final JavaSparkContext jsc = new JavaSparkContext(sparkConf);

    loadDemoData(jsc);/*from  w w w . j  a v a2 s.co m*/

    RiakTSJavaRDD<Row> rdd = javaFunctions(jsc).riakTSBucket(TABLE_NAME, Row.class)
            .sql(String.format(
                    "SELECT * FROM %s WHERE time >= %d AND time <= %d  AND  weather = 'sunny' AND family = 'f'",
                    TABLE_NAME, from, to));

    rdd.foreach(x -> System.out.println(Joiner.on(",").join(x.getCellsCopy())));
}

From source file:sample.data.jpa.SampleDataJpaApplication.java

public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext context = SpringApplication.run(SampleDataJpaApplication.class, args);

    Iterable<ObjectMapper> objectMappers = context.getBeansOfType(ObjectMapper.class).values();

    objectMappers.forEach(mapper -> mapper.registerModule(new Hibernate5Module()));

    Squiggly.init(objectMappers, new RequestSquigglyContextProvider() {
        @Override/*from w  w w .  j  ava  2  s.c  o  m*/
        public void serializeAsIncludedField(Object pojo, JsonGenerator jgen, SerializerProvider provider,
                PropertyWriter writer) throws Exception {
            if (isFilteringEnabled()) {
                Object value = writer.getMember().getValue(pojo);

                if (value instanceof PersistentCollection) {
                    ((PersistentCollection) value).forceInitialization();
                }
            }

            super.serializeAsIncludedField(pojo, jgen, provider, writer);
        }

        @Override
        protected String customizeFilter(String filter, HttpServletRequest request, Class beanClass) {

            if (filter != null && Page.class.isAssignableFrom(beanClass)) {
                filter = "**,content[" + filter + "]";
            }

            return filter;
        }
    });

    ObjectMapper objectMapper = Iterables.getFirst(objectMappers, null);

    // Enable Squiggly for Jackson message converter
    if (objectMapper != null) {
        for (MappingJackson2HttpMessageConverter converter : context
                .getBeansOfType(MappingJackson2HttpMessageConverter.class).values()) {
            converter.setObjectMapper(objectMapper);
        }
    }
}

From source file:org.jclouds.examples.blobstore.basics.MainApp.java

public static void main(String[] args) throws IOException {

    if (args.length < PARAMETERS)
        throw new IllegalArgumentException(INVALID_SYNTAX);

    // Args/*w  ww . j  a  v a  2s . c o m*/

    String provider = args[0];

    // note that you can check if a provider is present ahead of time
    checkArgument(contains(allKeys, provider), "provider %s not in supported list: %s", provider, allKeys);

    String identity = args[1];
    String credential = args[2];
    String containerName = args[3];

    // Init
    BlobStoreContext context = ContextBuilder.newBuilder(provider).credentials(identity, credential)
            .buildView(BlobStoreContext.class);

    try {

        ApiMetadata apiMetadata = context.unwrap().getProviderMetadata().getApiMetadata();
        // Create Container
        BlobStore blobStore = context.getBlobStore();
        Location location = null;
        if (apiMetadata instanceof SwiftApiMetadata) {
            location = Iterables.getFirst(blobStore.listAssignableLocations(), null);
        }
        blobStore.createContainerInLocation(location, containerName);
        String blobName = "test";
        ByteSource payload = ByteSource.wrap("testdata".getBytes(Charsets.UTF_8));

        // List Container Metadata
        for (StorageMetadata resourceMd : blobStore.list()) {
            if (containerName.equals(resourceMd.getName())) {
                System.out.println(resourceMd);
            }
        }

        // Add Blob
        Blob blob = blobStore.blobBuilder(blobName).payload(payload).contentLength(payload.size()).build();
        blobStore.putBlob(containerName, blob);

        // Use Provider API
        Object object = null;
        if (apiMetadata instanceof S3ApiMetadata) {
            S3Client api = context.unwrapApi(S3Client.class);
            object = api.headObject(containerName, blobName);
        } else if (apiMetadata instanceof SwiftApiMetadata) {
            SwiftApi api = context.unwrapApi(SwiftApi.class);
            object = api.getObjectApi(location.getId(), containerName).getWithoutBody(blobName);
        } else if (apiMetadata instanceof AzureBlobApiMetadata) {
            AzureBlobClient api = context.unwrapApi(AzureBlobClient.class);
            object = api.getBlobProperties(containerName, blobName);
        } else if (apiMetadata instanceof AtmosApiMetadata) {
            AtmosClient api = context.unwrapApi(AtmosClient.class);
            object = api.headFile(containerName + "/" + blobName);
        } else if (apiMetadata instanceof GoogleCloudStorageApiMetadata) {
            GoogleCloudStorageApi api = context.unwrapApi(GoogleCloudStorageApi.class);
            object = api.getObjectApi().getObject(containerName, blobName);
        }
        if (object != null) {
            System.out.println(object);
        }

    } finally {
        // Close connecton
        context.close();
    }

}

From source file:be.ugent.psb.util.Sets.java

public static <E> E pop(Set<E> set) {
    E element = Iterables.getFirst(set, null);
    set.remove(element);
    return element;
}

From source file:tool.Lst.java

public static <T> T car(Iterable<T> lst, T nil) {
    return Iterables.getFirst(lst, nil);
}

From source file:com.google.gcloud.com.google.gcloud.spi.ServiceRpcProvider.java

public static DatastoreRpc datastore(DatastoreServiceOptions options) {
    DatastoreRpcFactory factory = Iterables.getFirst(ServiceLoader.load(DatastoreRpcFactory.class), null);
    return factory == null ? new DefaultDatastoreRpc(options) : factory.create(options);
}

From source file:org.opendaylight.mdsal.binding.generator.impl.YangTextTemplate.java

static String formatSchemaPath(final String moduleName, final Iterable<QName> schemaPath) {
    final StringBuilder sb = new StringBuilder();
    sb.append(moduleName);/* w  ww  .  jav  a  2s. c o  m*/

    QName currentElement = Iterables.getFirst(schemaPath, null);
    for (QName pathElement : schemaPath) {
        sb.append('/');
        if (!currentElement.getNamespace().equals(pathElement.getNamespace())) {
            currentElement = pathElement;
            sb.append(pathElement);
        } else {
            sb.append(pathElement.getLocalName());
        }
    }
    return sb.toString();
}

From source file:ezbake.thrift.authentication.X509Utils.java

public static String getCn(LdapName name) {
    return Iterables.getFirst(getValueOfType(name, DnFields.CN), "");
}

From source file:com.google.gcloud.com.google.gcloud.spi.ServiceRpcProvider.java

public static StorageRpc storage(StorageServiceOptions options) {
    StorageRpcFactory factory = Iterables.getFirst(ServiceLoader.load(StorageRpcFactory.class), null);
    return factory == null ? new DefaultStorageRpc(options) : factory.create(options);
}

From source file:minium.internal.Paths.java

public static URL toURL(String urlPath) {
    Preconditions.checkArgument(!urlPath.startsWith(CLASSPATH_WILDCARD_PROTOCOL),
            "path starts with classpath*, use toURLs instead");
    return Iterables.getFirst(toURLs(urlPath), null);
}