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

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

Introduction

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

Prototype

public static String repeat(final char ch, final int repeat) 

Source Link

Document

<p>Returns padding using the specified delimiter repeated to a given length.</p> <pre> StringUtils.repeat('e', 0) = "" StringUtils.repeat('e', 3) = "eee" StringUtils.repeat('e', -2) = "" </pre> <p>Note: this method doesn't not support padding with <a href="http://www.unicode.org/glossary/#supplementary_character">Unicode Supplementary Characters</a> as they require a pair of char s to be represented.

Usage

From source file:de.uni.bremen.monty.moco.codegeneration.context.CodeWriter.java

/** Write a line to this buffer.
 *
 * The line gets indented according to the level of indentation and is terminated by a newline. */
public void appendLine(String data) {
    stringBuffer.append(StringUtils.repeat(" ", indentation * 4));
    stringBuffer.append(data);/*from w w  w  .  j  ava2 s  .  c  o  m*/
    appendEmptyLine();
}

From source file:datawarehouse.CSVLoader.java

/**
 * Parse CSV file using OpenCSV library and load in given database table.
 *
 * @param csvFile Input CSV file/*from www.j  a v  a 2s . c  om*/
 * @param tableName Database table name to import data
 * @param truncateBeforeLoad Truncate the table before inserting new
 * records.
 * @throws Exception
 */
public void loadCSV(String csvFile, String tableName, boolean truncateBeforeLoad) throws Exception {

    CSVReader csvReader = null;
    if (null == this.connection) {
        throw new Exception("Not a valid connection.");
    }
    try {

        csvReader = new CSVReader(new FileReader(csvFile), this.seprator);

    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Error occured while executing file. " + e.getMessage());
    }

    String[] headerRow = csvReader.readNext();

    if (null == headerRow) {
        throw new FileNotFoundException(
                "No columns defined in given CSV file." + "Please check the CSV file format.");
    }

    String questionmarks = StringUtils.repeat("?,", headerRow.length);
    questionmarks = (String) questionmarks.subSequence(0, questionmarks.length() - 1);

    String query = SQL_INSERT.replaceFirst(TABLE_REGEX, tableName);
    query = query.replaceFirst(KEYS_REGEX, StringUtils.join(headerRow, ","));
    query = query.replaceFirst(VALUES_REGEX, questionmarks);

    System.out.println("Query: " + query);

    String[] nextLine;
    Connection con = null;
    PreparedStatement ps = null;
    try {
        con = this.connection;
        con.setAutoCommit(false);
        ps = con.prepareStatement(query);

        if (truncateBeforeLoad) {
            //delete data from table before loading csv
            con.createStatement().execute("DELETE FROM " + tableName);
        }

        final int batchSize = 1000;
        int count = 0;
        while ((nextLine = csvReader.readNext()) != null) {
            if (null != nextLine) {
                int index = 1;
                for (String string : nextLine) {
                    //System.out.print(string + ": ");
                    try {
                        DateFormat format = new SimpleDateFormat("dd.mm.yyyy");
                        Date date = format.parse(string);
                        ps.setDate(index++, new java.sql.Date(date.getTime()));
                        //System.out.println("date");
                    } catch (ParseException | SQLException e) {
                        try {
                            Double income = parseDouble(string.replace(",", "."));
                            ps.setDouble(index++, income);
                            //System.out.println("double");
                        } catch (NumberFormatException | SQLException err) {
                            ps.setString(index++, string);
                            //System.out.println("string");
                        }
                    }
                }
                ps.addBatch();
            }
            if (++count % batchSize == 0) {
                ps.executeBatch();
            }
        }
        ps.executeBatch(); // insert remaining records
        con.commit();
    } catch (Exception e) {
        con.rollback();
        e.printStackTrace();
        throw new Exception("Error occured while loading data from file to database." + e.getMessage());
    } finally {
        if (null != ps) {
            ps.close();
        }
        if (null != con) {
            con.close();
        }

        csvReader.close();
    }
}

From source file:com.opensymphony.xwork2.conversion.impl.NumberConverterTest.java

public void testStringToBigDecimalConversionPL() throws Exception {
    // given/*from  w  w  w  . j  a v a2  s.co m*/
    NumberConverter converter = new NumberConverter();
    Map<String, Object> context = new HashMap<>();
    context.put(ActionContext.LOCALE, new Locale("pl", "PL"));

    // when a bit bigger than double
    String aBitBiggerThanDouble = "17976931348623157" + StringUtils.repeat('0', 291) + "1,"
            + StringUtils.repeat('0', 324) + "49";
    Object value = converter.convertValue(context, null, null, null, aBitBiggerThanDouble, BigDecimal.class);

    // then does not lose integer and fraction digits
    assertEquals(
            new BigDecimal(aBitBiggerThanDouble.substring(0, 309) + "." + aBitBiggerThanDouble.substring(310)),
            value);
}

