Example usage for com.google.common.collect ImmutableList builder

List of usage examples for com.google.common.collect ImmutableList builder

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableList builder.

Prototype

public static <E> Builder<E> builder() 

Source Link

Usage

From source file:com.palantir.typescript.services.language.ScriptElementKindModifier.java

public static ImmutableList<ScriptElementKindModifier> parseList(String kindModifiers) {
    ImmutableList.Builder<ScriptElementKindModifier> kindModifiersBuilder = ImmutableList.builder();

    if (kindModifiers.length() > 0) {
        for (String kindModifier : Splitter.on(',').split(kindModifiers)) {
            kindModifiersBuilder.add(fromString(kindModifier));
        }/*from   w ww.  ja  va  2s  .  c om*/
    }

    return kindModifiersBuilder.build();
}

From source file:io.prestosql.orc.OrcDataSourceUtils.java

/**
 * Merge disk ranges that are closer than {@code maxMergeDistance}.
 *//*w w  w  .  j a v  a2  s.c o  m*/
public static List<DiskRange> mergeAdjacentDiskRanges(Collection<DiskRange> diskRanges,
        DataSize maxMergeDistance, DataSize maxReadSize) {
    // sort ranges by start offset
    List<DiskRange> ranges = new ArrayList<>(diskRanges);
    ranges.sort(comparingLong(DiskRange::getOffset));

    // merge overlapping ranges
    long maxReadSizeBytes = maxReadSize.toBytes();
    long maxMergeDistanceBytes = maxMergeDistance.toBytes();
    ImmutableList.Builder<DiskRange> result = ImmutableList.builder();
    DiskRange last = ranges.get(0);
    for (int i = 1; i < ranges.size(); i++) {
        DiskRange current = ranges.get(i);
        DiskRange merged = last.span(current);
        if (merged.getLength() <= maxReadSizeBytes
                && last.getEnd() + maxMergeDistanceBytes >= current.getOffset()) {
            last = merged;
        } else {
            result.add(last);
            last = current;
        }
    }
    result.add(last);

    return result.build();
}

From source file:com.google.template.soy.msgs.SoyMsgIdConverter.java

/**
 * Converts received message ID strings, as rendered by the Soy <code>msgId</code> function, into
 * a list of long message IDs./*from  w  ww.  ja va2s . c  om*/
 *
 * <p>The message IDs are generated by Soy as Base64 web safe ("base64url")-encoded int64s.
 *
 * <p>For example, "URTo5tnzILU=" = [0x51 0x14 0xe8 0xe6 0xd9 0xf3 0x20 0xb5] = 0x5114e8e6d9f320b5
 * = 5842550694803087541.
 *
 * @throws IllegalArgumentException if any of the strings fails to decode into message IDs, is too
 *     long or too short.
 */
public static ImmutableList<Long> convertSoyMessageIdStrings(Iterable<String> encodedIds) {
    ImmutableList.Builder<Long> messageIds = ImmutableList.builder();
    for (String encodedId : encodedIds) {
        byte[] decodedBytes = BaseEncoding.base64Url().decode(encodedId);
        if (decodedBytes.length != Long.BYTES) {
            throw new IllegalArgumentException(
                    String.format("The message ID to decode ('%s') was of invalid size (%d != %d)", encodedId,
                            decodedBytes.length, Long.BYTES));
        }
        messageIds.add(Longs.fromByteArray(decodedBytes));
    }
    return messageIds.build();
}

From source file:com.facebook.buck.zip.Unzip.java

/**
 * Unzips a file to a destination and returns the paths of the written files.
 *///from  w ww .j a  v  a  2s.com
public static ImmutableList<Path> extractZipFile(String zipFile, String destination,
        boolean overwriteExistingFiles) throws IOException {
    // Create output directory if it does not exist
    File folder = new File(destination);
    // TODO(mbolin): UnzipStep could be a CompositeStep with a MakeCleanDirectoryStep for the output
    // dir.
    Files.createDirectories(folder.toPath());

    ImmutableList.Builder<Path> filesWritten = ImmutableList.builder();
    try (ZipInputStream zip = new ZipInputStream(new FileInputStream(zipFile))) {
        for (ZipEntry entry = zip.getNextEntry(); entry != null; entry = zip.getNextEntry()) {
            String fileName = entry.getName();
            File target = new File(folder, fileName);
            if (target.exists() && !overwriteExistingFiles) {
                continue;
            }

            // TODO(mbolin): Keep track of which directories have already been written to avoid
            // making unnecessary Files.createDirectories() calls. In practice, a single zip file will
            // have many entries in the same directory.

            if (entry.isDirectory()) {
                // Create the directory and all its parent directories
                Files.createDirectories(target.toPath());
            } else {
                // Create parent folder
                Files.createDirectories(target.toPath().getParent());

                filesWritten.add(target.toPath());
                // Write file
                try (FileOutputStream out = new FileOutputStream(target)) {
                    ByteStreams.copy(zip, out);
                }
            }
        }
    }
    return filesWritten.build();
}

