Example usage for java.lang StringBuilder charAt

List of usage examples for java.lang StringBuilder charAt

Introduction

In this page you can find the example usage for java.lang StringBuilder charAt.

Prototype

char charAt(int index);

Source Link

Document

Returns the char value at the specified index.

Usage

From source file:gov.jgi.meta.sequence.SequenceStringCompress.java

/**
 * The real method to do sequenceToBytearray
 * The logic has changed.  --lanhin// w  w  w . ja v  a2s  .c  o m
 */
private static byte[] pack(String sequenceToPack) {
    int numberOfBases = sequenceToPack.length();
    int numberOfFill = (3 - numberOfBases % 3) % 3;
    int numberOfBytes = (int) Math.ceil((2 + Math.ceil((double) numberOfBases / 3) * 7) / 8);//Wish I didn't make a mistake here  --lanhin
    int numberOfBaseGroup = (numberOfBases + numberOfFill) / 3;

    byte[] bytes = new byte[numberOfBytes];
    int leftBits, position = 0;// Position index in bytes[]

    byte former, latter, current;

    String newSequence = sequenceToPack;
    //Fill by 'n'
    for (int i = 0; i < numberOfFill; i++) {
        newSequence = newSequence + 'n';
    }

    former = (byte) (((byte) numberOfFill) << 6);//Record this var in the very first two bits.
    leftBits = 6;

    try {
        for (int i = 0; i < numberOfBaseGroup; i++) {

            StringBuilder subseq = new StringBuilder(newSequence.substring(i * 3, i * 3 + 3));
            for (int si = 0; si < 3; si++) {
                char sichar = subseq.charAt(si);
                if (sichar != 'a' && sichar != 't' && sichar != 'g' && sichar != 'c' && sichar != 'n') {
                    subseq.setCharAt(si, 'n');
                }
            }
            current = hash.get(subseq.toString());
            if (leftBits == 8)
                former = (byte) (current << 1);
            else
                former = (byte) ((current >> (7 - leftBits)) | former);
            latter = (byte) (current << (1 + leftBits));
            if (leftBits <= 7) {//A new byte produced
                bytes[position++] = former;
                former = latter;
            }
            leftBits = (1 + leftBits) % 8;
            if (leftBits == 0) {
                leftBits = 8;
            }
        }
        if (leftBits < 8)//Fill the last one byte
            bytes[position++] = former;

    } catch (Exception e) {
        System.out.println("Pack Error, Please Check." + e);
    }

    // sanity check
    assert (position == numberOfBytes);

    return bytes;
}

From source file:jongo.JongoUtils.java

/**
 * Generates a string like {call stmt(?,?,?)} to be used by a CallableStatement to execute a function
 * or a procedure./*from ww w.  jav  a 2  s.  c  o m*/
 * @param queryName the name of the function or procedure
 * @param paramsSize the amount of parameters to be passed to the function/procedure
 * @return a string ready to be fed to a CallableStatement
 */
public static String getCallableStatementCallString(final String queryName, final Integer paramsSize) {
    if (StringUtils.isBlank(queryName))
        throw new IllegalArgumentException("The name can't be null, empty or blank");

    StringBuilder b = new StringBuilder("{CALL ");
    b.append(queryName);
    b.append("(");
    for (int i = 0; i < paramsSize; i++) {
        b.append("?,");
    }
    if (b.charAt(b.length() - 1) == ',') {
        b.deleteCharAt(b.length() - 1);
    }
    b.append(")}");
    return b.toString();
}

From source file:org.apache.phoenix.util.StringUtil.java

/**
 * Lame - StringBuilder.equals is retarded.
 * @param b1/*from w ww . j a  v a 2s  .  c  o m*/
 * @param b2
 * @return whether or not the two builders consist the same sequence of characters
 */
