List of usage examples for java.lang StringBuilder deleteCharAt
@Override public StringBuilder deleteCharAt(int index)
From source file:Main.java
/** * Used for flattening a collection of objects into a string * @param array Array of elements to flatten * @param fmt Format string to use for array flattening * @param separator Separator to use for string concat * @return Representative string made up of array elements */ private static String flattenCollection(String elemName, Collection<?> array, String fmt, char separator) { StringBuilder builder = new StringBuilder(); //append all elements in the array into a string for (Object element : array) { String elemValue = null;/* w w w . ja v a2s.c om*/ //replace null values with empty string to maintain index order if (null == element) { elemValue = ""; builder.append(String.format(fmt, elemName, elemValue, separator)); } else { elemValue = tryUrlEncode(element.toString()); builder.append(String.format(fmt, elemName, elemValue, separator)); } } //remove the last separator, if appended if ((builder.length() > 1) && (builder.charAt(builder.length() - 1) == separator)) builder.deleteCharAt(builder.length() - 1); return builder.toString(); }
From source file:org.fastj.fit.tool.JSONHelper.java
public static Object jsonValue(String path, Map<String, Object> jo) throws DataInvalidException { //find(), data(), loop(), unique(), asc(), desc() String regex = "^[\\S\\s]*\\[((find|data|loop|unique|asc|desc)\\(\\))\\][\\S\\s]*$"; String regex1 = "\\[((find|data|loop|unique|asc|desc)\\(\\))\\]"; if (path.matches(regex)) { Matcher m = Pattern.compile(regex1).matcher(path); String op = null;//from w ww . java2 s.c om if (m.find()) { op = m.group(2); } if (m.find()) throw new DataInvalidException("JSON path invalid: more op."); String sizePath = path.replaceAll(regex1, "[size()]"); Object leno = jsonValueInner(sizePath, jo); if ("nil".equals(leno) || leno == null) { if ("loop".equals(op) || "find".equals(op)) return new ArrayList<>(); return "nil"; } int lsize = (Integer) leno; List<Object> vl = new ArrayList<>(); for (int i = 0; i < lsize; i++) { String npath = path.replaceAll(regex1, "[" + i + "]"); Object o = jsonValueInner(npath, jo); if (!"nil".equals(o) && !"null".equals(o) && null != o) { vl.add(o); } } switch (op) { case "loop": case "find": return vl; case "data": StringBuilder buff = new StringBuilder("@data:"); for (Object o : vl) { buff.append(String.valueOf(o)).append(","); } if (buff.charAt(buff.length() - 1) == ',') buff.deleteCharAt(buff.length() - 1); return buff.toString(); case "unique": List<Object> chkl = new ArrayList<Object>(); for (Object o : vl) { if (chkl.contains(o)) return "false"; chkl.add(o); } return "true"; case "asc": List<String> svl3 = new ArrayList<>(); for (int i = 0; i < vl.size(); i++) { svl3.add(String.valueOf(vl.get(i))); } List<String> svl4 = new ArrayList<>(); svl4.addAll(svl3); Collections.sort(svl4); for (int i = 0; i < svl3.size(); i++) { if (svl3.get(i).equals(svl4.get(i))) { continue; } else { return "false"; } } return "true"; case "desc": List<String> svl = new ArrayList<>(); for (int i = 0; i < vl.size(); i++) { svl.add(String.valueOf(vl.get(i))); } List<String> svl2 = new ArrayList<>(); svl2.addAll(svl); Collections.sort(svl2); int len = svl.size(); for (int i = 0; i < svl.size(); i++) { if (svl.get(i).equals(svl2.get(len - i - 1))) { continue; } else { return "false"; } } return "true"; default: return "nil"; } } else { return jsonValueInner(path, jo); } }
From source file:loci.apps.SlideScannerImport.VentanaScannerInterpreter.java
public static String removeNullFromString(String line) { if (line == null) return null; StringBuilder sb = new StringBuilder(line); char[] temp = line.toCharArray(); int counter = 0; for (int c = 0; c < temp.length; c++) { if (temp[c] == '\0') { sb.deleteCharAt(c - counter); counter++;/*w ww.j ava2s . c o m*/ } } return sb.toString(); }
From source file:com.impetus.client.couchdb.CouchDBUtils.java
/** * Creates the view./* w ww. jav a 2 s .co m*/ * * @param views * the views * @param columnName * the column name * @param columns * the columns */ static void createView(Map<String, MapReduce> views, String columnName, List<String> columns) { Iterator<String> iterator = columns.iterator(); MapReduce mapr = new MapReduce(); StringBuilder mapBuilder = new StringBuilder(); StringBuilder ifBuilder = new StringBuilder("function(doc){if("); StringBuilder emitFunction = new StringBuilder("{emit("); if (columns != null && columns.size() > 1) { emitFunction.append("["); } while (iterator.hasNext()) { String nextToken = iterator.next(); ifBuilder.append("doc." + nextToken); ifBuilder.append(" && "); emitFunction.append("doc." + nextToken); emitFunction.append(","); } ifBuilder.delete(ifBuilder.toString().lastIndexOf(" && "), ifBuilder.toString().lastIndexOf(" && ") + 3); emitFunction.deleteCharAt(emitFunction.toString().lastIndexOf(",")); ifBuilder.append(")"); if (columns != null && columns.size() > 1) { emitFunction.append("]"); } emitFunction.append(", doc)}}"); mapBuilder.append(ifBuilder.toString()).append(emitFunction.toString()); mapr.setMap(mapBuilder.toString()); views.put(columnName, mapr); }
From source file:hu.bme.mit.sette.run.Run.java
private static void exportCSV(SnippetProject snippetProject, File file) throws Exception { // TODO enhance this method Tool[] tools = ToolRegister.toArray(); Arrays.sort(tools, new Comparator<Tool>() { @Override/*from w w w . j a va 2s . c om*/ public int compare(Tool o1, Tool o2) { return o1.getName().compareToIgnoreCase(o2.getName()); } }); List<String> columns = new ArrayList<>(); columns.add("Category"); columns.add("Goal"); columns.add("Container"); columns.add("Required Java version"); columns.add("Snippet"); columns.add("Required coverage"); Map<Tool, RunnerProjectSettings<Tool>> rpss = new HashMap<>(); ResultType[] resultTypes = ResultType.values(); for (Tool tool : tools) { rpss.put(tool, new RunnerProjectSettings<>(snippetProject.getSettings(), OUTPUT_DIR, tool)); for (ResultType resultType : resultTypes) { columns.add(resultType.toString() + " - " + tool.getName()); } } StringBuilder sb = new StringBuilder(); for (String column : columns) { sb.append(column).append(','); } sb.deleteCharAt(sb.length() - 1); sb.append("\n"); for (SnippetContainer container : snippetProject.getModel().getContainers()) { for (Snippet snippet : container.getSnippets().values()) { sb.append(container.getCategory()).append(","); sb.append(container.getGoal()).append(","); sb.append(container.getJavaClass().getName()).append(","); sb.append(container.getRequiredJavaVersion()).append(","); sb.append(snippet.getMethod().getName()).append(","); sb.append(snippet.getRequiredStatementCoverage() + "%"); for (Tool tool : tools) { RunnerProjectSettings<Tool> set = rpss.get(tool); File inputs = RunnerProjectUtils.getSnippetInputsFile(set, snippet); File result = RunnerProjectUtils.getSnippetResultFile(set, snippet); if (result.exists()) { System.out.println(tool.getFullName()); System.out.println(snippet.getMethod()); // TODO error handling throw new RuntimeException("RESULT EXISTS"); } ResultType rt; SnippetInputsXml snippetInputsXml; if (!inputs.exists()) { // TODO input should exist, revise this section // System.out.println(tool.getFullName()); // System.out.println(snippet.getMethod()); // throw new RuntimeException("INPUT NOT EXISTS"); rt = ResultType.NA; } else { Serializer serializer = new Persister(new AnnotationStrategy()); snippetInputsXml = serializer.read(SnippetInputsXml.class, inputs); snippetInputsXml.validate(); rt = snippetInputsXml.getResultType(); } int pos = ArrayUtils.indexOf(resultTypes, rt); for (int i = 0; i < pos; i++) { sb.append(","); } sb.append(",1"); for (int i = pos + 1; i < resultTypes.length; i++) { sb.append(","); } } sb.append("\n"); } } try { FileUtils.write(file, sb); } catch (IOException e) { System.err.println("Operation failed"); e.printStackTrace(); } // System.out.println(sb.toString()); // StringBuilder sb = new StringBuilder(testCaseToolInputs.size() * // 100); // // if (testCaseToolInputs.size() <= 0) // return sb.append("No data"); // // List<Tool> tools = new ArrayList<>(testCases.get(0) // .generatedToolInputs().keySet()); // Collections.sort(tools); // // sb.append(";;"); // for (Tool tool : tools) { // sb.append(';').append(tool.getName()).append(";;;;;"); // } // // sb.append('\n'); // // sb.append("Package;Class;Test case"); // // for (int i = 0; i < tools.size(); i++) { // sb.append(";N/A;EX;T/M;NC;C;Note"); // } // // sb.append('\n'); // // Collections.sort(testCases); // // for (TestCase tc : testCases) { // sb.append(tc.getPkg()).append(';'); // sb.append(tc.getCls()).append(';'); // sb.append(tc.getName()); // // for (Tool tool : tools) { // TestCaseToolInput tcti = tc.generatedToolInputs().get(tool); // // switch (tcti.getResult()) { // case NA: // // sb.append(";1;0;0;0;0;"); // sb.append(";X;;;;;"); // break; // case EX: // // sb.append(";0;1;0;0;0;"); // sb.append(";;X;;;;"); // break; // case TM: // // sb.append(";0;0;1;0;0;"); // sb.append(";;;X;;;"); // break; // case NC: // // sb.append(";0;0;0;1;0;"); // sb.append(";;;;X;;"); // break; // case C: // // sb.append(";0;0;0;0;1;"); // sb.append(";;;;;X;"); // break; // case UNKNOWN: // default: // sb.append(";UNKNOWN;UNKNOWN;UNKNOWN;UNKNOWN;UNKNOWN;"); // break; // // } // // sb.append(tcti.getNote()); // } // // sb.append('\n'); // } // // return sb; }
From source file:com.p000ison.dev.simpleclans2.api.chat.ChatBlock.java
/** * Crops the string right in the {@link StringBuilder}. * * @param text The message to crop right. * @param length The lenght it of the section it should be crop right. *//*from w ww .j a v a 2 s . co m*/ public static void cropRight(StringBuilder text, double length) { if (text == null || text.length() == 0) { throw new IllegalArgumentException("The text can not be null or empty!"); } while (msgLength(text) >= length) { text.deleteCharAt(text.length() - 1); } }
From source file:com.p000ison.dev.simpleclans2.api.chat.ChatBlock.java
/** * Crops the string left in the {@link StringBuilder}. * * @param text The message to crop left. * @param length The lenght it of the section it should be crop left. *//*w w w. j ava2s.co m*/ public static void cropLeft(StringBuilder text, double length) { if (text == null || text.length() == 0) { throw new IllegalArgumentException("The text can not be null or empty!"); } while (msgLength(text) >= length) { text.deleteCharAt(0); } }
From source file:com.vmware.bdd.cli.commands.CommandsUtils.java
public static String prettyRoleOutput(List<String> roles, final String delimiter) { StringBuilder roleStr = new StringBuilder(); if (roles != null) { for (String role : roles) { roleStr.append(role + delimiter); }// w w w .ja v a 2s.c om } if (roleStr.length() > 0) { roleStr.deleteCharAt(roleStr.length() - 1); } return roleStr.toString(); }
From source file:Main.java
/** * Used for flattening a collection of objects into a string * @param array Array of elements to flatten * @param fmt Format string to use for array flattening * @param separator Separator to use for string concat * @return Representative string made up of array elements *///from w w w . j a va2s . c o m private static String flattenCollection(String elemName, Collection<?> array, String fmt, char separator) { StringBuilder builder = new StringBuilder(); //append all elements in the array into a string for (Object element : array) { String elemValue = null; //replace null values with empty string to maintain index order if (null == element) { elemValue = ""; } else if (element instanceof Date) { elemValue = dateToString((Date) element); } else { elemValue = element.toString(); } elemValue = tryUrlEncode(elemValue); builder.append(String.format(fmt, elemName, elemValue, separator)); } //remove the last separator, if appended if ((builder.length() > 1) && (builder.charAt(builder.length() - 1) == separator)) builder.deleteCharAt(builder.length() - 1); return builder.toString(); }
From source file:eu.digitisation.idiomaident.utils.ExtractTestSamples.java
private static String nextSample(File inFolder) { File[] langFolders = inFolder.listFiles(); Date now = new Date(); Random rand = new Random(now.getTime()); //get a ramdom language int numLangs = langFolders.length; int chosenLang = rand.nextInt(numLangs); String lang = langFolders[chosenLang].getName(); //get a ramdom file from the folder File[] langFiles = langFolders[chosenLang].listFiles(); int numFiles = langFiles.length; int chosenFile = rand.nextInt(numFiles); File langFile = langFiles[chosenFile]; //open and read the file try {/*from w ww .j a va 2 s .c o m*/ String text = FileUtils.readFileToString(langFile, Charset.forName("UTF-8")); text = StringNormalize.stringNormalize(text); text = text.replaceAll("[\\n.,:;]", " "); text = text.trim(); if (text.replaceAll("[\\p{Space}]+", "").length() < 20) { return null; } //split the text in words String[] words = text.split("[\\p{Space}]+"); boolean correct = false; StringBuilder sampleBuild = null; while (!correct) { //chosen the initial position to read the words int actualWord = rand.nextInt(words.length); correct = true; sampleBuild = new StringBuilder(); while (sampleBuild.length() < 20) { if (actualWord < words.length) { sampleBuild.append(words[actualWord]); sampleBuild.append(" "); } else { correct = false; break; } actualWord++; } } //complete the sample sampleBuild.deleteCharAt(sampleBuild.length() - 1); sampleBuild.append(";").append(lang).append("\n"); return sampleBuild.toString(); } catch (IOException ex) { System.out.println(ex.toString()); } return null; }