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:nl.knaw.huygens.timbuctoo.model.util.Period.java

private static Datable convert(String value) {
    String edtf = (value.length() < 4 && value.matches("\\d+")) ? Strings.padStart(value, 4, '0') : value;
    return new Datable(edtf);
}

From source file:net.kaikk.mc.DimRestrictor.DimRestrict.java

static String timeToString(int time) {
    int hours = time / 60;
    int mins = time % 60;
    return Strings.padStart(Integer.toString(hours), 2, '0') + ":"
            + Strings.padStart(Integer.toString(mins), 2, '0');
}

From source file:net.minecrell.serverlistplus.core.util.UUIDs.java

private static String toHexString(long unsigned) {
    return Strings.padStart(UnsignedLongs.toString(unsigned, 16), 16, '0');
}

From source file:com.outerspacecat.util.Totp.java

/**
 * Generates a six digit time-based one-time password using the TOTP
 * algorithm./*from  w w  w  .j  a v a  2 s  . c o  m*/
 * 
 * @param k the secret key to use for the HMAC with SHA1 function. Must be non
 *        {@code null}, may be any length.
 * @param t a timestamp in seconds relative to the UNIX epoch
 * @return a six digit one-time password. Never {@code null}.
 */
public static String totp(final byte[] k, final long t) {
    long steps = t / 30;

    byte[] c = new byte[] { (byte) (steps >>> 56), (byte) (steps >>> 48), (byte) (steps >>> 40),
            (byte) (steps >>> 32), (byte) (steps >>> 24), (byte) (steps >>> 16), (byte) (steps >>> 8),
            (byte) steps };

    byte[] hs = Utils.hmacSha1(c, k);

    int offset = 0x0F & hs[hs.length - 1];

    int binary = ((0x7F & hs[offset]) << 24) | ((0xFF & hs[offset + 1]) << 16) | ((0xFF & hs[offset + 2]) << 8)
            | (0xFF & hs[offset + 3]);

    int ret = binary % 1000000;

    return Strings.padStart(String.valueOf(ret), 6, '0');
}

From source file:pl.nn44.battleship.util.id.BigIdGenerator.java

@Override
public String nextId() {
    String nextId = new BigInteger(bits, random).toString(NUMBER_BASE);
    return Strings.padStart(nextId, chars, '0');
}

From source file:eu.thebluemountain.customers.dctm.brownbag.badcontentslister.Store.java

/**
 * The method that returns a new store/*from w  w  w  . ja  va  2  s.  c  o  m*/
 * @param id carries the store's identifier
 * @param name carries the unique store's name
 * @param path carries the unique store's path
 * @param extension indicates whether the store contains files with their
 *                  format extension
 * @return the matching store
 */
public static Store create(String id, String name, String path, boolean extension) {
    Preconditions.checkNotNull(id);
    Preconditions.checkNotNull(name);
    Preconditions.checkNotNull(path);
    // we recompute the path to carry the docbase identifier on
    // 0-left-padded 8 length.
    final String dbid = Strings.padStart(id.substring(2, 8), 8, '0');
    path = path + "/" + dbid;
    return new Store(id, name, path, extension);
}

From source file:org.glowroot.transaction.model.TraceUniqueId.java

@VisibleForTesting
static String twelveDigitHex(long x) {
    String s = BigInteger.valueOf(x).toString(16);
    if (s.length() == 11) {
        // this is the case for dates between Nov 3, 2004 and Jun 23, 2527 so it seems worth
        // optimizing :-)
        return '0' + s;
    }/*from  w ww .j  a  v a 2  s . co  m*/
    if (s.length() == 12) {
        return s;
    }
    if (s.length() > 12) {
        // Aug 1 of the year 10889 and beyond!
        return s.substring(s.length() - 12);
    }
    // living in the past?
    return Strings.padStart(s, 12, '0');
}

From source file:com.axelor.db.JpaSequence.java

/**
 * Get the next sequence value of the given sequence.<br>
 * <br>//from   w w  w  .  j a v a2  s.com
 * This method must be called inside a running transaction as it updates the
 * sequence details in database.
 * 
 * @param name
 *            the name of the sequence
 * @return next sequence value
 */
