List of usage examples for java.lang StringBuffer reverse
@Override public synchronized StringBuffer reverse()
From source file:Unsigned.java
/** * Return the unsigned value of the number. * /*w w w . j av a 2 s . co m*/ * @param number * a byte value * @return the unsigned value */ public static long unsigned(byte number) { long result = 0; BinaryFormat format = new BinaryFormat(); String binary = format.format(number); StringBuffer buffer = new StringBuffer(binary); buffer.reverse(); int length = buffer.length(); for (int i = 0; i < length; i++) { result += (buffer.charAt(i) == '1') ? 1 << i : 0; } return (result); }
From source file:Unsigned.java
/** * Return the unsigned value of the number. * // w w w.j ava 2s .c o m * @param number * a short value * @return the unsigned value */ public static long unsigned(short number) { long result = 0; BinaryFormat format = new BinaryFormat(); String binary = format.format(number); StringBuffer buffer = new StringBuffer(binary); buffer.reverse(); int length = buffer.length(); for (int i = 0; i < length; i++) { result += (buffer.charAt(i) == '1') ? 1 << i : 0; } return (result); }
From source file:Unsigned.java
/** * Return the unsigned value of the number. * // w ww . ja va 2 s . c o m * @param number * an int value * @return the unsigned value */ public static long unsigned(int number) { long result = 0; BinaryFormat format = new BinaryFormat(); String binary = format.format(number); StringBuffer buffer = new StringBuffer(binary); buffer.reverse(); int length = buffer.length(); for (int i = 0; i < length; i++) { result += (buffer.charAt(i) == '1') ? 1 << i : 0; } return (result); }
From source file:azkaban.common.utils.Utils.java
/** * Parse lines in byteArray and store the last *lineCount* lines in * *lastNLines*/*from w w w . j a va 2 s. c o m*/ * * @param byteArray source byte array * @param offset offset of the byte array * @param length length of the byte array * @param lineCount desired number of lines * @param lastNLines vector of last N lines * @return true indicates we get *lineCount* lines * false otherwise */ protected static boolean parseLinesFromLast(byte[] byteArray, int offset, int length, int lineCount, Vector<String> lastNLines) { if (lastNLines.size() > lineCount) return true; // convert byte array to string String lastNChars = new String(byteArray, offset, length); // reverse the string StringBuffer sb = new StringBuffer(lastNChars); lastNChars = sb.reverse().toString(); // tokenize the string using "\n" String[] tokens = lastNChars.split("\n"); // append lines to lastNLines for (int index = 0; index < tokens.length; index++) { StringBuffer sbLine = new StringBuffer(tokens[index]); String newline = sbLine.reverse().toString(); if (index == 0 && !lastNLines.isEmpty()) { // first line might not be a complete line int lineNum = lastNLines.size(); String halfLine = lastNLines.get(lineNum - 1); lastNLines.set(lineNum - 1, newline + halfLine); } else { lastNLines.add(newline); } if (lastNLines.size() > lineCount) { return true; } } return false; }
From source file:gov.gtas.parsers.util.FlightUtils.java
/** * Separate flight carrier and flight number from single input string. * @param s/*www . j a v a 2 s. c o m*/ * @return */ public static FlightNumber separateCarrierAndFlightNumber(String s) { StringBuffer fn = new StringBuffer(); int j; for (j = s.length() - 1; j >= 0; j--) { char c = s.charAt(j); if (Character.isDigit(c)) { fn.append(c); if (s.length() - fn.length() == MIN_CARRIER_LENG) { break; } else if (fn.length() == MAX_FLIGHT_NUM_LENG) { break; } } else { break; } } String carrier = s.substring(0, s.length() - fn.length()); return new FlightNumber(carrier, fn.reverse().toString()); }
From source file:cerrla.Performance.java
/** * Reads a raw numerical performance file and stores the values as * accessible private values./*from w ww. j a v a 2 s.co m*/ * * @param perfFile * The performance file to read. * @return True if the file was read successfully, false otherwise. */ public static boolean readRawPerformanceFile(File perfFile, boolean byEpisode) throws Exception { if (Config.getInstance().getGeneratorFile() == null) { // First, read the last line of the normal file for the time RandomAccessFile raf = new RandomAccessFile(perfFile, "r"); long pos = perfFile.length() - 1; StringBuffer line = new StringBuffer(); char c; boolean foundIt = false; do { raf.seek(pos); c = (char) raf.read(); foundIt |= Character.isDigit(c); line.append(c); pos--; } while (!foundIt || Character.isDigit(c) || c == ':'); raf.close(); String time = line.reverse().toString().trim(); String[] timeSplit = time.split(":"); runTime_ = (Long.parseLong(timeSplit[2]) + 60 * Long.parseLong(timeSplit[1]) + 3600 * Long.parseLong(timeSplit[0])) * 1000; } if (Config.getInstance().getGeneratorFile() == null) perfFile = new File(perfFile.getPath() + "raw"); else perfFile = new File(perfFile.getPath() + "greedy"); performanceMap_ = new TreeMap<Integer, Float[]>(); FileReader reader = new FileReader(perfFile); BufferedReader buf = new BufferedReader(reader); // For every value within the performance file String input = null; Float[] prevPerfs = null; while ((input = buf.readLine()) != null) { String[] vals = input.split("\t"); if (vals[PerformanceDetails.EPISODE.ordinal()].equals("Episode")) continue; Float[] perfs = new Float[PerformanceDetails.values().length]; int episode = 0; for (PerformanceDetails detail : PerformanceDetails.values()) { if (vals.length > detail.ordinal()) { if (!vals[detail.ordinal()].equals("null")) perfs[detail.ordinal()] = Float.parseFloat(vals[detail.ordinal()]); else if (detail.equals(PerformanceDetails.ELITEMEAN) && !vals[PerformanceDetails.ELITEMAX.ordinal()].equals("null")) perfs[detail.ordinal()] = Float.parseFloat(vals[PerformanceDetails.ELITEMAX.ordinal()]); else if (detail.equals(PerformanceDetails.ELITEMEAN) || detail.equals(PerformanceDetails.ELITEMAX)) perfs[detail.ordinal()] = Float.parseFloat(vals[PerformanceDetails.MEAN.ordinal()]); else if (prevPerfs != null) perfs[detail.ordinal()] = prevPerfs[detail.ordinal()]; } if (detail.equals(PerformanceDetails.EPISODE)) episode = perfs[detail.ordinal()].intValue(); } performanceMap_.put(episode, perfs); prevPerfs = perfs; } buf.close(); reader.close(); return true; }
From source file:TextBoxMIDlet.java
public void commandAction(Command c, Displayable d) { if (c == EXIT_COMMAND) { destroyApp(true);/*from w ww . j av a2 s . com*/ notifyDestroyed(); } else if (c == OK_COMMAND) { System.out.println("OK pressed"); } else if (c == CLEAR_COMMAND) { textBox.setString(null); } else if (c == REVERSE_COMMAND) { String str = textBox.getString(); if (str != null) { StringBuffer sb = new StringBuffer(str); textBox.setString(sb.reverse().toString()); } } }
From source file:gov.jgi.meta.MetaUtils.java
/** * given a sequence, return the reverse complement * @param s sequence// ww w. jav a2 s . co m * @return its reverse complement */ public static String reverseComplement(String s) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == 'a') { sb.append("t"); } else if (s.charAt(i) == 't') { sb.append("a"); } else if (s.charAt(i) == 'g') { sb.append("c"); } else if (s.charAt(i) == 'c') { sb.append("g"); } else if (s.charAt(i) == 'n') { sb.append("n"); } } return (sb.reverse().toString()); }
From source file:net.ljcomputing.core.controler.ErrorInfo.java
/** * Instantiates a new error info./*from w w w .j a v a 2 s. c o m*/ * * @param timestamp the timestamp * @param status the status * @param path the path * @param exception the exception */ public ErrorInfo(String timestamp, HttpStatus status, String path, Exception exception) { this.timestamp = timestamp; this.status = Integer.valueOf(status.value()).toString(); this.error = status.getReasonPhrase(); this.path = path; if (exception instanceof ConstraintViolationException) { StringBuffer eb = new StringBuffer("The save failed validation as follows: "); for (ConstraintViolation<?> cv : ((ConstraintViolationException) exception).getConstraintViolations()) { eb.append(cv.getMessage() + ","); } eb.reverse().replace(0, 1, "").reverse(); this.message = eb.toString(); } else if (null != exception.getLocalizedMessage()) { this.message = exception.getLocalizedMessage(); } else if (null != exception.getMessage()) { this.message = exception.getMessage(); } else { this.message = "An error occured during processing: " + exception.toString(); } }
From source file:org.apache.ode.utils.DOMUtils.java
/** * Parse the text in the cloneChild for any embedded prefixes, and define it in it's parent element * * @param sourceNode/*from w w w . j a v a 2 s .c om*/ * @param clonedNode * @param clonedChild */ private static void parseEmbeddedPrefixes(Node sourceNode, Node clonedNode, Node clonedChild) { Element clonedElement = null; if (clonedNode instanceof Attr) { clonedElement = ((Attr) clonedNode).getOwnerElement(); } else if (clonedNode instanceof Element) { clonedElement = (Element) clonedNode; } if (clonedElement == null) { // couldn't find an element to set prefixes on, so bail out return; } String text = ((Text) clonedChild).getNodeValue(); if (text != null && text.indexOf(":") > 0) { Name11Checker nameChecker = Name11Checker.getInstance(); for (int colonIndex = text.indexOf(":"); colonIndex != -1 && colonIndex < text.length(); colonIndex = text.indexOf(":", colonIndex + 1)) { StringBuffer prefixString = new StringBuffer(); for (int prefixIndex = colonIndex - 1; prefixIndex >= 0 && nameChecker.isNCNameChar(text.charAt(prefixIndex)); prefixIndex--) { prefixString.append(text.charAt(prefixIndex)); } prefixString.reverse(); if (prefixString.length() > 0) { String uri = sourceNode.lookupNamespaceURI(prefixString.toString()); if (uri != null) { clonedElement.setAttributeNS(NS_URI_XMLNS, "xmlns:" + prefixString, uri); } } } } }