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

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

Introduction

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

Prototype

boolean isEmpty();

Source Link

Document

Returns true if this list contains no elements.

Usage

From source file:ratpack.health.HealthCheck.java

/**
 * Execute health checks./*w  w w.  j a  v  a 2 s  . c o  m*/
 * <p>
 * The checks will be executed in parallel.
 *
 * @param registry the registry to pass to each health check
 * @param throttle the throttle to use to constrain the parallelism of the checks
 * @param healthChecks the health checks to execute
 * @return the results
 * @since 1.5
 */
static Promise<HealthCheckResults> checkAll(Registry registry, Throttle throttle,
        Iterable<? extends HealthCheck> healthChecks) {
    return Promise.flatten(() -> {
        ImmutableList<? extends HealthCheck> healthChecksCopy = ImmutableList.copyOf(healthChecks);
        if (healthChecksCopy.isEmpty()) {
            return Promise.value(HealthCheckResults.empty());
        }

        Iterable<Promise<Pair<Result, String>>> resultPromises = Iterables.transform(healthChecksCopy,
                h -> Promise.flatten(() -> h.check(registry)).throttled(throttle)
                        .mapError(HealthCheck.Result::unhealthy).right(r -> h.getName()));

        Map<String, Result> results = new ConcurrentHashMap<>();
        return ParallelBatch.of(resultPromises).forEach((i, result) -> results.put(result.right, result.left))
                .map(() -> new HealthCheckResults(ImmutableSortedMap.copyOf(results)));
    });
}

From source file:com.opengamma.strata.collect.io.CsvFile.java

private static ArrayList<ImmutableList<String>> parseAll(List<String> lines, char separator) {
    ArrayList<ImmutableList<String>> parsedLines = new ArrayList<>();
    for (String line : lines) {
        ImmutableList<String> parsed = parseLine(line, separator);
        if (!parsed.isEmpty()) {
            parsedLines.add(parsed);// ww  w.j  a v  a  2 s.  c o  m
        }
    }
    return parsedLines;
}

From source file:com.facebook.presto.metadata.FunctionListBuilder.java

private static List<Class<?>> getParameterTypes(Class<?>... types) {
    ImmutableList<Class<?>> parameterTypes = ImmutableList.copyOf(types);
    if (!parameterTypes.isEmpty() && parameterTypes.get(0) == ConnectorSession.class) {
        parameterTypes = parameterTypes.subList(1, parameterTypes.size());
    }//w  w  w. j  a va  2 s.c om
    return parameterTypes;
}

From source file:de.flapdoodle.apache.pivot.skin.WeightedSizes.java

private static <T> ImmutableList<ComponentAndSize<T>> resized(ImmutableList<ComponentSizeAndWeight<T>> sizes,
        int sizeOfAll) {

    int weightOfAll = weights(sizes);
    ImmutableList<ComponentAndSize<T>> weighted = weighted(sizes, sizeOfAll, weightOfAll);
    ImmutableList<ComponentAndSize<T>> toBig = toBig(weighted);

    if (!toBig.isEmpty()) {
        ImmutableList<ComponentSizeAndWeight<T>> left = withoutMatching(sizes, components(toBig));

        ImmutableList<ComponentAndSize<T>> maxOfToBig = sizeFixedToLimit(toBig);
        int sizeOfToBig = sumOrMax(maxOfToBig);

        Builder<ComponentAndSize<T>> builder = ImmutableList.<ComponentAndSize<T>>builder();
        builder.addAll(maxOfToBig);/*from ww w. j a v  a2 s.c  o  m*/
        builder.addAll(resized(left, sizeOfAll - sizeOfToBig));
        return builder.build();
    } else {
        ImmutableList<ComponentAndSize<T>> toSmall = toSmall(weighted);
        if (!toSmall.isEmpty()) {
            ImmutableList<ComponentSizeAndWeight<T>> left = withoutMatching(sizes, components(toSmall));

            ImmutableList<ComponentAndSize<T>> maxOfToSmall = sizeFixedToLimit(toSmall);
            int sizeOfToBig = sumOrMax(maxOfToSmall);

            Builder<ComponentAndSize<T>> builder = ImmutableList.<ComponentAndSize<T>>builder();
            builder.addAll(maxOfToSmall);
            builder.addAll(resized(left, sizeOfAll - sizeOfToBig));
            return builder.build();
        }
    }
    return weighted;
}