public static String nextValue(String name) {
    final MetaSequence sequence = find(name);
    final Long next = sequence.getNext();
    final String prefix = sequence.getPrefix();
    final String suffix = sequence.getSuffix();
    final Integer padding = sequence.getPadding();

    String value = "" + next;
    if (padding > 0) {
        value = Strings.padStart(value, padding, '0');
    }
    if (!StringUtils.isBlank(prefix)) {
        value = prefix + value;
    }
    if (!StringUtils.isBlank(suffix)) {
        value = value + suffix;
    }

    sequence.setNext(next + sequence.getIncrement());

    JPA.em().persist(sequence);

    return value;
}

From source file:eu.numberfour.n4js.antlr.UnicodeKeywordHelper.java

/**
 * Creates lexer rule for given keyword in order to handle JavaScript's unicode masks. E.g.:
 *
 * <pre>//from  w w w.j a  v  a 2s.  c om
 * If :
 *    ( 'i' | '\\' 'u' '0''0''6''9' )
 *    ( 'f' | '\\' 'u' '0''0''6''6' );
 * </pre>
 *
 * @param keyword
 *            the keyword as string, e.g. 'if'
 * @return the lexer body, e.g.
 *
 *         <pre>
 * ( 'i' | '\\' 'u' '0''0''6''9' ) ( 'f' | '\\' 'u' '0''0''6''6' )
 *         </pre>
 */
public static String toUnicodeKeyword(String keyword) {
    if (keyword.equals("async ")) {
        keyword = "async";
    }
    if (isIdentifier(keyword)) {
        StringBuilder result = new StringBuilder(keyword.length() * 30);
        for (char c : keyword.toCharArray()) {
            result.append("\n\t( '");
            result.append(c);
            result.append("' | '\\\\' 'u' ");
            String unicodeEscape = Strings.padStart(Integer.toHexString(c), 4, '0');
            for (char u : unicodeEscape.toCharArray()) {
                if ('0' <= u && u <= '9') {
                    result.append("'");
                    result.append(u);
                    result.append("'");
                } else {
                    result.append("( '");
                    result.append(u);
                    result.append("' | '");
                    result.append(Character.toUpperCase(u));
                    result.append("' )");
                }
            }
            result.append(" )");
        }
        return result.toString();
    }
    return "'" + AntlrGrammarGenUtil.toAntlrString(keyword) + "'";
}

From source file:com.outerspacecat.json.Json.java

/**
 * Escapes {@code seq} according to JSON string escaping rules. The returned
 * value will not be surrounded with quotation marks ('&quot;', U+0022).
 * Invalid UTF-16 is not reported or fixed.
 * <p>/*w ww  . j  a  va2  s.c o  m*/
 * Characters are escaped by replacing them with a Unicode escape sequence
 * consisting of a reverse solidus ('\', U+005C), followed by a latin small
 * letter 'u' ('u', U+0075), followed by four hexadecimal digits that
 * represent a Unicode code point.
 * <p>
 * The following characters will escaped.
 * <ol>
 * <li>Controls (U+0000-U+001F).</li>
 * <li>Quotation mark ('&quot;', U+0022).</li>
 * <li>Reverse solidus ('\', U+005C).</li>
 * <li>Surrogates (U+D800-U+DFFF).</li>
 * </ol>
 * 
 * @param seq the character sequence to escape. Must be non {@code null}.
 * @return an escaped character sequence. Never {@code null}.
 */
public static String escapeString(final CharSequence seq) {
    Preconditions.checkNotNull(seq, "seq required");

    StringBuilder ret = new StringBuilder();

    for (int i = 0; i < seq.length(); ++i) {
        char c = seq.charAt(i);
        if ((c >= 0x00 && c <= 0x1F) || c == 0x22 || c == 0x5C || (c > 0xD800 && c <= 0xDFFF)) {
            ret.append("\\u");
            ret.append(Strings.padStart(Integer.toHexString(c), 4, '0'));
        } else {
            ret.append(c);
        }
    }

    return ret.toString();
}