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

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

Introduction

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

Prototype

public static String padStart(String string, int minLength, char padChar) 

Source Link

Document

Returns a string, of length at least minLength , consisting of string prepended with as many copies of padChar as are necessary to reach that length.

Usage

From source file:com.nestedbird.models.occurrence.Occurrence.java

@Override
public String getUrl() {
    String name = "e";
    try {/*from  w w  w.  j  a va  2 s.  co  m*/
        name = URLEncoder.encode(getEvent().map(Event::getName).map(newName -> newName.replace(" ", "_"))
                .map(newName -> newName.replace("/", "")).map(newName -> Strings.padStart(newName, 1, 'e'))
                .orElse("#"), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        logger.info("[Occurrence] [getUrl] Failure To Encode URL", e);
    }

    return String.format("/Events/%s/%s?startTime=%s", getEvent().map(Event::getIdBase64).orElse(""), name, // uuidToBase64(getEvent().getId()),
            String.valueOf(getStartTime().getMillis()));
}

From source file:net.awairo.mcmod.common.v1.util.Colors.java

private static String hexToString(int hexInt) {
    return Strings.padStart(Integer.toHexString(hexInt), 2, '0');
}

From source file:net.minecrell.serverlistplus.core.favicon.FaviconHelper.java

private static String toHex(long num) {
    return Strings.padStart(Long.toHexString(num), 16, '0');
}

From source file:com.nestedbird.modules.facebookreader.FacebookPoster.java

private String createMessage(final Occurrence occurrence) {
    String eventArtists = occurrence.getEvent().map(Event::getAllArtistNames).orElse(new ArrayList<>()).stream()
            .collect(Collectors.joining(", "));

    if (eventArtists.lastIndexOf(',') > 0) {
        int start = eventArtists.lastIndexOf(',');
        eventArtists = eventArtists.substring(0, start) + " and" + eventArtists.substring(start + 1);
    }/*from  w ww.java  2 s.co m*/

    final String venueName = occurrence.getEvent().flatMap(Event::getLocation).map(Location::getName)
            .orElse("");
    final String facebookId = occurrence.getEvent().flatMap(Event::getLocation).map(Location::getFacebookId)
            .map(String::valueOf).orElse("");
    String venue = venueName;

    if (facebookId.length() > 0) {
        venue = "@[" + facebookId + ":1:" + venueName + "]";
    }

    return generateMessage(eventArtists, venue,
            Strings.padStart(String.valueOf(occurrence.getStartTime().getHourOfDay()), 2, '0') + ":"
                    + Strings.padStart(String.valueOf(occurrence.getStartTime().getMinuteOfHour()), 2, '0'));
}

From source file:org.spongepowered.asm.mixin.transformer.MethodMapper.java

/**
 * Finagle a string from an index thingummy, for science, you monster
 * /*from  www .ja v a  2 s .c om*/
 * @param index a positive number
 * @return unique identifier string of some kind
 */
private static String finagle(int index) {
    String hex = Integer.toHexString(index);
    StringBuilder sb = new StringBuilder();
    for (int pos = 0; pos < hex.length(); pos++) {
        char c = hex.charAt(pos);
        sb.append(c += c < 0x3A ? 0x31 : 0x0A);
    }
    return Strings.padStart(sb.toString(), 3, 'z');
}

From source file:org.openengsb.core.ekb.transformation.wonderland.internal.operation.PadOperation.java

/**
 * Perform the pad operation itself and returns the result
 */// w  w w  .j  av a2 s . c  o m
private String performPadOperation(String source, Integer length, Character padChar, String direction) {
    if (direction.equals("Start")) {
        return Strings.padStart(source, length, padChar);
    } else {
        return Strings.padEnd(source, length, padChar);
    }
}

From source file:org.robotframework.ide.eclipse.main.plugin.tableeditor.variables.ListVariableDetailCellEditorEntry.java

void setIndex(final int allElements, final int index) {
    final int maxElementLength = (int) Math.ceil(Math.log10(allElements));
    indexText = "[" + Strings.padStart(Integer.toString(index), maxElementLength, '0') + "]";
}

From source file:de.huberlin.german.korpling.laudatioteitool.TEIValidator.java

public static String formatParserExceptions(Errors errors) {
    if (errors.isEmpty()) {
        return "";
    }/*from w  ww . ja  va 2 s .c  o m*/

    StringBuilder sb = new StringBuilder();

    for (Map.Entry<File, List<SAXParseException>> entry : errors.entrySet()) {
        List<String> linesRaw = new LinkedList<String>();
        try {
            // split lines so we can reuse them later
            linesRaw = Files.readLines(entry.getKey(), Charsets.UTF_8);
        } catch (IOException ex) {
            log.error("UTF-8 is an unknown encoding on this computer", entry.getKey());
        }
        String[] lines = linesRaw.toArray(new String[linesRaw.size()]);

        String header = entry.getKey().getPath() + " has " + entry.getValue().size()
                + (entry.getValue().size() > 1 ? " errors" : " error");
        sb.append(header).append("\n");

        sb.append(Strings.repeat("=", header.length())).append("\n");

        ListIterator<SAXParseException> it = entry.getValue().listIterator();
        while (it.hasNext()) {
            SAXParseException ex = it.next();

            int columnNr = ex.getColumnNumber();
            int lineNr = ex.getLineNumber();

            sb.append("[line ").append(lineNr).append("/column ").append(columnNr).append("]").append("\n");
            String caption = ex.getLocalizedMessage();

            sb.append(caption).append("\n");

            // output complete affected line 
            String line = lines[lineNr - 1];
            sb.append(line).append("\n");

            // output a marker for the columns
            sb.append(Strings.padStart("^", columnNr, ' ')).append("\n");

            sb.append(Strings.repeat("-", Math.min(80, Math.max(caption.length(), line.length()))))
                    .append("\n");
        }
    }

    return sb.toString();
}

From source file:graph.features.shortestPath.ShortestPath.java

public void debug(final PathInterface<T>[][] array) {
    double max = 0;
    final int order = this.getGraph().getOrder();
    for (int i = 0; i < order; ++i)
        for (int j = 0; j < order; ++j)
            if (i != j && array[i][j] != null && array[i][j].getWeight() > max)
                max = array[i][j].getWeight();
    final int n = (int) Math.floor(Math.log10(Double.valueOf(max).intValue())) + 1;
    for (int i = 0; i < order; ++i) {
        for (int j = 0; j < order; ++j) {
            String string;// ww w.j av a 2  s.co m
            if (i == j)
                string = Strings.repeat(".", n);
            else if (array[i][j] == null)
                string = Strings.repeat("X", n);
            else
                string = Strings.padStart(String.valueOf((int) array[i][j].getWeight()), n, '0');
            System.out.print(string + " ");
        }
        System.out.println();
    }
    System.out.println();
}

From source file:com.axelor.dms.db.repo.DMSFileRepository.java

@Override
public DMSFile save(DMSFile entity) {

    final DMSFile parent = entity.getParent();
    final Model related = findRelated(entity);
    final boolean isAttachment = related != null && entity.getMetaFile() != null;

    // if new attachment, save attachment reference
    if (isAttachment) {
        // remove old attachment if file is moved
        MetaAttachment attachmentOld = attachments.all()
                .filter("self.metaFile.id = ? AND self.objectId != ? AND self.objectName != ?",
                        entity.getMetaFile().getId(), related.getId(), related.getClass().getName())
                .fetchOne();// ww  w  .java  2 s .c o m
        if (attachmentOld != null) {
            System.err.println("OLD: " + attachmentOld);
            attachments.remove(attachmentOld);
        }

        MetaAttachment attachment = attachments.all()
                .filter("self.metaFile.id = ? AND self.objectId = ? AND self.objectName = ?",
                        entity.getMetaFile().getId(), related.getId(), related.getClass().getName())
                .fetchOne();
        if (attachment == null) {
            attachment = metaFiles.attach(entity.getMetaFile(), related);
            attachments.save(attachment);
        }
    }

    // if not an attachment or has parent, do nothing
    if (parent != null || related == null) {
        return super.save(entity);
    }

    // create parent folders

    Mapper mapper = Mapper.of(related.getClass());
    String homeName = null;
    try {
        homeName = mapper.getNameField().get(related).toString();
    } catch (Exception e) {
    }
    if (homeName == null) {
        homeName = Strings.padStart("" + related.getId(), 5, '0');
    }

    DMSFile dmsRoot = all().filter(
            "(self.relatedId is null OR self.relatedId = 0) AND self.relatedModel = ? and self.isDirectory = true",
            entity.getRelatedModel()).fetchOne();

    final Inflector inflector = Inflector.getInstance();

    if (dmsRoot == null) {
        dmsRoot = new DMSFile();
        dmsRoot.setFileName(inflector.pluralize(inflector.humanize(related.getClass().getSimpleName())));
        dmsRoot.setRelatedModel(entity.getRelatedModel());
        dmsRoot.setIsDirectory(true);
        dmsRoot = super.save(dmsRoot); // should get id before it's child
    }

    DMSFile dmsHome = new DMSFile();
    dmsHome.setFileName(homeName);
    dmsHome.setRelatedId(entity.getRelatedId());
    dmsHome.setRelatedModel(entity.getRelatedModel());
    dmsHome.setParent(dmsRoot);
    dmsHome.setIsDirectory(true);
    dmsHome = super.save(dmsHome); // should get id before it's child

    entity.setParent(dmsHome);

    return super.save(entity);
}