Example usage for com.google.common.collect ObjectArrays concat

List of usage examples for com.google.common.collect ObjectArrays concat

Introduction

In this page you can find the example usage for com.google.common.collect ObjectArrays concat.

Prototype

public static <T> T[] concat(T[] array, @Nullable T element) 

Source Link

Document

Returns a new array that appends element to array .

Usage

From source file:humanize.Humanize.java

/**
 * <p>/*from www  .ja  v a 2  s  . c o  m*/
 * Applies the proper format for a given plural state.
 * </p>
 * Example:
 * 
 * <pre>
 * PluralizeParams p = PluralizeParams
 *         .begin(&quot;There is one file on {1}.&quot;)
 *         .many(&quot;There are {0} files on {1}.&quot;)
 *         .none(&quot;There are no files on {1}.&quot;)
 *         .exts(&quot;disk&quot;);
 * 
 * pluralize(1, p);
 * // == There is one file on disk.
 * pluralize(2, p);
 * // == There are 2 files on disk.
 * pluralize(0, p);
 * // == There are no files on disk.
 * 
 * // ---
 * 
 * PluralizeParams p = PluralizeParams
 *         .begin(&quot;one&quot;)
 *         .many(&quot;{0}&quot;)
 *         .none(&quot;none&quot;);
 * 
 * pluralize(1, p);
 * // = &quot;one&quot;
 * pluralize(2, p);
 * // = &quot;2&quot;
 * </pre>
 * 
 * @param number
 *            The number that triggers the plural state
 * @return formatted text according the right plural state
 */
public static String pluralize(final Number number, final PluralizeParams p) {
    Preconditions.checkNotNull(p.many, "Please, specify a format for many elements");
    Preconditions.checkNotNull(p.one, "Please, specify a format for a single element");

    String none = p.none == null ? p.many : p.none;
    MessageFormat format = pluralizeFormat("{0}", none, p.one, p.many);
    Object[] fp = p.exts == null ? new Object[] { number } : ObjectArrays.concat(number, p.exts);
    return format.render(fp);
}

From source file:com.android.tools.lint.psi.EcjPsiBuilder.java

@NonNull
private EcjPsiDeclarationStatement toDeclarationStatement(@NonNull EcjPsiSourceElement parent,
        @NonNull AbstractVariableDeclaration statement) {
    // ECJ doesn't distinguish between
    //    int x;//from   www .  jav a  2s .c  o  m
    //    int y;
    // and
    //    int x, y;
    //
    // However, we need to preserve this distinction in the AST. Therefore, figure
    // out when this is the case and fold these separate, consecutive statement nodes into a
    // single declaration statement

    if (parent.mLastChild instanceof EcjPsiDeclarationStatement
            && parent.mLastChild.mNativeNode instanceof AbstractVariableDeclaration
            && statement.declarationSourceStart == ((AbstractVariableDeclaration) parent.mLastChild.mNativeNode).declarationSourceStart) {
        // Just create a new variable as a child of the existing declaration statement
        EcjPsiDeclarationStatement declaration = (EcjPsiDeclarationStatement) parent.mLastChild;
        PsiElement prevVariable = declaration.getLastChild();
        assert statement instanceof LocalDeclaration;
        assert prevVariable instanceof EcjPsiLocalVariable;
        LocalDeclaration localDeclaration = (LocalDeclaration) statement;
        // Constructing variable directly instead of calling toVariable() since we
        // don't want to include modifiers and type elements here
        EcjPsiLocalVariable variable = toVariable(declaration, localDeclaration, false);

        // Tweak offsets such that they don't overlap: use sourceStart instead of
        // declarationSourceStart to exclude type for the second node, and limit the
        // end of the previous node to the start
        TextRange prevRange = prevVariable.getTextRange();
        ((EcjPsiLocalVariable) prevVariable).setRange(prevRange.getStartOffset(),
                Math.min(prevRange.getEndOffset(), localDeclaration.sourceStart));
        variable.setRange(localDeclaration.sourceStart,
                localDeclaration.initialization != null ? localDeclaration.initialization.sourceEnd + 1
                        : localDeclaration.sourceEnd + 1);

        // Merge array of declared elements
        PsiElement[] declaredElements = declaration.getDeclaredElements();
        declaration.setDeclaredElements(ObjectArrays.concat(declaredElements, variable));

        return declaration;
    }

    EcjPsiDeclarationStatement declaration = new EcjPsiDeclarationStatement(mManager, statement);
    parent.adoptChild(declaration);
    assert statement instanceof LocalDeclaration;
    EcjPsiVariable variable = toVariable(declaration, (LocalDeclaration) statement, true);
    declaration.setDeclaredElements(new PsiElement[] { variable });

    return declaration;
}