Example usage for com.google.common.collect Lists newArrayListWithCapacity

List of usage examples for com.google.common.collect Lists newArrayListWithCapacity

Introduction

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

Prototype

@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayListWithCapacity(int initialArraySize) 

Source Link

Document

Creates an ArrayList instance backed by an array with the specified initial size; simply delegates to ArrayList#ArrayList(int) .

Usage

From source file:org.terasology.entitySystem.metadata.core.EnumTypeHandler.java

public List<T> deserializeList(EntityData.Value value) {
    List<T> result = Lists.newArrayListWithCapacity(value.getStringCount());
    for (String item : value.getStringList()) {
        T enumItem = caseInsensitiveLookup.get(item.toLowerCase(Locale.ENGLISH));
        if (enumItem == null) {
            logger.warn("Unknown enum value: '{}' for enum {}", item, enumType.getSimpleName());
        } else {//from   www .j  av  a  2  s.c  o m
            result.add(enumItem);
        }
    }
    return result;
}

From source file:com.programmablefun.ide.CodeRepository.java

public CodeRepository(PersistenceStrategy persistenceStrategy) {
    this.persistenceStrategy = persistenceStrategy;
    syncerThread = new Thread(() -> {
        for (;;) {
            List<byte[]> buf = Lists.newArrayListWithCapacity(1024);
            outstanding.drainTo(buf);/*from   w  w  w  .j  av  a 2  s.  c  om*/
            if (buf.size() > 0) {
                byte[] lastData = buf.get(buf.size() - 1);
                try {
                    this.persistenceStrategy.save(lastData);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    syncerThread.setDaemon(true);
    syncerThread.start();
}

From source file:net.devh.boot.grpc.server.service.AnnotationGrpcServiceDiscoverer.java

@Override
public Collection<GrpcServiceDefinition> findGrpcServices() {
    Collection<String> beanNames = Arrays
            .asList(this.applicationContext.getBeanNamesForAnnotation(GrpcService.class));
    List<GrpcServiceDefinition> definitions = Lists.newArrayListWithCapacity(beanNames.size());
    GlobalServerInterceptorRegistry globalServerInterceptorRegistry = applicationContext
            .getBean(GlobalServerInterceptorRegistry.class);
    List<ServerInterceptor> globalInterceptorList = globalServerInterceptorRegistry.getServerInterceptors();
    for (String beanName : beanNames) {
        BindableService bindableService = this.applicationContext.getBean(beanName, BindableService.class);
        ServerServiceDefinition serviceDefinition = bindableService.bindService();
        GrpcService grpcServiceAnnotation = applicationContext.findAnnotationOnBean(beanName,
                GrpcService.class);
        serviceDefinition = bindInterceptors(serviceDefinition, grpcServiceAnnotation, globalInterceptorList);
        definitions.add(new GrpcServiceDefinition(beanName, bindableService.getClass(), serviceDefinition));
        log.debug("Found gRPC service: " + serviceDefinition.getServiceDescriptor().getName() + ", bean: "
                + beanName + ", class: " + bindableService.getClass().getName());
    }//from  ww  w.  java  2  s.  com
    return definitions;
}

From source file:com.cloudera.oryx.als.serving.MultiRescorerProvider.java

@Override
public Rescorer getRecommendToAnonymousRescorer(String[] itemIDs, OryxRecommender recommender, String... args) {
    List<Rescorer> rescorers = Lists.newArrayListWithCapacity(providers.length);
    for (RescorerProvider provider : providers) {
        Rescorer rescorer = provider.getRecommendToAnonymousRescorer(itemIDs, recommender, args);
        if (rescorer != null) {
            rescorers.add(rescorer);//  w ww .  ja  v  a 2  s. com
        }
    }
    return buildRescorer(rescorers);
}

From source file:org.summer.dsl.xbase.scoping.batch.FeatureScopeSessionWithStaticTypes.java

protected List<TypeBucket> concatTypeBuckets(List<? extends JvmType> types, List<TypeBucket> parentResult) {
    if (types.isEmpty()) {
        return parentResult;
    }// w w w  . j a v a2  s.c o  m
    List<TypeBucket> result = Lists.newArrayListWithCapacity(3);
    result.add(new TypeBucket(getId(), types, resolvedFeaturesProvider));
    result.addAll(parentResult);
    return result;
}

From source file:com.oculusinfo.binning.io.PyramidIOFactory.java

private static List<String> getPyramidTypes(List<ConfigurableFactory<?>> childFactories) {
    List<String> pyramidTypes = Lists.newArrayListWithCapacity(childFactories.size());

    //add any child factories
    if (childFactories != null) {
        for (ConfigurableFactory<?> factory : childFactories) {
            String factoryName = factory.getName();
            if (factoryName != null) {
                pyramidTypes.add(factoryName);
            }/*from w  w  w . ja  v  a  2 s  . c om*/
        }
    }

    return pyramidTypes;
}

From source file:com.github.explainable.util.RandomSampler.java

/**
 * Obtain a uniform random sample of the elements in the population. The order of elements in the
 * output list is also uniformly random.
 *
 * @param population the population from which a sample is to be drawn
 * @param n the sample size//w w  w  . j  a  v a  2 s.c  o  m
 * @return a sample of size {@code n} drawn from {@code population} uniformly at random
 */
public <T> List<T> sample(List<T> population, int n) {
    Preconditions.checkNotNull(population);
    Preconditions.checkArgument(n <= population.size());

    boolean[] selected = new boolean[population.size()];

    List<T> result = Lists.newArrayListWithCapacity(n);
    for (int i = 0; i < n; i++) {
        int j = random.nextInt(population.size());
        while (selected[j]) {
            j = random.nextInt(population.size());
        }

        selected[j] = true;
        result.add(population.get(j));
    }

    return result;
}

From source file:com.android.build.gradle.internal.dsl.NdkOptions.java

@NonNull
public NdkOptions ldLibs(String... libs) {
    if (ldLibs == null) {
        ldLibs = Lists.newArrayListWithCapacity(libs.length);
    }//from   w  w  w.j  a va  2  s  . c o m
    Collections.addAll(ldLibs, libs);
    return this;
}

From source file:org.terasology.persistence.typeSerialization.typeHandlers.core.EnumTypeHandler.java

@Override
public List<T> deserializeCollection(EntityData.Value value) {
    List<T> result = Lists.newArrayListWithCapacity(value.getStringCount());
    for (String item : value.getStringList()) {
        T enumItem = caseInsensitiveLookup.get(item.toLowerCase(Locale.ENGLISH));
        if (enumItem == null) {
            logger.warn("Unknown enum value: '{}' for enum {}", item, enumType.getSimpleName());
        } else {/*from w  w w .ja  va 2s . c  o m*/
            result.add(enumItem);
        }
    }
    return result;
}

From source file:com.pinterest.terrapin.server.TerrapinServerInternalImpl.java

@Override
public Future<TerrapinResponse> get(TerrapinInternalGetRequest request) {
    final TerrapinResponse response = new TerrapinResponse().setResponseMap((Map) Maps.newHashMap());
    List<Future<Map<ByteBuffer, Pair<ByteBuffer, Throwable>>>> readFutures = Lists
            .newArrayListWithCapacity(request.getKeyListSize());
    // Go through all the keys and retrieve the corresponding Reader object.
    String resource = null;//  w w  w.j  a  v  a  2 s .  c o  m
    for (MultiKey multiKey : request.getKeyList()) {
        Reader r = null;
        try {
            String keyResource = multiKey.getResource();
            if (resource != null) {
                if (!resource.equals(keyResource)) {
                    return Future.exception(new TerrapinGetException("Multiple resources within same get call.",
                            TerrapinGetErrorCode.INVALID_REQUEST));
                }
            } else {
                resource = keyResource;
            }
            r = this.resourcePartitionMap.getReader(keyResource, multiKey.getPartition());
        } catch (TerrapinGetException e) {
            for (ByteBuffer key : multiKey.getKey()) {
                response.getResponseMap().put(key, new TerrapinSingleResponse().setErrorCode(e.getErrorCode()));
            }
            continue;
        }
        // If we found a reader, issue reads on it.
        try {
            readFutures.add(r.getValues(multiKey.getKey()));
        } catch (Throwable t) {
            t.printStackTrace();
            // The entire batch failed.
            for (ByteBuffer key : multiKey.getKey()) {
                response.getResponseMap().put(key,
                        new TerrapinSingleResponse().setErrorCode(TerrapinGetErrorCode.READ_ERROR));
            }
        }
    }
    // Wait for all the futures to finish and return the response.
    return Future.collect(readFutures)
            .map(new Function<List<Map<ByteBuffer, Pair<ByteBuffer, Throwable>>>, TerrapinResponse>() {
                @Override
                public TerrapinResponse apply(List<Map<ByteBuffer, Pair<ByteBuffer, Throwable>>> mapList) {
                    for (Map<ByteBuffer, Pair<ByteBuffer, Throwable>> map : mapList) {
                        for (Map.Entry<ByteBuffer, Pair<ByteBuffer, Throwable>> entry : map.entrySet()) {
                            if (entry.getValue().getRight() != null) {
                                response.getResponseMap().put(entry.getKey(), new TerrapinSingleResponse()
                                        .setErrorCode(TerrapinGetErrorCode.READ_ERROR));
                            } else {
                                response.getResponseMap().put(entry.getKey(),
                                        new TerrapinSingleResponse().setValue(entry.getValue().getLeft()));
                            }
                        }
                    }
                    return response;
                }
            });
}