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

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

Introduction

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

Prototype

public static String leftPad(String str, int size, String padStr) 

Source Link

Document

Left pad a String with a specified String.

Usage

From source file:pl.edu.icm.coansys.commons.java.DiacriticsRemover.java

/**
 * Generates a sort key for a given text. This key is useful in environments
 * where only basic Latin characters are reliably sorted (for example, a
 * RDBMS with unknown collation settings).
 *
 * @param text Text to process.// ww w.  j  a  v  a  2 s .c o  m
 * @param idempotent Whether the conversion should be idempotent. This is
 * guaranteed to be true:
 * <code>alphaSortable(s, true).equals(alphaSortable(alphaSortable(s, true), true)</code>,
 * while this is not necessarily true:
 * <code>alphaSortable(s, false).equals(alphaSortable(alphaSortable(s, false), false)</code>.
 * @return
 */
public static String alphaSortable(String text, boolean idempotent) {
    if (text == null) {
        return null;
    }

    if (idempotent && text.startsWith(MAGIC)) {
        return text;
    }

    String tmp = text.toLowerCase(Locale.ENGLISH);
    tmp = Normalizer.normalize(tmp, Normalizer.Form.NFKD);

    StringBuilder builder = new StringBuilder();
    if (idempotent) {
        builder.append(MAGIC);
    }

    boolean wasSpaceSeparator = false;
    for (int i = 0; i < tmp.length(); i++) {
        Character ch = tmp.charAt(i);
        if (!ArrayUtils.contains(INTERESTING_TYPES, Character.getType(ch))
                && !ArrayUtils.contains(INTERESTING_CHARACTERS, ch)) {
            continue;
        }

        String s;

        // TODO quick fix of mantis 3231
        if (isSpaceSeparator(ch)) {
            if (wasSpaceSeparator) {
                continue;
            }
            wasSpaceSeparator = true;
        } else {
            wasSpaceSeparator = false;
        }

        if (alphaSortableMapping.containsKey(ch)) {
            s = alphaSortableMapping.get(ch);
        } else if (lookup.containsKey(ch)) {
            s = lookup.get(ch);
        } else {
            s = ch.toString();
        }

        for (int j = 0; j < s.length(); j++) {
            Character c = s.charAt(j);
            // TODO Very ugly workaround of the problem described in 0002643
            if (ArrayUtils.contains(INTERESTING_CHARACTERS, c)) {
                builder.append(c);
            } else {
                builder.append(StringUtils.leftPad(Integer.toHexString(c.charValue()), 4, '0'));
            }
        }
    }

    return builder.toString();
}

From source file:pt.ua.tm.neji.evaluation.CompleteEvaluator.java

private void printEvaluation(final String entity, final Evaluation evaluation) {
    DecimalFormat decimalFormat = new DecimalFormat("0.0000");

    logger.info("{}:\tTP:{}\tFP:{}\tFN:{}\t\tP:{}\tR:{}\tF1:{}",
            new Object[] { StringUtils.leftPad(entity.toUpperCase(), 30, " "), evaluation.getTP(),
                    evaluation.getFP(), evaluation.getFN(), decimalFormat.format(evaluation.getPrecision()),
                    decimalFormat.format(evaluation.getRecall()), decimalFormat.format(evaluation.getF1()) });

}

From source file:se.cambio.cds.gdl.editor.controller.GDLEditor.java

private String generateNextGTCode() {
    String nextGTCode = GT_HEADER + StringUtils.leftPad("" + (getNextTermNumber()), 4, "0");
    // Generate codes for all terminologies
    for (String langCode : getTermDefinitions().keySet()) {
        getTerm(langCode, nextGTCode);/*from   w  w w .  ja  v a2  s.c  o m*/
    }
    return nextGTCode;
}

From source file:se.cambio.cds.gdl.model.GuideDefinition.java

public void changeArchetypeBindings(List<ArchetypeBinding> archetypeBindings) {
    Map<String, ArchetypeBinding> archetypeBindingsAux = new HashMap<String, ArchetypeBinding>();
    int i = 1;/* w w w. j a  v  a  2 s . com*/
    for (ArchetypeBinding archetypeBinding : archetypeBindings) {
        String abCode = ARCHETYPE_BINDING_PREFIX + StringUtils.leftPad("" + (i++), 4, "0");
        archetypeBinding.setId(abCode);
        archetypeBindingsAux.put(abCode, archetypeBinding);
    }
    this.archetypeBindings = archetypeBindingsAux;
}

From source file:stroom.pipeline.destination.RollingFileDestination.java