From source file:org.basepom.mojo.propertyhelper.MacroField.java

public static List<MacroField> createMacros(final ValueCache valueCache,
        final MacroDefinition[] macroDefinitions, final AbstractPropertyHelperMojo mojo) throws IOException {
    checkNotNull(valueCache, "valueCache is null");
    checkNotNull(macroDefinitions, "macroDefinitions is null");
    checkNotNull(mojo, "mojo is null");

    final ImmutableList.Builder<MacroField> result = ImmutableList.builder();

    for (MacroDefinition macroDefinition : macroDefinitions) {
        macroDefinition.check();//  ww w. j  av a2 s.c o m
        final ValueProvider macroValue = valueCache.getValueProvider(macroDefinition);
        final MacroField macroField = new MacroField(macroDefinition, macroValue, mojo);
        result.add(macroField);
    }

    return result.build();
}

From source file:com.google.template.soy.exprtree.ExprRootNode.java

public static List<ExprRootNode> wrap(Iterable<? extends ExprNode> exprs) {
    ImmutableList.Builder<ExprRootNode> builder = ImmutableList.builder();
    for (ExprNode expr : exprs) {
        builder.add(new ExprRootNode(expr));
    }//w w w .ja v  a  2  s  .  c o  m
    return builder.build();
}

From source file:com.google.devtools.build.docgen.skylark.SkylarkDocUtils.java

/**
 * Returns a list of parameter documentation elements for a given method doc and the method's
 * parameters.//  ww w  . j a va  2  s . c  om
 */
static ImmutableList<SkylarkParamDoc> determineParams(SkylarkMethodDoc methodDoc, Param[] userSuppliedParams,
        Param extraPositionals, Param extraKeywords) {
    ImmutableList.Builder<SkylarkParamDoc> paramsBuilder = ImmutableList.builder();
    for (Param param : userSuppliedParams) {
        paramsBuilder.add(new SkylarkParamDoc(methodDoc, param));
    }
    if (!extraPositionals.name().isEmpty()) {
        paramsBuilder.add(new SkylarkParamDoc(methodDoc, extraPositionals));
    }
    if (!extraKeywords.name().isEmpty()) {
        paramsBuilder.add(new SkylarkParamDoc(methodDoc, extraKeywords));
    }
    return paramsBuilder.build();
}

From source file:net.techcable.spawnshield.Utils.java

public static <T> Collector<T, ?, ImmutableList<T>> immutableListCollector() {
    return Collector.<T, ImmutableList.Builder<T>, ImmutableList<T>>of(ImmutableList::builder,
            ImmutableList.Builder::add, (builder1, builder2) -> builder1.addAll(builder2.build()),
            ImmutableList.Builder::build);
}

From source file:org.hawkular.client.test.utils.AvailabilityDataGenerator.java

/**
 * Generate Availability data from a list of known states
 * @param states/*w ww.j  a  v  a2 s  . c o m*/
 * @return List of data points
 */
public static List<AvailabilityDataPoint> gen(AvailabilityType... states) {
    BTG ts = new BTG();
    Builder<AvailabilityDataPoint> builder = new ImmutableList.Builder<>();
    for (int i = 0; i < states.length; i++) {
        DataPoint<AvailabilityType> point = new DataPoint<>(ts.nextMilli(), states[i]);
        builder.add(new AvailabilityDataPoint(point));
    }
    return builder.build();
}

From source file:com.proofpoint.jaxrs.BeanValidationException.java

private static List<String> messagesFor(Set<ConstraintViolation<Object>> violations) {
    ImmutableList.Builder<String> messages = new ImmutableList.Builder<>();
    for (ConstraintViolation<?> violation : violations) {
        messages.add(violation.getPropertyPath().toString() + " " + violation.getMessage());
    }// ww w.  j  a  va 2s. c  o m

    return messages.build();
}