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:org.apache.hadoop.hbase.quotas.TestRegionSizeUse.java

/**
 * Writes at least {@code sizeInBytes} bytes of data to HBase and returns the TableName used.
 *
 * @param sizeInBytes The amount of data to write in bytes.
 * @return The table the data was written to
 *//* w w w  .j  a  va 2  s  .co  m*/
private TableName writeData(long sizeInBytes) throws IOException {
    final Connection conn = TEST_UTIL.getConnection();
    final Admin admin = TEST_UTIL.getAdmin();
    final TableName tn = TableName.valueOf(testName.getMethodName());

    // Delete the old table
    if (admin.tableExists(tn)) {
        admin.disableTable(tn);
        admin.deleteTable(tn);
    }

    // Create the table
    HTableDescriptor tableDesc = new HTableDescriptor(tn);
    tableDesc.addFamily(new HColumnDescriptor(F1));
    admin.createTable(tableDesc, Bytes.toBytes("1"), Bytes.toBytes("9"), NUM_SPLITS);

    final Table table = conn.getTable(tn);
    try {
        List<Put> updates = new ArrayList<>();
        long bytesToWrite = sizeInBytes;
        long rowKeyId = 0L;
        final StringBuilder sb = new StringBuilder();
        final Random r = new Random();
        while (bytesToWrite > 0L) {
            sb.setLength(0);
            sb.append(Long.toString(rowKeyId));
            // Use the reverse counter as the rowKey to get even spread across all regions
            Put p = new Put(Bytes.toBytes(sb.reverse().toString()));
            byte[] value = new byte[SIZE_PER_VALUE];
            r.nextBytes(value);
            p.addColumn(Bytes.toBytes(F1), Bytes.toBytes("q1"), value);
            updates.add(p);

            // Batch 50K worth of updates
            if (updates.size() > 50) {
                table.put(updates);
                updates.clear();
            }

            // Just count the value size, ignore the size of rowkey + column
            bytesToWrite -= SIZE_PER_VALUE;
            rowKeyId++;
        }

        // Write the final batch
        if (!updates.isEmpty()) {
            table.put(updates);
        }

        return tn;
    } finally {
        table.close();
    }
}

From source file:net.mindengine.galen.reports.HtmlSuiteReportingListener.java

private String extensionFrom(String filePath) {
    StringBuilder extension = new StringBuilder();
    for (int i = filePath.length() - 1; i >= 0; i--) {
        char ch = filePath.charAt(i);
        if (ch == '.') {
            break;
        } else/*from  ww  w .ja  va  2 s  .co  m*/
            extension.append(ch);
    }
    return extension.reverse().toString();
}

From source file:jfs.sync.encryption.AbstractEncryptedStorageAccess.java

protected String getMetaDataFileName(String relativePath) {
    StringBuilder result = new StringBuilder(getLastPathElement(JFSConfig.getInstance().getEncryptionPassPhrase(), relativePath));
    result.reverse();
    if (result.length()>8) {
        result.delete(0, result.length()-8);
    } // if//  ww  w. jav a  2 s  . co m
    result.append(".mt");
    return result.toString();
}

From source file:org.eclipse.skalli.model.Project.java

/**
 * Returns the short name if it exists or constructs one from the project name otherwise.
 *
 * <p>//  ww w.  j a  va 2  s .  co  m
 * The constructed name is guaranteed to contain only alpha-numeric letters and to be 2 to 10 characters long.
 * It will consist of the first letters of each word, if the project name includes multiple words.
 * Otherwise the project name shortened to max. 10 characters will be returned.
 * </p>
 *
 * @return
 */
public String getOrConstructShortName() {
    if (!StringUtils.isBlank(shortName)) {
        return shortName;
    } else {
        if (name.contains(" ")) { //$NON-NLS-1$
            // use first characters of each word, if they are alpha-numeric and the resulting length exceeds 1 char.
            StringBuilder retAbbrev = new StringBuilder();
            for (String n : name.split(" ")) { //$NON-NLS-1$
                if (n.length() > 0) {
                    char ch = n.charAt(0);
                    if (CharUtils.isAsciiAlphanumeric(ch) && retAbbrev.length() < 10) {
                        retAbbrev.append(ch);
                    }
                }
            }
            // if the first-char abbreviation is long enough, use it
            if (retAbbrev.length() > 1) {
                return retAbbrev.toString();
            }
        }

        // Otherwise use the first 10 alpha-numeric characters of the project name, if they exceed 1 char.
        StringBuilder retShortenedName = new StringBuilder();
        for (int i = 0; i < name.length() && retShortenedName.length() < 10; i++) {
            char ch = name.charAt(i);
            if (CharUtils.isAsciiAlphanumeric(ch)) {
                retShortenedName.append(ch);
            }
        }
        if (retShortenedName.length() > 1) {
            return retShortenedName.toString();
        }

        // If still no valid short name was found, then return the last 10 alpha-numeric characters of the project id
        StringBuilder retProjectId = new StringBuilder();
        for (int i = projectId.length() - 1; i >= 0 && retProjectId.length() < 10; i--) {
            char ch = projectId.charAt(i);
            if (CharUtils.isAsciiAlphanumeric(ch)) {
                retProjectId.append(ch);
            }
        }
        return retProjectId.reverse().toString();
    }
}

