Example usage for com.google.common.base Strings emptyToNull

List of usage examples for com.google.common.base Strings emptyToNull

Introduction

In this page you can find the example usage for com.google.common.base Strings emptyToNull.

Prototype

@Nullable
public static String emptyToNull(@Nullable String string) 

Source Link

Document

Returns the given string if it is nonempty; null otherwise.

Usage

From source file:org.jclouds.proxy.internal.GuiceProxyConfig.java

private static String checkNotEmpty(String nullableString, String message, Object... args) {
    checkArgument(Strings.emptyToNull(nullableString) != null, message, args);
    return nullableString;
}

From source file:de.schildbach.pte.StvProvider.java

@Override
public QueryDeparturesResult queryDepartures(final String stationId, final @Nullable Date time,
        final int maxDepartures, final boolean equivs) throws IOException {
    checkNotNull(Strings.emptyToNull(stationId));

    return queryDeparturesMobile(stationId, time, maxDepartures, equivs);
}

From source file:org.sonar.server.issue.notification.IssueChangeNotification.java

@CheckForNull
private static String neverEmptySerializableToString(@Nullable Serializable s) {
    return s != null ? Strings.emptyToNull(s.toString()) : null;
}

From source file:io.druid.query.expression.RegexpExtractExprMacro.java

@Override
public Expr apply(final List<Expr> args) {
    if (args.size() < 2 || args.size() > 3) {
        throw new IAE("Function[%s] must have 2 to 3 arguments", name());
    }// www . ja v a  2s  . com

    final Expr arg = args.get(0);
    final Expr patternExpr = args.get(1);
    final Expr indexExpr = args.size() > 2 ? args.get(2) : null;

    if (!patternExpr.isLiteral() || (indexExpr != null && !indexExpr.isLiteral())) {
        throw new IAE("Function[%s] pattern and index must be literals", name());
    }

    // Precompile the pattern.
    final Pattern pattern = Pattern.compile(String.valueOf(patternExpr.getLiteralValue()));

    final int index = indexExpr == null ? 0 : ((Number) indexExpr.getLiteralValue()).intValue();
    class RegexpExtractExpr implements Expr {
        @Nonnull
        @Override
        public ExprEval eval(final ObjectBinding bindings) {
            final Matcher matcher = pattern.matcher(Strings.nullToEmpty(arg.eval(bindings).asString()));
            final String retVal = matcher.find() ? matcher.group(index) : null;
            return ExprEval.of(Strings.emptyToNull(retVal));
        }

        @Override
        public void visit(final Visitor visitor) {
            arg.visit(visitor);
            visitor.visit(this);
        }
    }
    return new RegexpExtractExpr();
}

From source file:org.n52.shetland.w3c.xlink.Reference.java

public Reference setTitle(String title) {
    this.title = Optional.ofNullable(Strings.emptyToNull(title));
    return this;
}

From source file:org.obiba.onyx.quartz.core.engine.questionnaire.question.Attributes.java

/**
 * @param namespace//from   w ww .ja  v a 2s  . com
 * @param name
 * @return
 */
public static Predicate<Attribute> predicate(final String namespace, final String name) {
    final String nullIfEmptyNamespace = Strings.emptyToNull(namespace);
    return new Predicate<Attribute>() {
        @Override
        public boolean apply(Attribute input) {
            return input.getName().equals(name) && (input.getNamespace() == null ? nullIfEmptyNamespace == null
                    : input.getNamespace().equals(nullIfEmptyNamespace));
        }
    };
}

From source file:org.sonar.server.rule.Rules.java

public void updateRuleNote(int ruleId, String note) {
    RuleDto rule = findRuleNotNull(ruleId);
    String sanitizedNote = Strings.emptyToNull(note);
    if (sanitizedNote != null) {
        ruleOperations.updateRuleNote(rule, note, UserSession.get());
    } else {/*from  w ww . j  av a2s  .c  o m*/
        ruleOperations.deleteRuleNote(rule, UserSession.get());
    }
}