public static boolean equals(StringBuilder b1, StringBuilder b2) {
    if (b1.length() != b2.length()) {
        return false;
    }
    for (int i = 0; i < b1.length(); i++) {
        if (b1.charAt(i) != b2.charAt(i)) {
            return false;
        }
    }
    return true;
}

From source file:com.baidubce.util.HttpUtils.java

/**
 * Append the given path to the given baseUri.
 *
 * <p>//w  ww.ja  v  a 2s  . c  o  m
 * This method will encode the given path but not the given baseUri.
 *
 * @param baseUri
 * @param pathComponents
 */
public static URI appendUri(URI baseUri, String... pathComponents) {
    StringBuilder builder = new StringBuilder(baseUri.toASCIIString());
    for (String path : pathComponents) {
        if (path != null && path.length() > 0) {
            path = normalizePath(path);
            if (path.startsWith("/")) {
                if (builder.charAt(builder.length() - 1) == '/') {
                    builder.setLength(builder.length() - 1);
                }
            } else {
                if (builder.charAt(builder.length() - 1) != '/') {
                    builder.append('/');
                }
            }
            builder.append(path);
        }
    }
    try {
        return new URI(builder.toString());
    } catch (URISyntaxException e) {
        throw new RuntimeException("Unexpected error", e);
    }
}

From source file:Main.java

public static Pair<String, String> ReorderRTLTextForPebble(String source, int charLimit) {
    if (source == null || source.equals(""))
        return new Pair<>("", "");

    if (!hebrewPattern.matcher(source).find()) {
        return new Pair<>(source, "");
    } else {/* www .  jav a  2  s. c  o m*/

        StringBuilder sbResult = new StringBuilder();
        StringBuilder sbExtraResult = new StringBuilder();
        StringBuilder sbTemp = new StringBuilder();
        String[] words = source.split(" ");
        int charCount = 0;

        for (int wordIndex = 0; wordIndex < words.length; wordIndex++, sbTemp.setLength(0)) {
            sbTemp.append(words[wordIndex]);
            charCount += sbTemp.length();
            Matcher hebrewWord = hebrewPattern.matcher(words[wordIndex]);

            if (hebrewWord.find()) {
                sbTemp.reverse();
                if (sbTemp.charAt(0) == ')') {
                    sbTemp.replace(0, 1, "(");
                }
                if (sbTemp.charAt(sbTemp.length() - 1) == '(') {
                    sbTemp.replace(sbTemp.length() - 1, sbTemp.length(), ")");
                }
            }

            if (charCount <= charLimit) {
                sbResult.insert(0, sbTemp + " ");
            } else {
                sbExtraResult.insert(0, sbTemp + " ");
            }

            charCount++;
        }

        if (sbExtraResult.length() > charLimit) {
            sbExtraResult.replace(0, sbExtraResult.length() - charLimit, "...");
        }

        return new Pair<>(sbResult.toString().trim(), sbExtraResult.toString().trim());
    }
}

From source file:com.laxser.blitz.util.PlaceHolderUtils.java

public static String resolve(String text, Invocation inv) {
    if (StringUtils.isEmpty(text)) {
        return text;
    }//from   ww  w.  j av a  2  s  .c o  m
    int startIndex = text.indexOf(PLACEHOLDER_PREFIX);
    if (startIndex == -1) {
        return text;
    }
    StringBuilder buf = new StringBuilder(text);
    while (startIndex != -1) {
        int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length());
        if (endIndex != -1) {
            String placeholder = null;
            String defaultValue = null;
            for (int i = startIndex + PLACEHOLDER_PREFIX.length(); i < endIndex; i++) {
                if (buf.charAt(i) == '?') {
                    placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), i);
                    defaultValue = buf.substring(i + 1, endIndex);
                    break;
                }
            }
            if (placeholder == null) {
                placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex);
            }
            int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length();
            try {
                int dot = placeholder.indexOf('.');
                String attributeName = dot == -1 ? placeholder : placeholder.substring(0, dot);
                String propertyPath = dot == -1 ? "" : placeholder.substring(dot + 1);
                Object propVal = inv.getModel().get(attributeName);
                if (propVal != null) {
                    if (propertyPath.length() > 0) {
                        propVal = new BeanWrapperImpl(propVal).getPropertyValue(propertyPath);
                    }
                } else {
                    if ("flash".equals(attributeName)) {
                        propVal = inv.getFlash().get(propertyPath);
                    } else {
                        propVal = inv.getParameter(placeholder);
                    }
                }
                //
                if (propVal == null) {
                    propVal = defaultValue;
                }
                if (propVal == null) {
                    if (logger.isWarnEnabled()) {
                        logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "].");
                    }
                } else {
                    String toString = propVal.toString();
                    buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), toString);
                    nextIndex = startIndex + toString.length();
                }
            } catch (Throwable ex) {
                logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "] : " + ex);
            }
            startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex);
        } else {
            startIndex = -1;
        }
    }

    return buf.toString();
}