From source file:z.tool.web.servlet.AbstractServlet.java

private String getRealClassName(int max) {
    char[] chars = this.getClass().getName().toCharArray();

    int found = 0;
    StringBuilder buff = new StringBuilder();
    for (int i = chars.length - 1; i > -1; i--) {
        char c = chars[i];
        if ('.' == c) {
            found++;//from  w  w  w. j  a va2  s.c o m
        }
        if (found >= max) {
            break;
        }
        buff.append(c);
    }

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

From source file:etymology.config.Configuration.java

public void setWordsToMonitor(List<String> wordsToMonitor) {
    this.wordsToMonitor = wordsToMonitor;
    if (wordsToMonitor == null) {
        return;//from w  w w  . j a  v  a  2  s  .  co m
    }
    System.out.println("Flipped around: " + areWordsFlippedAround());
    if (areWordsFlippedAround()) {
        this.wordsToMonitor = new ArrayList();
        for (int i = 0; i < wordsToMonitor.size(); i++) {
            StringBuilder sb = new StringBuilder();
            sb.append(wordsToMonitor.get(i));
            sb.reverse();
            this.wordsToMonitor.add(sb.toString());
        }
    }

}

From source file:etymology.config.Configuration.java

public void setWordsFlippedAround(boolean flip) {
    this.flipWordsAround = flip;
    if (wordsToMonitor != null && flip) {
        List currentWordsToMonitor = wordsToMonitor;
        this.wordsToMonitor = new ArrayList();
        for (int i = 0; i < currentWordsToMonitor.size(); i++) {
            StringBuilder sb = new StringBuilder();
            sb.append(currentWordsToMonitor.get(i));
            sb.reverse();
            this.wordsToMonitor.add(sb.toString());
        }//from   ww w.jav  a 2s  .  c om
    }
}

From source file:org.apache.hadoop.hbase.quotas.SpaceQuotaHelperForTests.java

void writeData(TableName tn, long sizeInBytes, byte[] qual) throws IOException {
    final Connection conn = testUtil.getConnection();
    final Table table = conn.getTable(tn);
    try {/*from w w  w. j av a 2 s. c o m*/
        List<Put> updates = new ArrayList<>();
        long bytesToWrite = sizeInBytes;
        long rowKeyId = 0L;
        final StringBuilder sb = new StringBuilder();
        final Random r = new Random();
        while (bytesToWrite > 0L) {
            sb.setLength(0);
            sb.append(Long.toString(rowKeyId));
            // Use the reverse counter as the rowKey to get even spread across all regions
            Put p = new Put(Bytes.toBytes(sb.reverse().toString()));
            byte[] value = new byte[SIZE_PER_VALUE];
            r.nextBytes(value);
            p.addColumn(Bytes.toBytes(F1), qual, value);
            updates.add(p);

            // Batch ~13KB worth of updates
            if (updates.size() > 50) {
                table.put(updates);
                updates.clear();
            }

            // Just count the value size, ignore the size of rowkey + column
            bytesToWrite -= SIZE_PER_VALUE;
            rowKeyId++;
        }

        // Write the final batch
        if (!updates.isEmpty()) {
            table.put(updates);
        }

        LOG.debug("Data was written to HBase");
        // Push the data to disk.
        testUtil.getAdmin().flush(tn);
        LOG.debug("Data flushed to disk");
    } finally {
        table.close();
    }
}

From source file:org.patientview.monitoring.ImportMonitor.java

/**
 * Returns the last N lines of a file. Assumes lines are terminated by |n ascii character
 *//*from ww w. j  av  a2 s  .  com*/
private static List<String> getLastNLinesOfFile(int numberOfLinesToReturn) {
    List<String> lastNLines = new ArrayList<String>();
    java.io.RandomAccessFile fileHandler = null;

    try {
        File file = getTodaysCountFile();

        fileHandler = new java.io.RandomAccessFile(file, "r");

        long totalNumberOfCharactersInFile = file.length() - 1;

        StringBuilder sb = new StringBuilder();
        int numberOfLinesRead = 0;

        /**
         * loop through characters in file, construct lines out of characters, add lines to a list
         */
        for (long currentCharacter = totalNumberOfCharactersInFile; currentCharacter != -1; currentCharacter--) {
            fileHandler.seek(currentCharacter);

            int readByte = fileHandler.readByte();

            if (readByte == LINE_FEED || readByte == CARRIAGE_RETURN) {
                if (numberOfLinesRead == numberOfLinesToReturn) {
                    break;
                }

                numberOfLinesRead++;

                /**
                 * add line to line list
                 */
                String currentLine = sb.reverse().toString();
                sb = new StringBuilder();

                if (StringUtils.isNotBlank(currentLine)) {
                    lastNLines.add(currentLine);
                } else {
                    LOGGER.error("Read line does not contain any data");
                    continue;
                }
            } else {
                sb.append((char) readByte);
            }
        }

        /**
         * add the last line
         */
        lastNLines.add(sb.reverse().toString());
    } catch (Exception e) {
        LOGGER.error("Can not find today's file", e);
    } finally {
        if (fileHandler != null) {
            try {
                fileHandler.close();
            } catch (IOException e) {
                fileHandler = null;
            }
        }
    }

    return lastNLines;
}

From source file:org.squidy.designer.zoom.impl.SourceCodeShape.java

private JEditorPane createCodePane(URL sourceCodeURL) {

    codePane = new JEditorPane() {
        @Override/*  w  w w . j a  va  2s.c o m*/
        protected void processComponentKeyEvent(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_SPACE && e.isControlDown()) {
                System.out.println("Code completion");

                int caretPosition = codePane.getCaretPosition();

                String code = codePane.getText();
                switch (code.charAt(caretPosition - 1)) {
                case '.':
                    int pseudoCaret = caretPosition - 1;
                    StringBuilder word = new StringBuilder();
                    for (char c = code.charAt(--pseudoCaret); !isEndOfWord(c); c = code.charAt(--pseudoCaret)) {
                        word.append(c);
                    }

                    word = word.reverse();

                    System.out.println("WORD: " + word);

                    // Class<?> type =
                    // ReflectionUtil.loadClass(word.toString());
                    //                  
                    // System.out.println("TYPE: " + type);

                    JPopupMenu menu = new JPopupMenu("sdaf");
                    Point p = codePane.getCaret().getMagicCaretPosition();
                    System.out.println("CARET POS: " + p);
                    // Point p = codePane.get

                    // menu.setPreferredSize(new Dimension(200, 200));
                    menu.setLocation(30, 30);
                    menu.add("test");

                    codePane.add(menu);

                    // System.out.println(p);

                    // codePane.get

                    menu.show(codePane, p.x, p.y);

                    break;
                }
            }

            super.processComponentKeyEvent(e);
        }

        /**
         * @param c
         * @return
         */
        private boolean isEndOfWord(char c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t';
        }
    };

    EditorKit editorKit = new StyledEditorKit() {

        /**
         * 
         */
        private static final long serialVersionUID = 7024886168909204806L;

        public Document createDefaultDocument() {
            return new SyntaxDocument();
        }
    };

    codePane.setEditorKitForContentType("text/java", editorKit);
    codePane.setContentType("text/java");

    try {
        FileInputStream fis = new FileInputStream(sourceCodeURL.getPath());
        codePane.read(fis, null);
        originSourceCode = codePane.getText();

        computeHeightOfCodePane();
        codePane.setAutoscrolls(true);
    } catch (Exception e) {
        codePane.setText("File not found!");
    }

    codePane.requestFocus();
    codePane.setBorder(BorderFactory.createLineBorder(Color.BLACK));

    codePane.addKeyListener(new KeyAdapter() {

        /*
         * (non-Javadoc)
         * 
         * @see
         * java.awt.event.KeyAdapter#keyPressed(java.awt.event.KeyEvent)
         */
        @Override
        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);

            switch (e.getKeyCode()) {
            case KeyEvent.VK_ENTER:
            case KeyEvent.VK_DELETE:
            case KeyEvent.VK_BACK_SPACE:
            case KeyEvent.VK_SPACE:
                computeHeightOfCodePane();
                break;
            }

            markDirty();
        }
    });

    // final JPopupMenu menu = new JPopupMenu();
    //
    // JMenuItem i = new JMenuItem("Option 1");
    // JMenuItem i2 = new JMenuItem("Option 2");
    // menu.add(i);
    // menu.add(i2);
    // edit.add(menu);
    // getComponent().addKeyListener(new KeyAdapter() {
    //      
    // public void keyTyped(KeyEvent e) {
    // if(e.getModifiers() == 2 && e.getKeyChar() == ' ') {
    // Point popupLoc = edit.getCaret().getMagicCaretPosition();
    // System.out.println(popupLoc);
    // menu.setLocation(new
    // Point((int)popupLoc.getX(),(int)popupLoc.getY()));
    // menu.setVisible(true);
    // }
    //      
    // }
    // });

    return codePane;
}