List of usage examples for com.google.common.base Strings padStart
public static String padStart(String string, int minLength, char padChar)
From source file:benchmarkio.controlcenter.LaunchRocket.java
@VisibleForTesting public static String createMessage(final double msgSizeInKB) { final int msgSizeInBytes = (int) (msgSizeInKB * 1024); // Java chars are 2 bytes final int msgSizeInChars = msgSizeInBytes / 2; final String message = Strings.padStart("", msgSizeInChars, 'a'); return message; }
From source file:org.rf.ide.core.executor.ArgumentsFile.java
File writeToTemporaryOrUseAlreadyExisting() throws IOException { final String content = generateContent(); // it's deprecated although we don't need security here, so md5 is fine @SuppressWarnings("deprecation") final HashFunction md5Hasher = Hashing.md5(); final HashCode hash = md5Hasher.hashString(content, Charsets.UTF_8); final String fileName = "args_" + Strings.padStart(Integer.toHexString(hash.asInt()), 8, '0') + ".arg"; final Path dir = RobotRuntimeEnvironment.createTemporaryDirectory(); for (final File existingArgFile : dir.toFile().listFiles((d, name) -> name.equals(fileName))) { final HashCode candidateHash = md5Hasher .hashString(Files.asCharSource(existingArgFile, Charsets.UTF_8).read(), Charsets.UTF_8); if (hash.equals(candidateHash)) { return existingArgFile; }/* w w w . j a v a2 s .com*/ } final File filePath = dir.resolve(fileName).toFile(); writeTo(filePath); return filePath; }
From source file:org.eclipse.xtext.formatting2.debug.TextRegionListToString.java
@Override public String toString() { int offsetDigits = 0; int lengthDigits = 0; for (Item item : items) { if (item.region != null) { int lengthD = String.valueOf(item.region.getLength()).length(); if (lengthDigits < lengthD) lengthDigits = lengthD;/*from w ww . ja va2 s . c om*/ int lengthO = String.valueOf(item.region.getOffset()).length(); if (offsetDigits < lengthO) offsetDigits = lengthO; } } List<String> result = Lists.newArrayListWithExpectedSize(items.size()); String prefix = Strings.repeat(" ", offsetDigits + lengthDigits + 2); for (Item item : items) { String[] lines = item.text.split("\n"); if (item.region != null) { String offset = Strings.padStart(String.valueOf(item.region.getOffset()), offsetDigits, ' '); String length = Strings.padStart(String.valueOf(item.region.getLength()), lengthDigits, ' '); if (lines.length == 1) { result.add(offset + " " + length + " " + lines[0]); } else { String offsetSpace = Strings.repeat(" ", offsetDigits); String lengthSpace = Strings.repeat(" ", lengthDigits); for (int i = 0; i < lines.length; i++) { String first = i == 0 ? offset : offsetSpace; String second = i == lines.length - 1 ? length : lengthSpace; result.add(first + " " + second + " " + lines[i]); } } } else if (item.indented) { for (int i = 0; i < lines.length; i++) result.add(prefix + lines[i]); } else { for (int i = 0; i < lines.length; i++) result.add(lines[i]); } } return Joiner.on("\n").join(result); }
From source file:org.eclipse.xtend.core.UnicodeTerminalsGenerator.java
public static StringWriter generateUnicodeRules(final Function1<? super Integer, ? extends Boolean> guard) { Character prev = null;//ww w. j ava 2 s .c o m boolean run = false; boolean first = true; char c = Character.MIN_VALUE; final StringWriter result = new StringWriter(); final PrintWriter printer = new PrintWriter(result, true); while ((c != Character.MAX_VALUE)) { { Boolean _apply = guard.apply(Integer.valueOf(((int) c))); if ((_apply).booleanValue()) { if ((!run)) { prev = Character.valueOf(c); run = true; } } else { if (run) { if ((!first)) { printer.print("| "); } else { printer.print(" "); first = false; } String _padStart = Strings.padStart(Integer.toHexString((prev).charValue()).toUpperCase(), 4, '0'); String _plus = ("\'\\u" + _padStart); String _plus_1 = (_plus + "\'"); printer.print(_plus_1); char _charValue = prev.charValue(); boolean _equals = (_charValue == (c - 1)); if (_equals) { printer.println(); } else { String _padStart_1 = Strings.padStart(Integer.toHexString((c - 1)).toUpperCase(), 4, '0'); String _plus_2 = ("..\'\\u" + _padStart_1); String _plus_3 = (_plus_2 + "\'"); printer.println(_plus_3); } prev = null; run = false; } } c = ((char) (c + 1)); } } return result; }
From source file:net.sourceforge.cilib.functions.discrete.BitStringMatcher.java
/** * {@inheritDoc}/*www . j av a2 s . c o m*/ */ @Override public Integer apply(Vector input) { if (this.targetRandomString == null) { this.numberOfBits = input.size(); BigInteger bi = new BigInteger(this.numberOfBits, new RandomAdaptor(new MersenneTwister())); this.targetRandomString = Strings.padStart(bi.toString(2), numberOfBits, '0'); } int result = 0; for (int i = 0; i < input.size(); i++) { boolean bitValue = (this.targetRandomString.charAt(i) == '1') ? true : false; if (input.booleanValueOf(i) == bitValue) { result++; } } return result; }
From source file:org.osiam.resource_server.storage.helper.NumberPadder.java
/** * Adds an offset and padding to a number * * @param value//from ww w. j a va 2 s . com * the number as {@link String} * @return the padded string with the added offset. */ public String pad(String value) { String integralPart = value; String fractionalPart = ""; if (value.contains(".")) { int indexOfDecimalSeparator = value.indexOf('.'); integralPart = value.substring(0, indexOfDecimalSeparator); fractionalPart = value.substring(indexOfDecimalSeparator); } // The max allowed length of the integral part depends on the presence of a '-' as first character. // If it is present 21 characters are allowed, otherwise 20 (This is done by applying indexOf magic). if (integralPart.length() > (PAD_LENGTH + integralPart.indexOf('-'))) { throw new IllegalArgumentException("The given value has more than " + (PAD_LENGTH - 1) + " digits."); } integralPart = new BigInteger(integralPart).add(BIG_OFFSET).toString(); integralPart = Strings.padStart(integralPart, PAD_LENGTH, '0'); return integralPart + fractionalPart; }
From source file:org.fenixedu.treasury.domain.paymentcodes.PaymentReferenceCode.java
protected void init(final String referenceCode, final LocalDate beginDate, final LocalDate endDate, final PaymentReferenceCodeStateType state, PaymentCodePool pool, BigDecimal minAmount, BigDecimal maxAmount) {/* w w w. j av a2 s . co m*/ setReferenceCode(Strings.padStart(referenceCode, LENGTH_REFERENCE_CODE, '0')); setBeginDate(beginDate); setEndDate(endDate); setState(state); setPaymentCodePool(pool); setMinAmount(minAmount); setMaxAmount(maxAmount); checkRules(); }
From source file:android.support.test.espresso.util.HumanReadables.java
/** * Prints out an error message feature the view hierarchy starting at the rootView. * * @param rootView the root of the hierarchy tree to print out. * @param problemViews list of the views that you would like to point out are causing the error * message or null, if you want to skip this feature. * @param errorHeader the header of the error message (should contain the description of why the * error is happening)./*from www. j a v a 2s . c o m*/ * @param problemViewSuffix the message to append to the view description in the tree printout. * Required if problemViews is supplied. Otherwise, null is acceptable. * @return a string for human consumption. */ public static String getViewHierarchyErrorMessage(View rootView, final List<View> problemViews, String errorHeader, final String problemViewSuffix) { Preconditions.checkArgument(problemViews == null || problemViewSuffix != null); StringBuilder errorMessage = new StringBuilder(errorHeader); if (problemViewSuffix != null) { errorMessage.append(String.format("\nProblem views are marked with '%s' below.", problemViewSuffix)); } errorMessage.append("\n\nView Hierarchy:\n"); Joiner.on("\n").appendTo(errorMessage, Iterables.transform(depthFirstViewTraversalWithDistance(rootView), new Function<ViewAndDistance, String>() { @Override public String apply(ViewAndDistance viewAndDistance) { String formatString = "+%s%s "; if (problemViews != null && problemViews.contains(viewAndDistance.getView())) { formatString += problemViewSuffix; } formatString += "\n|"; return String.format(formatString, Strings.padStart(">", viewAndDistance.getDistanceFromRoot() + 1, '-'), HumanReadables.describe(viewAndDistance.getView())); } })); return errorMessage.toString(); }
From source file:com.facebook.buck.debug.TraceEvent.java
/** * Return the sec.ms part of time (if time = "20:06:11.566", "11.566") * * @param time The time in milliseconds. * @return A formatted string as "sec.ms". */// w ww . j a v a 2s.c om static String formatTime(long time) { String sec = String.valueOf(Math.max(0, (time / 1000) % 60)); String ms = String.valueOf(Math.max(time % 1000, 0)); return Strings.padStart(sec, 2, '0') + '.' + Strings.padStart(ms, 3, '0'); }
From source file:com.davidbracewell.wordnet.io.properties.InformationContentLoader.java
@Override public void load(WordNetDB db) { Counter<String> ic = Counters.newHashMapCounter(); Counter<WordNetPOS> roots = Counters.newHashMapCounter(); for (String line : resource) { line = line.trim();/*from w ww. ja va 2 s. c om*/ if (StringUtils.isNullOrBlank(line) || line.startsWith("wnver")) { continue; } String[] parts = line.split("\\s+"); String key = parts[0]; double cnt = Double.parseDouble(parts[1]); ic.set(key, cnt); if (parts.length == 3 && parts[2].equalsIgnoreCase("ROOT")) { roots.increment(WordNetPOS.fromString(key.substring(key.length() - 1)), cnt); } } for (String key : ic.items()) { Synset synset = db.idToSynsetMap.get(Strings.padStart(key, 9, '0').toLowerCase()); setProperty(synset, propertyName, new InformationContent(ic.get(key) / roots.get(synset.getPOS()))); } }