From source file:com.yunmel.syncretic.utils.commons.StrUtils.java

/**
 * ?//w  ww.ja  v  a  2  s  .  c  o m
 */
public static String camelhumpToUnderline(String str) {
    final int size;
    final char[] chars;
    final StringBuilder sb = new StringBuilder((size = (chars = str.toCharArray()).length) * 3 / 2 + 1);
    char c;
    for (int i = 0; i < size; i++) {
        c = chars[i];
        if (isLowercaseAlpha(c)) {
            sb.append(toUpperAscii(c));
        } else {
            sb.append('_').append(c);
        }
    }
    return sb.charAt(0) == '_' ? sb.substring(1) : sb.toString();
}

From source file:com.github.dryangkun.hbase.tidx.hive.HBaseSerDeHelper.java

/**
 * Trims by removing the trailing "," if any
 * /*from  w w  w.  j  av a2 s.co  m*/
 * @param sb StringBuilder to trim
 * @return StringBuilder trimmed StringBuilder
 * */
private static StringBuilder trim(StringBuilder sb) {
    if (sb.charAt(sb.length() - 1) == StringUtils.COMMA) {
        return sb.deleteCharAt(sb.length() - 1);
    }

    return sb;
}

From source file:info.magnolia.cms.security.SecurityUtil.java

public static String byteArrayToHex(byte[] raw) {
    if (raw == null) {
        return null;
    }/*from ww w  .j  a  va 2  s  .c om*/
    final StringBuilder hex = new StringBuilder(2 * raw.length);
    for (final byte b : raw) {
        hex.append(HEX.charAt((b & 0xF0) >> 4)).append(HEX.charAt((b & 0x0F)));
    }
    return hex.toString();
}

From source file:org.commonjava.maven.galley.util.UrlUtils.java

public static String buildUrl(final String baseUrl, final Map<String, String> params, final String... parts)
        throws MalformedURLException {
    if (parts == null || parts.length < 1) {
        return baseUrl;
    }/*from   w w  w . j  a va 2 s  .  c om*/

    final StringBuilder urlBuilder = new StringBuilder();

    if (parts[0] == null || !parts[0].startsWith(baseUrl)) {
        urlBuilder.append(baseUrl);
    }

    for (String part : parts) {
        if (part == null || part.trim().length() < 1) {
            continue;
        }

        if (part.startsWith("/")) {
            part = part.substring(1);
        }

        if (urlBuilder.length() > 0 && urlBuilder.charAt(urlBuilder.length() - 1) != '/') {
            urlBuilder.append("/");
        }

        urlBuilder.append(part);
    }

    if (params != null && !params.isEmpty()) {
        urlBuilder.append("?");
        boolean first = true;
        for (final Map.Entry<String, String> param : params.entrySet()) {
            if (first) {
                first = false;
            } else {
                urlBuilder.append("&");
            }

            urlBuilder.append(param.getKey()).append("=").append(param.getValue());
        }
    }

    return new URL(urlBuilder.toString()).toExternalForm();
}