Example usage for java.lang StringBuilder reverse

List of usage examples for java.lang StringBuilder reverse

Introduction

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

Prototype

@Override
    public StringBuilder reverse() 

Source Link

Usage

From source file:haven.Utils.java

public static String fpformat(int num, int div, int dec) {
    StringBuilder buf = new StringBuilder();
    boolean s = false;
    if (num < 0) {
        num = -num;//  w  w  w.  j a  v  a 2s  .  c  o  m
        s = true;
    }
    for (int i = 0; i < div - dec; i++)
        num /= 10;
    for (int i = 0; i < dec; i++) {
        buf.append((char) ('0' + (num % 10)));
        num /= 10;
    }
    buf.append('.');
    if (num == 0) {
        buf.append('0');
    } else {
        while (num > 0) {
            buf.append((char) ('0' + (num % 10)));
            num /= 10;
        }
    }
    if (s)
        buf.append('-');
    return (buf.reverse().toString());
}

From source file:com.docdoku.core.util.Tools.java

public static String increaseId(String id, String mask) throws ParseException {
    LOGGER.info("#### Tools.increaseId id = " + id + " , mask = " + mask);
    MaskFormatter formatter = new MaskFormatter(mask);
    formatter.setValueContainsLiteralCharacters(false);
    String value = formatter.stringToValue(id).toString();
    StringBuilder newValue = new StringBuilder();
    boolean increase = true;
    for (int i = value.length() - 1; i >= 0; i--) {
        char c = value.charAt(i);
        switch (c) {
        case '9':
            newValue.append((increase) ? '0' : '9');
            break;

        case '8':
            newValue.append((increase) ? '9' : '8');
            increase = false;/*from  w w w. j  a va  2s . c o m*/
            break;

        case '7':
            newValue.append((increase) ? '8' : '7');
            increase = false;
            break;

        case '6':
            newValue.append((increase) ? '7' : '6');
            increase = false;
            break;

        case '5':
            newValue.append((increase) ? '6' : '5');
            increase = false;
            break;

        case '4':
            newValue.append((increase) ? '5' : '4');
            increase = false;
            break;

        case '3':
            newValue.append((increase) ? '4' : '3');
            increase = false;
            break;

        case '2':
            newValue.append((increase) ? '3' : '2');
            increase = false;
            break;

        case '1':
            newValue.append((increase) ? '2' : '1');
            increase = false;
            break;

        case '0':
            newValue.append((increase) ? '1' : '0');
            increase = false;
            break;

        default:
            newValue.append(c);
            break;
        }
    }
    return formatter.valueToString(newValue.reverse().toString());
}

From source file:Ternary.java

public String toString() {
    if (trits.size() == 0)
        return Trit.NEUTRAL.toString();

    StringBuilder sb = new StringBuilder();
    for (Trit trit : trits)
        sb.append(trit);//from w  w  w  . j a v a 2  s  .c  o  m

    return sb.reverse().toString();
}

From source file:org.ala.spatial.util.AnalysisJobMaxent.java

private String getMaxentError(File file, int count) {
    try {/*from   w  ww  .  j  a v a  2  s  . c o m*/
        RandomAccessFile rf = new RandomAccessFile(file, "r");

        // first check if maxent threw a 'No species selected' error
        String nosp = rf.readLine(); // first line: date/time
        nosp = rf.readLine(); // second line: maxent version
        nosp = rf.readLine(); // third line: "No species selected"
        if (nosp.equals("No species selected")) {
            return "No species selected";
        }

        long flen = file.length() - 1;
        int nlcnt = -1;
        StringBuilder lines = new StringBuilder();
        while (nlcnt != count) {
            rf.seek(flen--);
            char c = (char) rf.read();
            lines.append(c);
            if (c == '\n') {
                nlcnt++;
            }

        }
        String line = lines.reverse().toString();
        if (line.contains("Warning: Skipping species because it has 0 test samples")) {
            return "Warning: Skipping species because it has 0 test samples";
        }

        rf.close();
    } catch (Exception e) {
        System.out.println("Unable to read lines");
        e.printStackTrace(System.out);
    }

    // return false anyways
    return null;
}

From source file:org.jamwiki.parser.jflex.HtmlLinkTag.java

/**
 * Returns any trailing period, comma, semicolon, or colon characters
 * from the given string.  This method is useful when parsing raw HTML
 * links, in which case trailing punctuation must be removed.  Note that
 * only punctuation that is not previously matched is trimmed - if the
 * input is "http://example.com/page_(page)" then the trailing parantheses
 * will not be trimmed.//ww w  .j  a  v a2 s. c om
 *
 * @param text The text from which trailing punctuation should be returned.
 * @return Any trailing punctuation from the given text, or an empty string
 *  otherwise.
 */
private String extractTrailingPunctuation(String text) {
    if (StringUtils.isBlank(text)) {
        return "";
    }
    StringBuilder buffer = new StringBuilder();
    for (int i = text.length() - 1; i >= 0; i--) {
        char c = text.charAt(i);
        if (c == '.' || c == ';' || c == ',' || c == ':' || c == '(' || c == '[' || c == '{') {
            buffer.append(c);
            continue;
        }
        // if the value ends with ), ] or } then strip it UNLESS there is a matching
        // opening tag
        if (c == ')' || c == ']' || c == '}') {
            String closeChar = String.valueOf(c);
            String openChar = (c == ')') ? "(" : ((c == ']') ? "[" : "{");
            int pos = Utilities.findMatchingStartTag(text, i, openChar, closeChar);
            if (pos == -1) {
                buffer.append(c);
                continue;
            }
        }
        break;
    }
    if (buffer.length() == 0) {
        return "";
    }
    buffer = buffer.reverse();
    return buffer.toString();
}

