List of usage examples for org.apache.commons.lang StringUtils repeat
public static String repeat(String str, int repeat)
Repeat a String repeat
times to form a new String.
From source file:org.batoo.common.impl.log.BLoggerImpl.java
/** * {@inheritDoc}/*from w w w . j ava2s. co m*/ * */ @Override public String boxed(String block, Object[] parameters) { try { if ((parameters != null) && (parameters.length > 0)) { block += "\n\n" + Arrays.toString(parameters); } block = block.replaceAll("\\t", " "); final List<String> lines = IOUtils.readLines(new StringReader(block)); int max = 0; for (final String line : lines) { max = Math.max(max, line.length()); } max += 4; final StringBuffer boxed = new StringBuffer("\n"); boxed.append(StringUtils.repeat("-", max)); boxed.append("\n"); for (final String line : lines) { boxed.append("| "); boxed.append(StringUtils.rightPad(line, max - 4)); boxed.append(" |\n"); } boxed.append(StringUtils.repeat("-", max)); return boxed.toString(); } catch (final Exception e) { return block; } }
From source file:org.batoo.jpa.benchmark.BenchmarkTest.java
private void waitUntilFinish(ThreadPoolExecutor executor) { final BlockingQueue<Runnable> workQueue = executor.getQueue(); try {//from w ww . j av a 2 s . co m final long started = System.currentTimeMillis(); int lastToGo = workQueue.size(); final int total = workQueue.size(); int performed = 0; int maxStatusMessageLength = 0; while (!workQueue.isEmpty()) { final float doneNow = lastToGo - workQueue.size(); performed += doneNow; final float elapsed = (System.currentTimeMillis() - started) / 1000; lastToGo = workQueue.size(); if (performed > 0) { final float throughput = performed / elapsed; final float eta = ((elapsed * total) / performed) - elapsed; final float percentDone = (100 * (float) lastToGo) / total; final int gaugeDone = (int) ((100 - percentDone) / 5); final String gauge = "[" + StringUtils.repeat("", gaugeDone) + StringUtils.repeat("-", 20 - gaugeDone) + "]"; final String sampling = this.profilingQueue.size() > 0 ? MessageFormat.format(" | Samples {0}", this.profilingQueue.size()) : ""; if ((maxStatusMessageLength != 0) || (eta > 5)) { String statusMessage = MessageFormat.format( "\r{4} %{5,number,00.00} | ETA {2} | LAST TPS {0} ops / sec | AVG TPS {1,number,#.0} | LEFT {3}{6}", // doneNow, throughput, this.etaToString((int) eta), workQueue.size(), gauge, percentDone, sampling); maxStatusMessageLength = Math.max(statusMessage.length(), maxStatusMessageLength); statusMessage = StringUtils.leftPad(statusMessage, maxStatusMessageLength - statusMessage.length()); System.out.print(statusMessage); } } if (elapsed > BenchmarkTest.MAX_TEST_TIME) { throw new IllegalStateException("Max allowed test time exceeded"); } Thread.sleep(1000); } if (maxStatusMessageLength > 0) { System.out.print("\r" + StringUtils.repeat(" ", maxStatusMessageLength) + "\r"); } executor.shutdown(); if (!executor.awaitTermination(10, TimeUnit.SECONDS)) { BenchmarkTest.LOG.warn("Forcefully shutting down the thread pool"); executor.shutdownNow(); } BenchmarkTest.LOG.warn("Iterations completed"); } catch (final InterruptedException e) { throw new RuntimeException(e); } }
From source file:org.batoo.jpa.benchmark.TimeElement.java
/** * @param rowNo//from w w w . j av a2 s . c o m * the row no * @param depth * the depth * @return the row no * * @since $version * @author hceylan */ public int dump1(int rowNo, int depth) { if ((depth > 0) && (this.timeWithoutDerby > 10000000)) { rowNo++; final String tabs = StringUtils.repeat(" ", depth); System.out.println(String.format("%010d", rowNo) + // " " + String.format("%010d", depth) + // " " + String.format("%010d", this.hits) + // " " + String.format("%010d", this.selfHit) + // " " + String.format("%010d", this.time / 1000000) + // " " + String.format("%010d", this.timeWithoutDerby / 1000000) + // " " + String.format("%010d", this.self / 1000000) + // tabs + this.key); } final List<TimeElement> children = Lists.newArrayList(this.values()); Collections.sort(children); for (final TimeElement child : children) { rowNo = child.dump1(rowNo, depth + 1); } return rowNo; }
From source file:org.batoo.jpa.common.impl.log.BLoggerImpl.java
/** * {@inheritDoc}/*ww w .j a va 2s. c o m*/ * */ @Override public String boxed(String block, Object[] parameters) { try { if ((parameters != null) && (parameters.length > 0)) { block += "\n\n" + Arrays.toString(parameters); } block = block.replaceAll("\\t", " "); final List<String> lines = IOUtils.readLines(new StringReader(block)); int max = 0; for (final String line : lines) { max = Math.max(max, line.length()); } max += 4; final StringBuffer boxed = new StringBuffer("\n"); boxed.append(StringUtils.repeat("-", max)); boxed.append("\n"); for (final String line : lines) { boxed.append("| "); boxed.append(StringUtils.rightPad(line, max - 4)); boxed.append(" |\n"); } boxed.append(StringUtils.repeat("-", max)); return boxed.toString(); } catch (final Throwable e) { return block; } }
From source file:org.batoo.jpa.core.impl.criteria.QueryImpl.java
private void dumpResultSet() throws SQLException { final int[] lengths = new int[this.labels.length]; for (int i = 0; i < lengths.length; i++) { lengths[i] = this.max(lengths[i], StringUtils.length(this.labels[i])); }//from w ww. ja v a 2 s . c o m for (final Object[] data : this.data) { for (int i = 0; i < this.labels.length; i++) { final Object value = data[i]; if (value != null) { lengths[i] = this.max(lengths[i], StringUtils.length(value.toString())); } } } int length = 1; for (final int l : lengths) { length += l + 3; } final StringBuffer dump = new StringBuffer("Query returned {0} row(s):\n"); // the labels dump.append(StringUtils.repeat("-", length)); dump.append("\n| "); for (int i = 0; i < this.labels.length; i++) { String strValue = StringUtils.abbreviate(this.labels[i], lengths[i]); strValue = StringUtils.rightPad(strValue, lengths[i]); dump.append(strValue); dump.append(" | "); } // the data dump.append("\n"); dump.append(StringUtils.repeat("-", length)); for (final Object[] data : this.data) { dump.append("\n| "); for (int i = 0; i < this.labels.length; i++) { final Object value = data[i]; String strValue = value != null ? value.toString() : "!NULL!"; strValue = StringUtils.abbreviate(strValue, lengths[i]); if (value instanceof Number) { strValue = StringUtils.leftPad(strValue, lengths[i]); } else { strValue = StringUtils.rightPad(strValue, lengths[i]); } dump.append(strValue); dump.append(" | "); } } dump.append("\n"); dump.append(StringUtils.repeat("-", length)); QueryImpl.LOG.debug(dump.toString(), this.data.size()); }
From source file:org.beangle.commons.property.PropertyConfigBean.java
public String toString() { StringBuilder sb = new StringBuilder("DefaultSystemConfig["); List<String> props = new ArrayList<String>(properties.keySet()); Collections.sort(props);/* w ww. j a v a2 s . co m*/ int maxlength = 0; for (String property : props) { if (property.length() > maxlength) { maxlength = property.length(); } } for (String property : props) { sb.append('\n').append(property); sb.append(StringUtils.repeat(" ", maxlength - property.length())); sb.append('=').append(properties.get(property)); } sb.append("\n]"); return sb.toString(); }
From source file:org.beangle.ems.dictionary.service.impl.SeqCodeGenerator.java
/** * ???// w w w. j ava 2s . c om */ @SuppressWarnings("unchecked") public String gen(CodeFixture fixture) { // ?? String script = fixture.getScript(); CodeScript codeScript = null; if (null == script) { codeScript = getCodeScript(EntityUtils.getEntityClassName(fixture.getEntity().getClass())); if (null == codeScript) { return null; } script = codeScript.getScript(); try { String code = (String) PropertyUtils.getProperty(fixture.getEntity(), codeScript.getAttr()); if (isValidCode(code)) { return code; } } catch (Exception e) { throw new RuntimeException(e); } } int seqLength = -1; // ???seq[x] if (StringUtils.contains(script, SEQ)) { seqLength = NumberUtils.toInt(StringUtils.substringBetween(script, SEQ + "[", "]")); script = StringUtils.replace(script, SEQ + "[" + StringUtils.substringBetween(script, SEQ + "[", "]") + "]", SEQ); } fixture.setScript(script); String code = super.gen(fixture); List<String> seqs = CollectUtils.newArrayList(); if (-1 != seqLength) { try { OqlBuilder<?> builder = OqlBuilder.from(Class.forName(codeScript.getCodeClassName()), "entity"); builder.select("select substr(entity." + codeScript.getAttr() + "," + (code.indexOf(SEQ) + 1) + "," + seqLength + ")"); builder.where(" entity." + codeScript.getAttr() + " like :codeExample", StringUtils.replace(code, SEQ, "%")); builder.where("length(entity." + codeScript.getAttr() + ")=" + (code.length() - SEQ.length() + seqLength)); seqs = (List<String>) entityDao.search(builder); Collections.sort(seqs); } catch (Exception e) { throw new RuntimeException(e); } synchronized (this) { int newSeqNo = 0; for (Iterator<String> iter = seqs.iterator(); iter.hasNext();) { String seqNo = iter.next(); if (NumberUtils.toInt(seqNo) - newSeqNo >= 2) { break; } else { newSeqNo = NumberUtils.toInt(seqNo); } } newSeqNo++; String seqNo = String.valueOf(newSeqNo); if (0 != seqLength) { seqNo = StringUtils.repeat("0", seqLength - seqNo.length()) + newSeqNo; } code = StringUtils.replace(code, SEQ, seqNo); } } return code; }
From source file:org.caleydo.view.tourguide.internal.view.col.GroupDistributionRankColumnModel.java
/** * @param row//from w w w . j av a2 s. c o m * @param string * @return */ private static String getLabel(AScoreRow r, String default_) { Collection<GroupInfo> infos = r.getGroupInfos(); int sum = 0; for (GroupInfo info : infos) sum += info.getSize(); if (sum == 0) return default_; StringBuilder b = new StringBuilder(); int maxSize = "Total".length(); for (GroupInfo info : infos) maxSize = Math.max(info.getLabel().length(), maxSize); // maxSize = (int) (maxSize * 1.2); b.append("Total").append(StringUtils.repeat(" ", maxSize - "Total".length())).append('\t').append(sum); final float factor = 100.f / sum; for (GroupInfo info : infos) { b.append('\n').append(info.getLabel()) .append(StringUtils.repeat(" ", maxSize - info.getLabel().length())); b.append('\t').append(info.getSize()); b.append(String.format("(%.2f%%)", info.getSize() * factor)); } return b.toString(); }
From source file:org.codehaus.mojo.mrm.servlet.FileSystemServlet.java
private void serveDirectoryAsHTML(HttpServletResponse resp, String path, String context, DirectoryEntry dirEntry, Entry[] entries) throws IOException { resp.setContentType("text/html"); PrintWriter w = resp.getWriter(); w.println("<html>"); w.println(" <head>"); w.println(" <title>Index of " + context + path + "</title>"); w.println(" <meta http-equiv=\"Content-Type\" repository=\"text/html; charset=utf-8\"/>"); w.println("</head>"); w.println("<body>"); w.println("<h1>Index of " + context + path + "</h1>"); w.println(" <hr/>"); w.write("<pre>"); if (dirEntry.getParent() != null) { w.println("<a href='../'>../</a>"); }/*from ww w . jav a2 s .com*/ SimpleDateFormat format = new SimpleDateFormat("dd-MMM-yyyy hh:mm"); if (entries != null) { for (int i = 0; i < entries.length; i++) { final String childName = entries[i].getName(); boolean directory = entries[i] instanceof DirectoryEntry; if (directory) { String dirName = childName + "/"; w.write("<a href=\"./" + Utils.urlEncodePathSegment(childName) + "/\">" + formatName(dirName) + "</a>" + StringUtils.repeat(" ", Math.max(0, NAME_COL_WIDTH - dirName.length()))); } else { w.write("<a href=\"./" + Utils.urlEncodePathSegment(childName) + "\">" + formatName(childName) + "</a>" + StringUtils.repeat(" ", Math.max(0, NAME_COL_WIDTH - childName.length()))); } long timestamp = 0; try { timestamp = entries[i].getLastModified(); } catch (IOException e) { // ignore } w.write(" "); w.write(format.format(timestamp != -1 ? new Date(timestamp) : new Date())); if (directory) { w.println(StringUtils.leftPad("-", SIZE_COL_WIDTH)); } else if (entries[i] instanceof FileEntry) { FileEntry fileEntry = (FileEntry) entries[i]; try { long size = fileEntry.getSize(); if (size >= 0) { w.println(StringUtils.leftPad(Long.toString(size), SIZE_COL_WIDTH)); } else { w.println(StringUtils.leftPad("-", SIZE_COL_WIDTH)); } } catch (IOException e) { w.println(StringUtils.leftPad("-", SIZE_COL_WIDTH)); } } else { w.println(StringUtils.leftPad("-", SIZE_COL_WIDTH)); } } } w.write("</pre>"); w.println(" <hr/>"); w.println("</body>"); w.println("</html>"); return; }
From source file:org.codice.ddf.catalog.ui.query.delegate.SearchTermTest.java
/** * While there is no absolute guarantee that this test will fail on any given machine, during * development and testing, the search against this input value of 1000 'a' with a terminal '!' * ran for well over a minute before being manually stopped. If performance improves to the point * where this test fails, we can bump up the search string by another order of magnitude. * * <p>This test will also fail if a future version of Java were to introduce an improved, * linear-time regex implementation (at which time we could yank this time-limited band-aid); * however, that's not on the table for J11 so we should expect this to be a necessary workaround * for the foreseeable future./*from w ww . ja va2s.c o m*/ */ @Test public void questionableMatch() { String inputTerm = "*(a+)+"; SearchTerm searchTerm = new SearchTerm(inputTerm); assertThat(searchTerm.match(StringUtils.repeat("a", 1000) + "!"), is(false)); }