Example usage for org.apache.commons.lang StringUtils repeat

List of usage examples for org.apache.commons.lang StringUtils repeat

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils repeat.

Prototype

public static String repeat(String str, String separator, int repeat) 

Source Link

Document

Repeat a String repeat times to form a new String, with a String separator injected each time.

Usage

From source file:org.batoo.jpa.core.impl.jdbc.EntityTable.java

private String getRemoveSql(int size) {
    String sql = this.removeSqlMap.get(size);
    if (sql != null) {
        return sql;
    }//  w  ww.j av a 2 s .c  o m

    synchronized (this) {
        sql = this.removeSqlMap.get(size);
        if (sql != null) {
            return sql;
        }

        this.removeColumns = new AbstractColumn[this.pkColumns.size()];
        this.pkColumns.values().toArray(this.removeColumns);

        String restriction;
        if (this.pkColumns.size() > 1) {
            final Collection<String> restrictions = Collections2.transform(this.pkColumns.values(),
                    new Function<AbstractColumn, String>() {

                        @Override
                        public String apply(AbstractColumn input) {
                            return input.getName() + " = ?";
                        }
                    });
            final String singleRestriction = Joiner.on(" AND ").join(restrictions);
            restriction = StringUtils.repeat(singleRestriction, " OR ", size);
        } else if (size == 1) {
            restriction = this.removeColumns[0].getName() + " = ?";
        } else {
            restriction = this.removeColumns[0].getName() + " IN (" + StringUtils.repeat("?", ", ", size) + ")";
        }

        sql = "DELETE FROM " + this.getQName() + " WHERE " + restriction;
        this.removeSqlMap.put(size, sql);

        return sql;
    }
}

From source file:org.batoo.jpa.jdbc.AbstractTable.java

/**
 * Generates the insert statement for the type.
 * /*from   w w  w .  jav a  2  s  .co  m*/
 * @param type
 *            the type to generate the insert statement for
 * @param pkColumns
 *            the primary key columns
 * 
 * @since 2.0.0
 */
private synchronized void generateInsertSql(final EntityTypeDescriptor type, int size) {
    final String sqlKey = type != null ? type.getName() + size : "" + size;

    String sql = this.insertSqlMap.get(sqlKey);
    if (sql != null) { // other thread finished the job for us
        return;
    }

    final List<AbstractColumn> insertColumns = Lists.newArrayList();

    // Filter out the identity physicalColumns
    final Collection<AbstractColumn> filteredColumns = type == null ? this.columnMap.values()
            : Collections2.filter(this.columnMap.values(), new Predicate<AbstractColumn>() {

                @Override
                public boolean apply(AbstractColumn input) {
                    return AbstractTable.this.isInsertableColumn(type, input);
                }

            });

    // prepare the names tuple in the form of "COLNAME [, COLNAME]*"

    final Collection<String> columnNames = Collections2.transform(filteredColumns,
            new Function<AbstractColumn, String>() {

                @Override
                public String apply(AbstractColumn input) {
                    insertColumns.add(input);

                    return input.getName();
                }
            });

    if (columnNames.size() == 0) {
        // TODO investigate in others with identity
        sql = "INSERT INTO " + this.getQName() + " DEFAULT VALUES";
    } else {

        final Collection<String> singleParams = Collections2.transform(filteredColumns,
                new Function<AbstractColumn, String>() {

                    @Override
                    public String apply(AbstractColumn input) {
                        String writeParam = null;
                        if (input instanceof BasicColumn) {
                            final ColumnTransformerMetadata columnTransformer = ((BasicColumn) input)
                                    .getMapping().getColumnTransformer();
                            writeParam = columnTransformer != null ? columnTransformer.getWrite() : null;
                        }
                        writeParam = Strings.isNullOrEmpty(writeParam) ? "?" : writeParam;

                        return writeParam;
                    }
                });

        // prepare the parameters in the form of "? [, ?]*"
        final String singleParamStr = "\t(" + Joiner.on(", ").join(singleParams) + ")";
        final String parametersStr = StringUtils.repeat(singleParamStr, ",\n", size);

        final String columnNamesStr = Joiner.on(", ").join(columnNames);

        // INSERT INTO SCHEMA.TABLE
        // (COL [, COL]*)
        // VALUES (PARAM [, PARAM]*)
        sql = "INSERT INTO " + this.getQName() //
                + "\n(" + columnNamesStr + ")"//
                + "\nVALUES\n" + parametersStr;
    }

    this.insertSqlMap.put(sqlKey, sql);
    this.insertColumnsMap.put(sqlKey, insertColumns.toArray(new AbstractColumn[insertColumns.size()]));
}

From source file:org.batoo.jpa.jdbc.EntityTable.java

private String getRemoveSql(int size) {
    String sql = this.removeSqlMap.get(size);
    if (sql != null) {
        return sql;
    }//w w  w. j  ava 2  s.c  om

    synchronized (this) {
        sql = this.removeSqlMap.get(size);
        if (sql != null) {
            return sql;
        }

        String restriction;
        if (size == 1) {
            restriction = this.getRestrictionSql(this.pkColumns);
        } else {
            restriction = this.pkColumns.values().iterator().next().getName() + " IN ("
                    + StringUtils.repeat("?", ", ", size) + ")";
        }

        sql = "DELETE FROM " + this.getQName() + " WHERE " + restriction;
        this.removeSqlMap.put(size, sql);

        return sql;
    }
}

From source file:org.broadinstitute.gatk.queue.extensions.gatk.GATKExtensionsGenerator.java

/**
 * Returns a string representing the type parameters for a class, or an empty string if there are no type parameters.
 * @param clazz The class to look for type parameters.
 * @return The type parameters or an empty string.
 *//*from w  ww .j  a  va2  s  .c o m*/
private String getScalaTypeParams(Class<?> clazz) {
    TypeVariable[] typeParams = clazz.getTypeParameters();
    if (typeParams.length == 0)
        return "";
    return "[" + StringUtils.repeat("_", ",", typeParams.length) + "]";
}

From source file:org.caleydo.core.util.impute.KNNImpute.java

public static void main(String[] args) throws IOException {
    ImmutableList.Builder<Gene> b = ImmutableList.builder();
    List<String> lines = CharStreams
            .readLines(new InputStreamReader(KNNImpute.class.getResourceAsStream("khan.csv")));
    lines = lines.subList(1, lines.size());
    int j = 0;//from   w  w w  .j a va 2  s  .  c o m
    for (String line : lines) {
        String[] l = line.split(";");
        float[] d = new float[l.length];
        int nans = 0;
        for (int i = 0; i < l.length; ++i) {
            if ("NA".equals(l[i])) {
                nans++;
                d[i] = Float.NaN;
            } else {
                d[i] = Float.parseFloat(l[i]);
            }
        }
        b.add(new Gene(j++, nans, d));
    }
    final KNNImputeDescription desc2 = new KNNImputeDescription();
    desc2.setMaxp(100000);
    KNNImpute r = new KNNImpute(desc2, b.build());
    ForkJoinPool p = new ForkJoinPool();
    p.invoke(r);
    try (PrintWriter w = new PrintWriter("khan.imputed.csv")) {
        w.println(StringUtils.repeat("sample", ";", r.samples));
        for (Gene g : r.genes) {
            float[] d = g.data;
            int nan = 0;
            w.print(Float.isNaN(d[0]) ? g.nanReplacements[nan++] : d[0]);
            for (int i = 1; i < d.length; ++i)
                w.append(';').append(String.valueOf(Float.isNaN(d[i]) ? g.nanReplacements[nan++] : d[i]));
            w.println();
        }
    }
}