List of usage examples for java.util Formatter Formatter
public Formatter()
From source file:com.flexive.war.beans.admin.content.ContentGeneratorBean.java
public String create() { try {//w w w. j av a 2s . co m final ContentEngine contentEngine = EJBLookup.getContentEngine(); final TreeEngine treeEngine = EJBLookup.getTreeEngine(); final FxTreeNode folder; if (StringUtils.isNotBlank(treeFolder)) { treeFolder = (treeFolder.startsWith("/") ? treeFolder : "/" + treeFolder).trim(); final long[] nodes = treeEngine.createNodes(FxTreeMode.Edit, FxTreeNode.ROOT_NODE, 0, treeFolder); folder = treeEngine.getNode(FxTreeMode.Edit, nodes[nodes.length - 1]); } else { folder = null; } final String typeName = CacheAdmin.getEnvironment().getType(type).getName(); final long startTime = System.currentTimeMillis(); long initializeTime = 0; long randomizeTime = 0; long saveTime = 0; long treeTime = 0; for (int i = 0; i < count; i++) { long start = System.currentTimeMillis(); final FxContent co = contentEngine.initialize(type); initializeTime += System.currentTimeMillis() - start; start = System.currentTimeMillis(); co.randomize(maxMultiplicity); randomizeTime += System.currentTimeMillis() - start; start = System.currentTimeMillis(); final FxPK pk = contentEngine.save(co); saveTime += System.currentTimeMillis() - start; if (folder != null) { start = System.currentTimeMillis(); treeEngine.save(FxTreeNodeEdit.createNewChildNode(folder).setName(typeName + "-" + pk) .setReference(pk)); treeTime += System.currentTimeMillis() - start; } } final long totalTime = System.currentTimeMillis() - startTime; new FxFacesMsgInfo("Content.nfo.testData.created", count, totalTime, initializeTime, 100 * initializeTime / totalTime, randomizeTime, 100 * randomizeTime / totalTime, saveTime, 100 * saveTime / totalTime, new Formatter().format("%.2f", count / (double) totalTime * 1000), treeTime, 100 * treeTime / totalTime).addToContext(); } catch (Exception e) { new FxFacesMsgErr(e).addToContext(); } return "contentTestData"; }
From source file:org.apache.camel.component.mongodb.MongoDbOperationsTest.java
@Test public void testUpdate() throws Exception { // Prepare test assertEquals(0, testCollection.count()); for (int i = 1; i <= 100; i++) { String body = null;/*from w w w.j a v a 2 s.co m*/ Formatter f = new Formatter(); if (i % 2 == 0) { body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString(); } else { body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\", \"extraField\": true}", i) .toString(); } template.requestBody("direct:insert", body); } assertEquals(100L, testCollection.count()); // Testing the update logic DBObject extraField = new BasicDBObject("extraField", true); assertEquals("Number of records with 'extraField' flag on must equal 50", 50L, testCollection.count(extraField)); assertEquals("Number of records with 'scientist' field = Darwin on must equal 0", 0, testCollection.count(new BasicDBObject("scientist", "Darwin"))); DBObject updateObj = new BasicDBObject("$set", new BasicDBObject("scientist", "Darwin")); Object result = template.requestBodyAndHeader("direct:update", new Object[] { extraField, updateObj }, MongoDbConstants.MULTIUPDATE, true); assertTrue(result instanceof WriteResult); assertEquals("Number of records with 'scientist' field = Darwin on must equal 50 after update", 50, testCollection.count(new BasicDBObject("scientist", "Darwin"))); }
From source file:module.mission.presentationTier.dto.SearchMissionsDTO.java
public String getRequestParameters() { return new Formatter().format( "processNumber=%s&ruOID=%s&puOID=%s&f=%s&d=%s&i=%s&rpOID=%s&pOID=%s&ps=%s&auOID=%s&paaOID=%s&fc=%s&ft=%s", getProcessNumber(), getRequestingUnitParameter(), getPayingUnitParameter(), getForeignParameter(), getDateParameter(), getIntervalParameter(), getRequestingPersonParameter(), getParticipantParameter(), getPendingStateParameter(), getAccountingUnitParameter(), getParticipantAuthorizationAuthorityParameter(), getFilterCanceledProcesses(), getFilterTakenProcesses()).toString(); }
From source file:com.adobe.ac.pmd.rules.core.Violation.java
public String toXmlString(final IFlexFile violatedFile, final String ruleSetName) { final Formatter formatter = new Formatter(); if (rule != null) { final StringBuffer message = new StringBuffer(getRuleMessage()); formatter.format(" <violation beginline=\"%d\" " + "endline=\"%d\" begincolumn=\"%d\" " + "endcolumn=\"%d\" rule=\"%s\" " + "ruleset=\"%s\" package=\"%s\" " + "class=\"%s\" externalInfoUrl=\"%s\" " + "priority=\"%s\">%s</violation>" + getNewLine(), beginLine, endLine, beginColumn, endColumn, rule.getRuleName(), ruleSetName, violatedFile.getPackageName(), violatedFile.getClassName(), RULESET_CREATOR_URL + extractShortName(rule.getName()), rule.getPriority(), message); }//from w w w . j av a 2 s .c o m return formatter.toString(); }
From source file:edu.cmu.tetrad.cli.AbstractAlgorithmCli.java
private String createRunInfo(Set<String> excludedVariables, DataSet dataSet) { Formatter fmt = new Formatter(); fmt.format("Runtime Parameters:%n"); fmt.format("verbose = %s%n", verbose); fmt.format("number of threads = %s%n", numOfThreads); fmt.format("%n"); fmt.format("Dataset:%n"); fmt.format("file = %s%n", dataFile.getFileName()); fmt.format("delimiter = %s%n", Args.getDelimiterName(delimiter)); fmt.format("cases read in = %s%n", dataSet.getNumRows()); fmt.format("variables read in = %s%n", dataSet.getNumColumns()); fmt.format("%n"); if (excludedVariableFile != null || knowledgeFile != null) { fmt.format("Filters:%n"); if (excludedVariableFile != null) { fmt.format("excluded variables (%d variables) = %s%n", excludedVariables.size(), excludedVariableFile.getFileName()); }/*w ww . jav a 2 s. c om*/ if (knowledgeFile != null) { fmt.format("knowledge = %s%n", knowledgeFile.getFileName()); } fmt.format("%n"); } fmt.format("Algorithm Parameters:%n"); printParameterInfos(fmt); fmt.format("%n"); fmt.format("Data Validations:%n"); printValidationInfos(fmt); return fmt.toString(); }
From source file:gdv.xport.util.XmlFormatter.java
private void write(final String attribute, final String format, final Object... args) throws XMLStreamException { Formatter formatter = new Formatter(); String s = formatter.format(format, args).toString(); xmlStreamWriter.writeAttribute(attribute, s); formatter.close();/*from w ww .j av a 2 s . co m*/ }
From source file:module.mission.presentationTier.dto.SearchMissionsDTO.java
public String getRequestParametersWithSort() { return getRequestParameters() + new Formatter().format("&sortBy=%s=%s", getSortByParameter(), getOrderByParameter()).toString(); }
From source file:models.Attachment.java
/** * Moves a file to the Upload Directory. * * This method is used to move a file stored in temporary directory by * PlayFramework to the Upload Directory managed by Yobi. * * @param file//ww w .j av a 2 s . c o m * @return SHA1 hash of the file * @throws NoSuchAlgorithmException * @throws IOException */ private static String moveFileIntoUploadDirectory(File file) throws NoSuchAlgorithmException, IOException { // Compute sha1 checksum. MessageDigest algorithm = MessageDigest.getInstance("SHA1"); byte buf[] = new byte[10240]; FileInputStream fis = new FileInputStream(file); for (int size = 0; size >= 0; size = fis.read(buf)) { algorithm.update(buf, 0, size); } Formatter formatter = new Formatter(); for (byte b : algorithm.digest()) { formatter.format("%02x", b); } String hash = formatter.toString(); formatter.close(); fis.close(); // Store the file. // Before do that, create upload directory if it doesn't exist. File uploads = new File(uploadDirectory); uploads.mkdirs(); if (!uploads.isDirectory()) { throw new NotDirectoryException("'" + file.getAbsolutePath() + "' is not a directory."); } File attachedFile = new File(uploadDirectory, hash); boolean isMoved = file.renameTo(attachedFile); if (!isMoved) { FileUtils.copyFile(file, attachedFile); file.delete(); } // Close all resources. return hash; }
From source file:org.apache.camel.component.mongodb.MongoDbOperationsTest.java
@Test public void testRemove() throws Exception { // Prepare test assertEquals(0, testCollection.count()); for (int i = 1; i <= 100; i++) { String body = null;/*from w w w . j a va 2 s . c o m*/ Formatter f = new Formatter(); if (i % 2 == 0) { body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\"}", i).toString(); } else { body = f.format("{\"_id\":\"testSave%d\", \"scientist\":\"Einstein\", \"extraField\": true}", i) .toString(); } template.requestBody("direct:insert", body); } assertEquals(100L, testCollection.count()); // Testing the update logic DBObject extraField = new BasicDBObject("extraField", true); assertEquals("Number of records with 'extraField' flag on must equal 50", 50L, testCollection.count(extraField)); Object result = template.requestBody("direct:remove", extraField); assertTrue(result instanceof WriteResult); assertEquals("Number of records with 'extraField' flag on must be 0 after remove", 0, testCollection.count(extraField)); }
From source file:com.flexive.war.beans.admin.main.StructureImportBean.java
/** * Refactorisation of the groovy upload//from w w w . j av a 2 s.c o m * * @param code the script code * @return returns true if the import / running the script went ok */ private boolean groovyUpload(String code) { final String fileName = "importConsole.groovy"; long start = System.currentTimeMillis(); try { result = ScriptConsoleBean.runScript(code, fileName, false); return true; } catch (Throwable t) { final StringWriter writer = new StringWriter(); t.printStackTrace(new PrintWriter(writer)); final String msg = t.getCause() != null ? t.getCause().getMessage() : t.getMessage(); result = new Formatter().format("Exception caught: %s%n%s", msg, writer.getBuffer()); return false; } finally { executionTime = System.currentTimeMillis() - start; } }