List of usage examples for java.lang StringBuffer insert
@Override public StringBuffer insert(int offset, double d)
From source file:ClassUtils.java
/** * Build a nice qualified name for an array: * component type class name + "[]"./*from w w w. j a v a 2s .c o m*/ * @param clazz the array class * @return a qualified name for the array class */ private static String getQualifiedNameForArray(Class clazz) { StringBuffer buffer = new StringBuffer(); while (clazz.isArray()) { clazz = clazz.getComponentType(); buffer.append(ClassUtils.ARRAY_SUFFIX); } buffer.insert(0, clazz.getName()); return buffer.toString(); }
From source file:StringUtils.java
static public String padWithLeadingZeros(String string, int desiredLength) { StringBuffer bff = new StringBuffer(string); while (bff.length() < desiredLength) bff.insert(0, '0'); return bff.toString(); }
From source file:com.spaceprogram.simplejpa.util.AmazonSimpleDBUtil.java
public static String encodeRealNumberRange(BigDecimal number, int maxDigitsLeft, int maxDigitsRight, BigDecimal offsetValue) { BigDecimal shiftMultiplier = new BigDecimal(Math.pow(10, maxDigitsRight)); // System.out.println("shiftMultiplier=" + shiftMultiplier); BigDecimal shiftedNumber = number.multiply(shiftMultiplier); // System.out.println("shiftedNumber=" + shiftedNumber); shiftedNumber = shiftedNumber.setScale(0, BigDecimal.ROUND_HALF_UP); // System.out.println("shiftedNumber rounded=" + shiftedNumber); BigDecimal shiftedOffset = offsetValue.multiply(shiftMultiplier); // System.out.println("shiftedOffset=" + shiftedOffset); BigDecimal offsetNumber = shiftedNumber.add(shiftedOffset); // System.out.println("offsetNumber=" + offsetNumber); String longString = offsetNumber.toString(); // System.out.println("shifted string=" + longString); int numBeforeDecimal = longString.length(); int numZeroes = maxDigitsLeft + maxDigitsRight - numBeforeDecimal; StringBuffer strBuffer = new StringBuffer(numZeroes + longString.length()); for (int i = 0; i < numZeroes; i++) { strBuffer.insert(i, '0'); }//from ww w . j a v a2s. c om strBuffer.append(longString); return strBuffer.toString(); }
From source file:net.fenyo.gnetwatch.GenericTools.java
static public String formatNumericString(final Config config, final String value) { final StringBuffer result = new StringBuffer(); for (int idx = value.length() - 1; idx >= 0; idx--) { if (idx != value.length() - 1 && (value.length() - 1 - idx) % 3 == 0) result.insert(0, config.getString("numeric_separator")); result.insert(0, value.charAt(idx)); }//from w ww . j a v a 2 s.co m return result.toString(); }
From source file:Main.java
private static String convertStringToTime(String oldTime) { if (oldTime == null || oldTime.length() == 0) { return "00h00m00s"; }//from w w w . j a v a 2 s . com boolean hasHours = (oldTime.indexOf('h') != -1); boolean hasMinutes = (oldTime.indexOf('m') != -1); StringBuffer rtn = new StringBuffer(oldTime); // Delete the seconds fields and replace other chars with colons rtn.deleteCharAt(oldTime.indexOf('s')); if (hasMinutes) { rtn.setCharAt(oldTime.indexOf('m'), ':'); } else { rtn.insert(0, "00:"); } if (hasHours) { rtn.setCharAt(oldTime.indexOf('h'), ':'); } else { rtn.insert(0, "00:"); } StringTokenizer st = new StringTokenizer(rtn.toString(), ":"); int index = 1; String hh = "00"; String mm = "00"; String ss = "00"; while (st.hasMoreTokens()) { String toke = st.nextToken(); switch (index) { case 1: if (toke.length() < 2) { hh = "0" + toke; } else { hh = toke; } break; case 2: if (toke.length() < 2) { mm = "0" + toke; } else { mm = toke; } break; case 3: if (toke.length() < 2) { ss = "0" + toke; } else { ss = toke; } break; } index++; } return hh + "h" + mm + "m" + ss + "s"; }
From source file:Main.java
/** * Searches for a node within a DOM document with a given node path expression. * Elements are separated by '.' characters. * Example: Foo.Bar.Poo//from ww w. ja v a 2s .c o m * @param doc DOM Document to search for a node. * @param pathExpression dot separated path expression * @return Node element found in the DOM document. */ public static Node findNodeByName(Document doc, String pathExpression) { final StringTokenizer tok = new StringTokenizer(pathExpression, "."); final int numToks = tok.countTokens(); NodeList elements; if (numToks == 1) { elements = doc.getElementsByTagNameNS("*", pathExpression); return elements.item(0); } String element = pathExpression.substring(pathExpression.lastIndexOf('.') + 1); elements = doc.getElementsByTagNameNS("*", element); String attributeName = null; if (elements.getLength() == 0) { //No element found, but maybe we are searching for an attribute attributeName = element; //cut off attributeName and set element to next token and continue Node found = findNodeByName(doc, pathExpression.substring(0, pathExpression.length() - attributeName.length() - 1)); if (found != null) { return found.getAttributes().getNamedItem(attributeName); } else { return null; } } StringBuffer pathName; Node parent; for (int j = 0; j < elements.getLength(); j++) { int cnt = numToks - 1; pathName = new StringBuffer(element); parent = elements.item(j).getParentNode(); do { if (parent != null) { pathName.insert(0, '.'); pathName.insert(0, parent.getLocalName());//getNodeName()); parent = parent.getParentNode(); } } while (parent != null && --cnt > 0); if (pathName.toString().equals(pathExpression)) { return elements.item(j); } } return null; }
From source file:ExecuteSQL.java
/** * This method attempts to output the contents of a ResultSet in a textual * table. It relies on the ResultSetMetaData class, but a fair bit of the * code is simple string manipulation.// w ww . jav a 2s. co m */ static void printResultsTable(ResultSet rs, OutputStream output) throws SQLException { // Set up the output stream PrintWriter out = new PrintWriter(output); // Get some "meta data" (column names, etc.) about the results ResultSetMetaData metadata = rs.getMetaData(); // Variables to hold important data about the table to be displayed int numcols = metadata.getColumnCount(); // how many columns String[] labels = new String[numcols]; // the column labels int[] colwidths = new int[numcols]; // the width of each int[] colpos = new int[numcols]; // start position of each int linewidth; // total width of table // Figure out how wide the columns are, where each one begins, // how wide each row of the table will be, etc. linewidth = 1; // for the initial '|'. for (int i = 0; i < numcols; i++) { // for each column colpos[i] = linewidth; // save its position labels[i] = metadata.getColumnLabel(i + 1); // get its label // Get the column width. If the db doesn't report one, guess // 30 characters. Then check the length of the label, and use // it if it is larger than the column width int size = metadata.getColumnDisplaySize(i + 1); if (size == -1) size = 30; // Some drivers return -1... if (size > 500) size = 30; // Don't allow unreasonable sizes int labelsize = labels[i].length(); if (labelsize > size) size = labelsize; colwidths[i] = size + 1; // save the column the size linewidth += colwidths[i] + 2; // increment total size } // Create a horizontal divider line we use in the table. // Also create a blank line that is the initial value of each // line of the table StringBuffer divider = new StringBuffer(linewidth); StringBuffer blankline = new StringBuffer(linewidth); for (int i = 0; i < linewidth; i++) { divider.insert(i, '-'); blankline.insert(i, " "); } // Put special marks in the divider line at the column positions for (int i = 0; i < numcols; i++) divider.setCharAt(colpos[i] - 1, '+'); divider.setCharAt(linewidth - 1, '+'); // Begin the table output with a divider line out.println(divider); // The next line of the table contains the column labels. // Begin with a blank line, and put the column names and column // divider characters "|" into it. overwrite() is defined below. StringBuffer line = new StringBuffer(blankline.toString()); line.setCharAt(0, '|'); for (int i = 0; i < numcols; i++) { int pos = colpos[i] + 1 + (colwidths[i] - labels[i].length()) / 2; overwrite(line, pos, labels[i]); overwrite(line, colpos[i] + colwidths[i], " |"); } // Then output the line of column labels and another divider out.println(line); out.println(divider); // Now, output the table data. Loop through the ResultSet, using // the next() method to get the rows one at a time. Obtain the // value of each column with getObject(), and output it, much as // we did for the column labels above. while (rs.next()) { line = new StringBuffer(blankline.toString()); line.setCharAt(0, '|'); for (int i = 0; i < numcols; i++) { Object value = rs.getObject(i + 1); if (value != null) overwrite(line, colpos[i] + 1, value.toString().trim()); overwrite(line, colpos[i] + colwidths[i], " |"); } out.println(line); } // Finally, end the table with one last divider line. out.println(divider); out.flush(); }
From source file:de.xwic.appkit.core.model.util.FormatUtils.java
/** * Splits a number string into pairs seperated by a space. If the * number of numbers is not a multiple of 2, the first number stands * alone./*from www .j av a2s . c o m*/ * @param num * @return */ private static String splashNumber(String num) { // splash lastNo StringBuffer sb = new StringBuffer(); int nr = 0; for (int i = num.length() - 1; i >= 0; i--) { char c = num.charAt(i); if (c >= '0' && c <= '9') { if (nr == 2) { sb.insert(0, ' '); nr = 0; } sb.insert(0, c); nr++; } else if (c == '-') { sb.insert(0, " - "); nr = 0; } else { sb.insert(0, c); } } return sb.toString(); }
From source file:org.executequery.gui.text.TextUtilities.java
public static void insertFromFile(JTextComponent textComponent) { StringBuffer buf = null;// w ww. ja va 2s. co m String text = null; FileChooserDialog fileChooser = new FileChooserDialog(); fileChooser.setDialogTitle("Insert from file"); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setDialogType(JFileChooser.OPEN_DIALOG); int result = fileChooser.showDialog(GUIUtilities.getInFocusDialogOrWindow(), "Insert"); if (result == JFileChooser.CANCEL_OPTION) return; File file = fileChooser.getSelectedFile(); try { FileInputStream input = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(input)); buf = new StringBuffer(10000); char newLine = '\n'; while ((text = reader.readLine()) != null) buf.append(text).append(newLine); reader.close(); reader = null; input.close(); input = null; int index = textComponent.getCaretPosition(); StringBuffer sb = new StringBuffer(textComponent.getText()); sb.insert(index, buf.toString()); textComponent.setText(sb.toString()); textComponent.setCaretPosition(index + buf.length()); } catch (OutOfMemoryError e) { buf = null; text = null; System.gc(); GUIUtilities.displayErrorMessage("Out of Memory.\nThe file is " + "too large to\nopen for viewing."); } catch (IOException e) { e.printStackTrace(); StringBuffer sb = new StringBuffer(); sb.append("An error occurred opening the selected file.").append("\n\nThe system returned:\n") .append(e.getMessage()); GUIUtilities.displayExceptionErrorDialog(sb.toString(), e); } }
From source file:com.aw.core.dao.AWQueryRunner.java
public static String buildSQLLog(String sql, Object[] filterKeys) { StringBuffer buf = new StringBuffer(); buf.append(sql);/*from www. ja v a2s . c om*/ int idx = 0; while (buf.indexOf("?") >= 0) { int pos = buf.indexOf("?"); buf.deleteCharAt(pos); Object filterKey = filterKeys[idx]; if (filterKey instanceof String) filterKey = "'" + filterKey + "'"; buf.insert(pos, filterKey); idx++; } // if (filterKeys != null) { // buf.append(" params:"); // buf.append(Arrays.asList(filterKeys).toString()); // } return buf.toString(); }