From source file:io.druid.query.expression.LookupExprMacro.java

@Override
public Expr apply(final List<Expr> args) {
    if (args.size() != 2) {
        throw new IAE("Function[%s] must have 2 arguments", name());
    }/* ww  w .ja v  a 2 s  . c o m*/

    final Expr arg = args.get(0);
    final Expr lookupExpr = args.get(1);

    if (!lookupExpr.isLiteral() || lookupExpr.getLiteralValue() == null) {
        throw new IAE("Function[%s] second argument must be a registered lookup name", name());
    }

    final String lookupName = lookupExpr.getLiteralValue().toString();
    final RegisteredLookupExtractionFn extractionFn = new RegisteredLookupExtractionFn(lookupReferencesManager,
            lookupName, false, null, false, null);

    class LookupExpr implements Expr {
        @Nonnull
        @Override
        public ExprEval eval(final ObjectBinding bindings) {
            return ExprEval.of(extractionFn.apply(Strings.emptyToNull(arg.eval(bindings).asString())));
        }

        @Override
        public void visit(final Visitor visitor) {
            arg.visit(visitor);
            visitor.visit(this);
        }
    }

    return new LookupExpr();
}

From source file:com.reprezen.swagedit.assist.ext.MediaTypeContentAssistExt.java

@Override
public Collection<Proposal> getProposals(TypeDefinition type, AbstractNode node, String prefix) {
    Collection<Proposal> proposals = new ArrayList<>();

    prefix = Strings.emptyToNull(prefix);

    for (JsonNode mediaType : mediaTypes) {
        String asText = mediaType.asText();
        if (prefix != null) {
            if (asText.contains(prefix.trim())) {
                proposals.add(createProposal(type, mediaType));
            }/*  w  ww .j a  v a 2 s. c  o m*/
        } else {
            proposals.add(createProposal(type, mediaType));
        }
    }

    return proposals;
}

From source file:io.druid.query.extraction.FunctionalExtraction.java

/**
 * The general constructor which handles most of the logic for extractions which can be expressed as a function of string-->string
 *
 * @param extractionFunction      The function to call when doing the extraction. The function must be able to accept a null input.
 * @param retainMissingValue      Boolean value on if functions which result in `null` should use the original value or should be kept as `null`
 * @param replaceMissingValueWith String value to replace missing values with (instead of `null`)
 * @param injective               If this function always has 1:1 renames and the domain has the same cardinality of the input, this should be true and enables optimized query paths.
 *///from   w w  w  .j a  v a  2  s  .  c o  m
public FunctionalExtraction(final Function<String, String> extractionFunction, final boolean retainMissingValue,
        final String replaceMissingValueWith, final boolean injective) {
    this.retainMissingValue = retainMissingValue;
    this.replaceMissingValueWith = Strings.emptyToNull(replaceMissingValueWith);
    Preconditions.checkArgument(
            !(this.retainMissingValue && !Strings.isNullOrEmpty(this.replaceMissingValueWith)),
            "Cannot specify a [replaceMissingValueWith] and set [retainMissingValue] to true");

    // If missing values are to be retained, we have a slightly different code path
    // This is intended to have the absolutely fastest code path possible and not have any extra logic in the function
    if (this.retainMissingValue) {

        this.extractionFunction = new Function<String, String>() {
            @Nullable
            @Override
            public String apply(@Nullable String dimValue) {
                final String retval = extractionFunction.apply(dimValue);
                return Strings.isNullOrEmpty(retval) ? Strings.emptyToNull(dimValue) : retval;
            }
        };
    } else {
        this.extractionFunction = new Function<String, String>() {
            @Nullable
            @Override
            public String apply(@Nullable String dimValue) {
                final String retval = extractionFunction.apply(dimValue);
                return Strings.isNullOrEmpty(retval) ? FunctionalExtraction.this.replaceMissingValueWith
                        : retval;
            }
        };
    }
    this.extractionType = injective ? ExtractionType.ONE_TO_ONE : ExtractionType.MANY_TO_ONE;
}