From source file:com.aestasit.markdown.visitors.AstPrinter.java

protected void printNode(Node node) {
    printer.print(StringUtils.repeat(" ", level) + node.toString());
}

From source file:ch.cyberduck.core.http.LoggingHttpRequestExecutor.java

@Override
protected HttpResponse doSendRequest(final HttpRequest request, final HttpClientConnection conn,
        final HttpContext context) throws IOException, HttpException {
    synchronized (listener) {
        listener.log(TranscriptListener.Type.request, request.getRequestLine().toString());
        for (Header header : request.getAllHeaders()) {
            switch (header.getName()) {
            case HttpHeaders.AUTHORIZATION:
            case "X-Auth-Key":
            case "X-Auth-Token":
                listener.log(TranscriptListener.Type.request, String.format("%s: %s", header.getName(),
                        StringUtils.repeat("*", Integer.min(8, StringUtils.length(header.getValue())))));
                break;
            default:
                listener.log(TranscriptListener.Type.request, header.toString());
                break;
            }/*ww w. ja  v  a  2 s .  co m*/
        }
    }
    return super.doSendRequest(request, conn, context);
}

From source file:com.qcadoo.model.internal.types.DecimalTypeTest.java

@Test
public final void shouldFormatDecimalWithoutOfficiousTrimmingFractionalValue2() {
    // given//from ww  w  .j  av a 2  s .  co m
    final String decimalAsString = "1234567."
            + StringUtils.repeat("9", NumberService.DEFAULT_MAX_FRACTION_DIGITS_IN_DECIMAL) + "000001";
    BigDecimal value = new BigDecimal(decimalAsString);

    // when
    String result = decimalType.toString(value, locale);

    // then
    assertFormattedEquals(value, result);
}

From source file:com.qtplaf.library.util.NumberUtils.java

/**
 * Returns the default string number with the given decimal places. For instance, returns '0.00000' for 5 decimal
 * places.// w  w  w .  j av  a2  s  .  c o  m
 * 
 * @param decimals The number of decimal places.
 * @return The default string number.
 */
public static String defaultStringNumber(int decimals) {
    StringBuilder b = new StringBuilder();
    b.append("0");
    if (decimals > 0) {
        b.append(".");
        b.append(StringUtils.repeat("0", decimals));
    }
    return b.toString();
}

From source file:com.joyent.manta.client.MantaObjectDepthComparatorTest.java

private static List<MantaObject> fileObjects(final MantaObject dirObject, final int number) {
    List<MantaObject> objects = new ArrayList<>();

    for (int i = 0; i < number; i++) {
        final MantaObject object = mock(MantaObject.class);
        final int segmentChar = (number + i) % 26;
        String path = dirObject.getPath() + SEPARATOR + StringUtils.repeat(((char) (97 + segmentChar)), 4)
                + ".json";
        when(object.getPath()).thenReturn(path);
        when(object.getType()).thenReturn(MantaObject.MANTA_OBJECT_TYPE_OBJECT);
        when(object.getContentType()).thenReturn(ContentType.APPLICATION_JSON.toString());
        when(object.toString()).thenReturn("[F] " + path);
        objects.add(object);//from w ww  .  j a  v  a 2  s.c  om
    }

    return objects;
}

From source file:com.feilong.core.date.DateUtilTest.java

/**
 * Test get last date of this day.//  w w w  . ja  v a  2  s . c o m
 */
@Test
public void testGetLastDateOfThisDay() {
    logDate(DateUtil.getLastDateOfThisDay(NOW));
    LOGGER.debug(StringUtils.repeat("*", 20));

    logDate(DateUtils.ceiling(NOW, Calendar.DAY_OF_MONTH));
    logDate(DateUtils.round(NOW, Calendar.DAY_OF_MONTH));
    logDate(DateUtils.truncate(NOW, Calendar.DAY_OF_MONTH));
    LOGGER.debug(StringUtils.repeat("*", 20));
    logDate(DateUtils.ceiling(NOW, Calendar.MONTH));
    logDate(DateUtils.round(NOW, Calendar.MONTH));
    logDate(DateUtils.truncate(NOW, Calendar.MONTH));
}

From source file:com.xpn.xwiki.web.ExportURLFactoryContext.java

/**
 * Pushes "../" prefixes.//www  . jav a2s. co  m
 *
 * @param count the numer of "../" prefixes to add
 */
public void pushCSSPathAdjustment(int count) {
    this.cssPathAdjustementStack.push(StringUtils.repeat("../", count));
}