From source file:com.facebook.buck.cxx.Depfiles.java

/**
 * Parses the input as a .d Makefile as emitted by {@code gcc -MD}
 * and returns the (target, [dep, dep2, ...]) inside.
 *//*from  ww w. ja v a  2s.  c  o m*/
public static Depfile parseDepfile(Readable readable) throws IOException {
    String target = null;
    ImmutableList.Builder<String> prereqsBuilder = ImmutableList.builder();
    State state = State.LOOKING_FOR_TARGET;
    StringBuilder identifierBuilder = new StringBuilder();

    CharBuffer buffer = CharBuffer.allocate(4096);
    int numBackslashes = 0;

    while (readable.read(buffer) != -1) {
        buffer.flip();

        while (buffer.hasRemaining()) {
            char c = buffer.get();
            Action action = Action.NONE;
            boolean isBackslash = c == '\\';
            boolean isCarriageReturn = c == '\r';
            boolean isNewline = c == '\n';
            boolean isWhitespace = WHITESPACE_CHARS.indexOf(c) != -1;
            boolean inIdentifier = identifierBuilder.length() > 0;
            boolean isEscaped;
            if (state == State.LOOKING_FOR_TARGET) {
                isEscaped = ESCAPED_TARGET_CHARS.indexOf(c) != -1;
            } else {
                isEscaped = ESCAPED_PREREQ_CHARS.indexOf(c) != -1;
            }

            if (isBackslash) {
                // We need to count the number of backslashes in case the
                // first non-backslash is an escaped character.
                numBackslashes++;
            } else if (numBackslashes > 0 && isEscaped) {
                // Consume one backslash to escape the special char.
                numBackslashes--;
                if (inIdentifier) {
                    action = Action.APPEND_TO_IDENTIFIER;
                }
            } else if (isWhitespace) {
                if (numBackslashes == 0) {
                    if (state == State.FOUND_TARGET && inIdentifier) {
                        action = Action.ADD_PREREQ;
                    }
                    if (state == State.FOUND_TARGET && (isNewline || isCarriageReturn)) {
                        state = State.LOOKING_FOR_TARGET;
                    }
                } else if (isNewline) {
                    // Consume one backslash to escape \n or \r\n.
                    numBackslashes--;
                } else if (!isCarriageReturn) {
                    action = Action.APPEND_TO_IDENTIFIER;
                }
            } else if (c == ':' && state == State.LOOKING_FOR_TARGET) {
                state = State.FOUND_TARGET;
                action = Action.SET_TARGET;
            } else {
                action = Action.APPEND_TO_IDENTIFIER;
            }

            if (!isBackslash && numBackslashes > 0 && !isCarriageReturn) {
                int numBackslashesToAppend;
                if (isEscaped || isWhitespace) {
                    // Backslashes escape themselves before an escaped character or whitespace.
                    numBackslashesToAppend = numBackslashes / 2;
                } else {
                    // Backslashes are literal before a non-escaped character.
                    numBackslashesToAppend = numBackslashes;
                }

                for (int i = 0; i < numBackslashesToAppend; i++) {
                    identifierBuilder.append('\\');
                }
                numBackslashes = 0;
            }

            switch (action) {
            case NONE:
                break;
            case APPEND_TO_IDENTIFIER:
                identifierBuilder.append(c);
                break;
            case SET_TARGET:
                if (target != null) {
                    throw new HumanReadableException(
                            "Depfile parser cannot handle .d file with multiple targets");
                }
                target = identifierBuilder.toString();
                identifierBuilder.setLength(0);
                break;
            case ADD_PREREQ:
                prereqsBuilder.add(identifierBuilder.toString());
                identifierBuilder.setLength(0);
                break;
            }
        }

        buffer.clear();
    }

    ImmutableList<String> prereqs = prereqsBuilder.build();
    if (target == null || prereqs.isEmpty()) {
        throw new IOException("Could not find target or prereqs parsing depfile");
    } else {
        return new Depfile(target, prereqs);
    }
}