private void roll() throws IOException {
    rolled = true;// w  w w .j a v a  2  s. co m

    boolean success = false;
    IOException exception = null;

    // If we have written then write a footer if we have one.
    if (footer != null && outputStream != null && outputStream.getBytesWritten() > 0) {
        // Write the footer.
        try {
            write(footer);
        } catch (final Throwable e) {
            exception = handleException(exception, e);
        }
    }
    // Try and close the output stream.
    try {
        close();
    } catch (final Throwable e) {
        exception = handleException(exception, e);
    }

    String destFileName = rolledFileName;
    destFileName = PathCreator.replaceTimeVars(destFileName);
    destFileName = PathCreator.replaceUUIDVars(destFileName);
    destFileName = PathCreator.replaceFileName(destFileName, fileName);

    // Create the destination file.
    final File destFile = new File(dir, destFileName);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Rolling file '%s' to '%s'", getFullPath(file), getFullPath(destFile));
    }

    // Create source path.
    Path source = null;
    try {
        source = file.toPath();
    } catch (final Throwable t) {
        exception = handleRollException(file, destFile, exception, t);
    }

    // Create destination path.
    Path dest = null;
    try {
        dest = destFile.toPath();
    } catch (final Throwable t) {
        exception = handleRollException(file, destFile, exception, t);
    }

    // If we have got valid paths for source and dest then attempt move.
    if (source != null && dest != null) {
        if (Files.isRegularFile(dest)) {
            LOGGER.error("Failed to roll file '%s' to '%s' as target exists", getFullPath(file),
                    getFullPath(destFile));
        } else {
            try {
                Files.move(source, dest);
                success = true;
            } catch (final Throwable t) {
                exception = handleRollException(file, destFile, exception, t);
            }
        }
    }

    if (source != null && !success) {
        try {
            int attempt = 1;
            while (!success && attempt <= MAX_FAILED_RENAME_ATTEMPTS) {
                // Try to rename the file to something else.
                final String suffix = StringUtils.leftPad(String.valueOf(attempt), 3, '0');
                final File failedFile = new File(dir, file.getName() + "." + suffix);
                try {
                    dest = null;
                    dest = failedFile.toPath();
                } catch (final Throwable t) {
                    LOGGER.error(t.getMessage(), t);
                }

                if (dest != null && !Files.isRegularFile(dest)) {
                    if (LOGGER.isDebugEnabled()) {
                        LOGGER.debug("Renaming file '%s' to '%s'", getFullPath(file), getFullPath(failedFile));
                    }
                    try {
                        Files.move(source, dest);
                        success = true;
                    } catch (final Throwable t) {
                        LOGGER.debug(t.getMessage(), t);
                    }
                }
                attempt++;
            }

            // If we didn't succeed in renaming the file to a failed file
            // then try and delete the file.
            if (!success) {
                // Try to delete the file so we can continue to create a
                // destination.
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Deleting file '%s'", getFullPath(file));
                }
                if (!Files.deleteIfExists(source)) {
                    LOGGER.error("Failed to delete file '%s'", getFullPath(file));
                }
            }
        } catch (final Throwable t) {
            LOGGER.debug(t.getMessage(), t);
        }
    }

    if (exception != null) {
        throw exception;
    }
}

From source file:stroom.pipeline.server.writer.PathCreator.java

private static String replace(final String path, final String type, final long replacement, final int pad) {
    String value = String.valueOf(replacement);
    if (pad > 0) {
        value = StringUtils.leftPad(value, pad, '0');
    }// w  w w.  j a  v a  2 s. c  o m
    return replace(path, type, value);
}

From source file:stroom.proxy.repo.ProxyRepositoryCreator.java

private void loadInput(final Path file, final boolean mandateEffectiveDate, final Long effectiveMs) {
    // Get the feed.
    final Feed feed = getFeed(file);

    try {/*from w  ww  .jav a 2s. c om*/
        if (feed.isReference() == mandateEffectiveDate) {
            LOGGER.info("Loading data: " + file.toAbsolutePath().toString());

            final StroomZipOutputStream zipOutputStream = repository.getStroomZipOutputStream();

            int i = 0;
            i++;
            String newName = Integer.toString(i);
            newName = StringUtils.leftPad(newName, 3, '0');

            // Add meta data.
            OutputStream zipPart = zipOutputStream
                    .addEntry(new StroomZipEntry(null, newName, StroomZipFileType.Meta));
            final MetaMap map = createMap(feed, effectiveMs);
            map.write(zipPart, true);

            // Add data.
            zipPart = zipOutputStream.addEntry(new StroomZipEntry(null, newName, StroomZipFileType.Data));
            StreamUtil.streamToStream(new BufferedInputStream(Files.newInputStream(file)), zipPart);

            zipOutputStream.close();
        }
    } catch (final IOException e) {
        throw new RuntimeException("Error loading file: " + file.toAbsolutePath().toString(), e);
    }
}

From source file:stroom.proxy.repo.ProxyRepositoryCreator.java