From source file:org.apache.solr.handler.component.RuleManagerComponent.java

/**
 * Resets a string builder so it can be reused.
 * @param stringBuilder String builder to reuse.
 * @return new empty string builder based on the one provided.
 *///  w  w  w  .  j a v a2 s.  c o m
private StringBuilder reuseStringBuilder(StringBuilder stringBuilder) {
    stringBuilder.reverse().setLength(0);
    return stringBuilder;
}

From source file:org.nmdp.ngs.reads.GeneratePairedEndReads.java

private void write(final int start, final int end, final boolean reverse, final int count, final String suffix,
        final Appendable appendable) {
    // truncate on both ends
    int s = Math.max(1, start);
    int e = Math.min(reference.length() + 1, end);
    SymbolList read = reference.subList(s, e - 1);

    // mutate and build sequence
    StringBuilder sequence = new StringBuilder();
    for (Iterator<Symbol> i = read.iterator(); i.hasNext();) {
        Symbol symbol = i.next();// w  w w  . j  a v a 2s . co m
        if (random.nextDouble() < mutationRate) {
            for (Iterator<Symbol> j = mutation.mutate(symbol).iterator(); j.hasNext();) {
                Symbol mutated = j.next();
                try {
                    sequence.append(DNATools.dnaToken(mutated));
                } catch (IllegalSymbolException ex) {
                    // ignore
                }
            }
        } else {
            try {
                sequence.append(DNATools.dnaToken(symbol));
            } catch (IllegalSymbolException ex) {
                // ignore
            }
        }
    }

    if (reverse) {
        // reverse orientation
        sequence.reverse();

        // ...and reverse complement
        for (int index = 0; index < sequence.length(); index++) {
            if (sequence.charAt(index) == 'a') {
                sequence.setCharAt(index, 't');
            } else if (sequence.charAt(index) == 't') {
                sequence.setCharAt(index, 'a');
            } else if (sequence.charAt(index) == 'g') {
                sequence.setCharAt(index, 'c');
            } else if (sequence.charAt(index) == 'c') {
                sequence.setCharAt(index, 'g');
            }
        }
    }

    // apply quality scores
    StringBuilder qualityScores = new StringBuilder();
    for (int i = 0, size = sequence.length(); i < size; i++) {
        double qualityScore = quality.qualityScore(i, size);
        qualityScores
                .append(variant.quality(Math.max(Math.min((int) qualityScore, variant.maximumQualityScore()),
                        variant.minimumQualityScore())));
    }

    // write fastq
    Fastq fastq = new FastqBuilder().withVariant(variant)
            .withDescription(reference.getName() + "-" + count + suffix).withSequence(sequence.toString())
            .withQuality(qualityScores.toString()).build();

    try {
        writer.append(appendable, fastq);

        if (appendable instanceof Flushable) {
            ((Flushable) appendable).flush();
        }
    } catch (IOException ex) {
        // ignore
    }

    // update coverage
    coverage.add(reference, s, Math.min(reference.length() + 1, s + read.length()));
}

From source file:com.alu.e3.logger.LogCollector.java

private static String getTailOfFile(File file, int numLines) throws FileNotFoundException, IOException {
    if (numLines < 0) {
        return null;
    } else if (numLines == 0) {
        return "";
    }/*from  w  ww  .j  a va2s. com*/
    java.io.RandomAccessFile raFile = new java.io.RandomAccessFile(file, "r");
    long fileLength = file.length() - 1;
    StringBuilder sb = new StringBuilder();
    int line = 0;

    for (long filePointer = fileLength; filePointer >= 0; filePointer--) {
        raFile.seek(filePointer);
        int readByte = raFile.readByte();

        if (readByte == 0xA) {
            if (filePointer < fileLength) {
                line = line + 1;
                if (line >= numLines) {
                    break;
                }
            }
        }
        sb.append((char) readByte);
    }

    String lastLines = sb.reverse().toString();
    return lastLines;
}

From source file:com.meiah.core.util.StringUtil.java

/**
  * ??????/*from   w  ww . ja v  a  2 s .c o  m*/
  * @param postUrl
  * @return
  */
 public static String substringUrl(String postUrl) {
     if (StringUtils.isBlank(postUrl)) {// 
         return postUrl;
     }

     // ?.????????://??. modified by xiegh, 2012/11/12
     int beginIndex = StringUtils.indexOf(postUrl, "://");
     beginIndex = (beginIndex == -1) ? 0 : (beginIndex + 3);

     int endIndex = postUrl.indexOf("/", beginIndex);
     StringBuilder sb = new StringBuilder(postUrl).reverse();
     if (beginIndex != 0 || endIndex != -1) {
         endIndex = endIndex >= 0 ? endIndex : postUrl.length();
         postUrl = postUrl.substring(beginIndex, endIndex);
         sb = new StringBuilder(postUrl).reverse();
     }

     // ??.dict.baidu.com?baidu.com, gwt.google.com.cn?google.com.cn
     int shouldConcatToken = postUrl.toLowerCase().endsWith("cn") ? 3 : 2;// ?cn??
     StringTokenizer st = new StringTokenizer(sb.toString(), ".");
     sb = new StringBuilder();
     while (st.hasMoreTokens()) {
         if (shouldConcatToken-- == 0) {// ???
             break;
         }
         if (sb.length() > 0) {
             sb.append(".");
         }

         sb.append(st.nextToken());
     }

     return sb.reverse().toString();
 }

From source file:org.asoem.greyfish.utils.collect.BitString.java

@Override
public final String toString() {
    final StringBuilder builder = new StringBuilder();
    for (int i = 0; i < size(); i++) {
        builder.append(get(i) ? '1' : '0');
    }/*from ww  w  . ja  v a  2 s .  c  o m*/
    return builder.reverse().toString();
}