From source file:net.derquinse.common.jaxrs.PathSegments.java

/**
 * Turns a collection of string into a list of decoded segments.
 * @param encoded If the segments are encoded.
 * @param segments String segments.//from   w  ww.  j  ava  2s.  com
 * @return The never {@code null} list of segments.
 */
public static PathSegments of(boolean encoded, @Nullable Iterable<String> segments) {
    if (segments == null) {
        return EMPTY;
    }
    if (!encoded) {
        return new PathSegments(ImmutableList.copyOf(segments));
    }
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    for (String s : segments) {
        if (s != null && s.length() > 0) {
            if (encoded) {
                try {
                    s = URLDecoder.decode(s, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                }
            }
            builder.add(s);
        }
    }
    ImmutableList<String> list = builder.build();
    if (list.isEmpty()) {
        return EMPTY;
    }
    return new PathSegments(builder.build());
}

From source file:com.google.javascript.jscomp.JSDocInfoPrinter.java

public static String print(JSDocInfo info) {
    StringBuilder sb = new StringBuilder("/**");
    if (info.isConstructor()) {/*from  ww w. j  a va2s.  c o  m*/
        sb.append("@constructor ");
    }
    if (info.isInterface() && !info.usesImplicitMatch()) {
        sb.append("@interface ");
    }
    if (info.isInterface() && info.usesImplicitMatch()) {
        sb.append("@record ");
    }
    if (info.makesDicts()) {
        sb.append("@dict ");
    }
    if (info.makesStructs()) {
        sb.append("@struct ");
    }
    if (info.makesUnrestricted()) {
        sb.append("@unrestricted ");
    }
    if (info.isDefine()) {
        sb.append("@define {");
        appendTypeNode(sb, info.getType().getRoot());
        sb.append("} ");
    }

    if (info.isOverride()) {
        sb.append("@override ");
    }
    if (info.isConstant()) {
        sb.append("@const ");
    }
    if (info.isExport()) {
        sb.append("@export ");
    }
    if (info.isDeprecated()) {
        sb.append("@deprecated ");
        sb.append(info.getDeprecationReason() + " ");
    }
    if (info.getVisibility() != null && info.getVisibility() != Visibility.INHERITED) {
        sb.append("@" + info.getVisibility().toString().toLowerCase() + " ");
    }

    Iterator<String> suppressions = info.getSuppressions().iterator();
    if (suppressions.hasNext()) {
        sb.append("@suppress {");
        while (suppressions.hasNext()) {
            sb.append(suppressions.next());
            if (suppressions.hasNext()) {
                sb.append(",");
            }
        }
        sb.append("} ");
    }

    ImmutableList<String> names = info.getTemplateTypeNames();
    if (!names.isEmpty()) {
        sb.append("@template ");
        Joiner.on(',').appendTo(sb, names);
        sb.append("\n"); // @template needs a newline afterwards
    }

    if (info.getParameterCount() > 0) {
        for (String name : info.getParameterNames()) {
            sb.append("@param ");
            if (info.getParameterType(name) != null) {
                sb.append("{");
                appendTypeNode(sb, info.getParameterType(name).getRoot());
                sb.append("} ");
            }
            sb.append(name);
            sb.append(' ');
        }
    }
    if (info.hasReturnType()) {
        sb.append("@return {");
        appendTypeNode(sb, info.getReturnType().getRoot());
        sb.append("} ");
    }
    if (info.hasThisType()) {
        sb.append("@this {");
        Node typeNode = info.getThisType().getRoot();
        if (typeNode.getType() == Token.BANG) {
            appendTypeNode(sb, typeNode.getFirstChild());
        } else {
            appendTypeNode(sb, typeNode);
        }
        sb.append("} ");
    }
    if (info.hasBaseType()) {
        sb.append("@extends {");
        Node typeNode = info.getBaseType().getRoot();
        if (typeNode.getType() == Token.BANG) {
            appendTypeNode(sb, typeNode.getFirstChild());
        } else {
            appendTypeNode(sb, typeNode);
        }
        sb.append("} ");
    }
    for (JSTypeExpression type : info.getImplementedInterfaces()) {
        sb.append("@implements {");
        Node typeNode = type.getRoot();
        if (typeNode.getType() == Token.BANG) {
            appendTypeNode(sb, typeNode.getFirstChild());
        } else {
            appendTypeNode(sb, typeNode);
        }
        sb.append("} ");
    }
    if (info.hasTypedefType()) {
        sb.append("@typedef {");
        appendTypeNode(sb, info.getTypedefType().getRoot());
        sb.append("} ");
    }
    if (info.hasType()) {
        if (info.isInlineType()) {
            sb.append(" ");
            appendTypeNode(sb, info.getType().getRoot());
            sb.append(" ");
        } else {
            sb.append("@type {");
            appendTypeNode(sb, info.getType().getRoot());
            sb.append("} ");
        }
    }
    if (!info.getThrownTypes().isEmpty()) {
        sb.append("@throws {");
        appendTypeNode(sb, info.getThrownTypes().get(0).getRoot());
        sb.append("} ");
    }
    if (info.hasEnumParameterType()) {
        sb.append("@enum {");
        appendTypeNode(sb, info.getEnumParameterType().getRoot());
        sb.append("} ");
    }
    sb.append("*/");
    return sb.toString();
}

From source file:org.locationtech.geogig.model.impl.RevTreeImpl.java

public static RevTree create(final ObjectId id, final long size, final int childTreeCount,
        @Nullable ImmutableList<Node> trees, @Nullable ImmutableList<Node> features,
        @Nullable ImmutableSortedMap<Integer, Bucket> buckets) {

    checkNotNull(id);//from  ww  w .ja  va2 s .co m

    if (buckets == null || buckets.isEmpty()) {
        return new LeafTree(id, size, features, trees);
    }

    if ((features == null || features.isEmpty()) && (trees == null || trees.isEmpty())) {
        return new NodeTree(id, size, childTreeCount, buckets);
    }
    return new MixedTree(id, size, childTreeCount, trees, features, buckets);
}

From source file:net.conquiris.lucene.search.Filters.java

/**
 * Adds clauses to a boolean filter.//from   www. j  av a2  s.  com
 * @param filter Boolean filter to which the clauses are added.
 * @param occur Specifies how clauses are to occur in matching documents.
 * @param filters Filters to use to build the clauses.
 * @return The provided boolean filter.
 * @throws IllegalArgumentException if the filters argument is empty.
 */
public static BooleanFilter addClauses(BooleanFilter filter, Occur occur, Iterable<? extends Filter> filters) {
    checkNotNull(filter, "The destination boolean filter must be provided");
    checkNotNull(occur, "The occurrence specification must be provided");
    ImmutableList<Filter> list = ImmutableList.copyOf(filters);
    checkArgument(!list.isEmpty(), "At least one filter must be provided");
    for (Filter f : filters) {
        filter.add(f, occur);
    }
    return filter;
}

From source file:org.sonarqube.gradle.SonarQubePlugin.java

@Nullable
public static <T> List<T> nonEmptyOrNull(Iterable<T> iterable) {
    ImmutableList<T> list = ImmutableList.copyOf(iterable);
    return list.isEmpty() ? null : list;
}