private void loadZip(final Path file, final boolean mandateEffectiveDate, final Long effectiveMs) {
    // Get the feed.
    final Feed feed = getFeed(file);

    if (feed.isReference() == mandateEffectiveDate) {
        LOGGER.info("Loading data: " + file.toAbsolutePath().toString());

        try {/*from   ww w .ja  v  a 2  s. c  om*/
            final StroomZipOutputStream zipOutputStream = repository.getStroomZipOutputStream();

            final StroomZipFile stroomZipFile = new StroomZipFile(file);

            int i = 0;
            for (String baseName : stroomZipFile.getStroomZipNameSet().getBaseNameSet()) {
                i++;
                String newName = Integer.toString(i);
                newName = StringUtils.leftPad(newName, 3, '0');

                // Add meta data.
                InputStream inputStream = stroomZipFile.getInputStream(baseName, StroomZipFileType.Meta);
                final MetaMap map = createMap(feed, effectiveMs);
                if (inputStream != null) {
                    map.read(inputStream, true);
                }
                OutputStream outputStream = zipOutputStream
                        .addEntry(new StroomZipEntry(null, newName, StroomZipFileType.Meta));
                map.write(outputStream, true);

                // Add context data.
                inputStream = stroomZipFile.getInputStream(baseName, StroomZipFileType.Context);
                if (inputStream != null) {
                    outputStream = zipOutputStream
                            .addEntry(new StroomZipEntry(null, newName, StroomZipFileType.Context));
                    StreamUtil.streamToStream(inputStream, outputStream);
                }

                // Add data.
                inputStream = stroomZipFile.getInputStream(baseName, StroomZipFileType.Data);
                if (inputStream != null) {
                    outputStream = zipOutputStream
                            .addEntry(new StroomZipEntry(null, newName, StroomZipFileType.Data));
                    StreamUtil.streamToStream(inputStream, outputStream);
                }
            }

            stroomZipFile.close();
            zipOutputStream.close();

        } catch (final IOException e) {
            throw new RuntimeException("Error loading file: " + file.toAbsolutePath().toString(), e);
        }
    }
}

From source file:stroom.ProxyRepositoryCreator.java

private void loadInput(final File file, final boolean mandateEffectiveDate, final Long effectiveMs) {
    // Get the feed.
    final Feed feed = getFeed(file);

    try {//from w  w  w  .j a v a  2s.  c  o  m
        if (feed.isReference() == mandateEffectiveDate) {
            LOGGER.info("Loading data: " + file.getAbsolutePath());

            final StroomZipOutputStream zipOutputStream = repository.getStroomZipOutputStream();

            int i = 0;
            i++;
            String newName = Integer.toString(i);
            newName = StringUtils.leftPad(newName, 3, '0');

            // Add meta data.
            OutputStream zipPart = zipOutputStream
                    .addEntry(new StroomZipEntry(null, newName, StroomZipFileType.Meta));
            final HeaderMap map = createMap(feed, effectiveMs);
            map.write(zipPart, true);

            // Add data.
            zipPart = zipOutputStream.addEntry(new StroomZipEntry(null, newName, StroomZipFileType.Data));
            StreamUtil.streamToStream(new BufferedInputStream(new FileInputStream(file)), zipPart);

            zipOutputStream.close();
        }
    } catch (final IOException e) {
        throw new RuntimeException("Error loading file: " + file.getAbsolutePath(), e);
    }
}

From source file:stroom.ProxyRepositoryCreator.java

private void loadZip(final File file, final boolean mandateEffectiveDate, final Long effectiveMs) {
    // Get the feed.
    final Feed feed = getFeed(file);

    if (feed.isReference() == mandateEffectiveDate) {
        LOGGER.info("Loading data: " + file.getAbsolutePath());

        try {//from   w w w.  j a v a  2  s.  co m
            final StroomZipOutputStream zipOutputStream = repository.getStroomZipOutputStream();

            final StroomZipFile stroomZipFile = new StroomZipFile(file);

            int i = 0;
            for (String baseName : stroomZipFile.getStroomZipNameSet().getBaseNameSet()) {
                i++;
                String newName = Integer.toString(i);
                newName = StringUtils.leftPad(newName, 3, '0');

                // Add meta data.
                InputStream inputStream = stroomZipFile.getInputStream(baseName, StroomZipFileType.Meta);
                final HeaderMap map = createMap(feed, effectiveMs);
                if (inputStream != null) {
                    map.read(inputStream, true);
                }
                OutputStream outputStream = zipOutputStream
                        .addEntry(new StroomZipEntry(null, newName, StroomZipFileType.Meta));
                map.write(outputStream, true);

                // Add context data.
                inputStream = stroomZipFile.getInputStream(baseName, StroomZipFileType.Context);
                if (inputStream != null) {
                    outputStream = zipOutputStream
                            .addEntry(new StroomZipEntry(null, newName, StroomZipFileType.Context));
                    StreamUtil.streamToStream(inputStream, outputStream);
                }

                // Add data.
                inputStream = stroomZipFile.getInputStream(baseName, StroomZipFileType.Data);
                if (inputStream != null) {
                    outputStream = zipOutputStream
                            .addEntry(new StroomZipEntry(null, newName, StroomZipFileType.Data));
                    StreamUtil.streamToStream(inputStream, outputStream);
                }
            }

            stroomZipFile.close();
            zipOutputStream.close();

        } catch (final IOException e) {
            throw new RuntimeException("Error loading file: " + file.getAbsolutePath(), e);
        }
    }
}