@Test public void singleValueField_WithMaxSize() throws Exception { SearchResponse response = client() .prepareSearch("idx") .setTypes("high_card_type") .addAggregation( terms("terms") .executionHint(randomExecutionHint()) .field(SINGLE_VALUED_FIELD_NAME) .size(20) .order( Terms.Order.term( true))) // we need to sort by terms cause we're checking the first 20 // values .execute() .actionGet(); assertSearchResponse(response); Terms terms = response.getAggregations().get("terms"); assertThat(terms, notNullValue()); assertThat(terms.getName(), equalTo("terms")); assertThat(terms.getBuckets().size(), equalTo(20)); for (int i = 0; i < 20; i++) { Terms.Bucket bucket = terms.getBucketByKey("val" + Strings.padStart(i + "", 3, '0')); assertThat(bucket, notNullValue()); assertThat(key(bucket), equalTo("val" + Strings.padStart(i + "", 3, '0'))); assertThat(bucket.getDocCount(), equalTo(1l)); } }
public void outputTable(int width, PrintStream out) { // Heading for (final Entry<String, Interval> e : entrySet()) out.print(Strings.padStart(e.getKey(), width, ' ') + " "); out.println(); // Values for (final Entry<String, Interval> e : entrySet()) out.print(Strings.padStart(e.getValue().toString(), width, ' ') + " "); out.println(); }
/** * returns formatted cells with 6 columns. * * @return */ public String getScheduleAsAsciiTable() { String[][] scheduleRaw = getSchedule(); StringBuilder schedule = new StringBuilder(); // loan amount will be the largest number in the table // so this gives a good size for the table int cellWidth = (amountLoan + "").length(); if (cellWidth < 5) { cellWidth = 5; } schedule.append(Strings.padStart("1*", cellWidth, ' ')); schedule.append(Strings.padStart("2*", cellWidth, ' ')); schedule.append(Strings.padStart("3*", cellWidth, ' ')); schedule.append(Strings.padStart("4*", cellWidth, ' ')); schedule.append(Strings.padStart("5*", cellWidth, ' ')); schedule.append(Strings.padStart("6*", cellWidth, ' ')); schedule.append("\n"); schedule.append(Strings.padStart("", cellWidth * 6, '-')); schedule.append("\n"); for (int i = 0; i < scheduleRaw.length; i++) { for (int j = 0; j < scheduleRaw[i].length; j++) { schedule.append(Strings.padStart(scheduleRaw[i][j], cellWidth, ' ')); } schedule.append("\n"); } Log.d("", schedule.toString()); return schedule.toString(); }
@Before public void init() throws Exception { createIndex("idx"); IndexRequestBuilder[] lowCardBuilders = new IndexRequestBuilder[5]; // TODO randomize the size? for (int i = 0; i < lowCardBuilders.length; i++) { lowCardBuilders[i] = client() .prepareIndex("idx", "type") .setSource( jsonBuilder() .startObject() .field(SINGLE_VALUED_FIELD_NAME, "val" + i) .field("i", i) .field("tag", i < lowCardBuilders.length / 2 + 1 ? "more" : "less") .startArray(MULTI_VALUED_FIELD_NAME) .value("val" + i) .value("val" + (i + 1)) .endArray() .endObject()); } indexRandom(true, lowCardBuilders); IndexRequestBuilder[] highCardBuilders = new IndexRequestBuilder[100]; // TODO randomize the size? for (int i = 0; i < highCardBuilders.length; i++) { highCardBuilders[i] = client() .prepareIndex("idx", "high_card_type") .setSource( jsonBuilder() .startObject() .field(SINGLE_VALUED_FIELD_NAME, "val" + Strings.padStart(i + "", 3, '0')) .startArray(MULTI_VALUED_FIELD_NAME) .value("val" + Strings.padStart(i + "", 3, '0')) .value("val" + Strings.padStart((i + 1) + "", 3, '0')) .endArray() .endObject()); } indexRandom(true, highCardBuilders); createIndex("idx_unmapped"); ensureSearchable(); }
@Override public StringBuffer format( final Date date, final StringBuffer toAppendTo, final FieldPosition fieldPosition) { StringBuffer out = toAppendTo == null ? new StringBuffer() : toAppendTo; if (date != null) { out.append(date.getTime() / 1000); out.append('.'); out.append(Strings.padStart(Long.toString(date.getTime() % 1000), 3, '0')); } return out; }
@Override public void serialize( final Date date, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeRawValue( String.valueOf(date.getTime() / 1000) + "." + Strings.padStart(Long.toString(date.getTime() % 1000), 3, '0')); }
private String getPeriodReadable(int periodLifetime) { switch (time) { case ANNUAL: return (periodLifetime + 1) + ""; case QUARTERLY: return ((periodLifetime % 4) + 1) + "/" + ((periodLifetime / 4) + 1); case MONTHLY: return Strings.padStart(((periodLifetime % 12) + 1) + "", 2, '0') + "/" + ((periodLifetime / 12) + 1); default: return null; } }
private static String getPaddedGenerationID(long generationID) { return Strings.padStart(Long.toString(generationID), 5, '0'); }
/** * Returns a string, of length at least {@code minLength}, consisting of {@code string} prepended * with as many copies of {@code padChar} as are necessary to reach that length. For example, * * <ul> * <li>{@code padStart("7", 3, '0')} returns {@code "007"} * <li>{@code padStart("2010", 3, '0')} returns {@code "2010"} * </ul> * * <p>See {@link Formatter} for a richer set of formatting capabilities. * * @param string the string which should appear at the end of the result * @param minLength the minimum length the resulting string must have. Can be zero or negative, in * which case the input string is always returned. * @param padChar the character to insert at the beginning of the result until the minimum length * is reached * @return the padded string * @see Strings#padStart(String, int, char) */ public static String padStart(final String string, final int minLength, final char padChar) { return Strings.padStart(string, minLength, padChar); }
private static String pad(int padding) { return Strings.padStart("", padding * 4, ' '); }
public String getSha1Fingerprint() throws CertificateEncodingException, NoSuchAlgorithmException { MessageDigest messageDigest = MessageDigest.getInstance("SHA1"); messageDigest.update(cert.getEncoded()); String digest = new BigInteger(1, messageDigest.digest()).toString(RADIX); return Strings.padStart(digest, SIZE, '0'); }
@Override public DMSFile save(DMSFile entity) { final DMSFile parent = entity.getParent(); final Model related = findRelated(entity); final boolean isAttachment = related != null && entity.getMetaFile() != null; // if new attachment, save attachment reference if (isAttachment) { // remove old attachment if file is moved MetaAttachment attachmentOld = attachments .all() .filter( "self.metaFile.id = ? AND self.objectId != ? AND self.objectName != ?", entity.getMetaFile().getId(), related.getId(), related.getClass().getName()) .fetchOne(); if (attachmentOld != null) { System.err.println("OLD: " + attachmentOld); attachments.remove(attachmentOld); } MetaAttachment attachment = attachments .all() .filter( "self.metaFile.id = ? AND self.objectId = ? AND self.objectName = ?", entity.getMetaFile().getId(), related.getId(), related.getClass().getName()) .fetchOne(); if (attachment == null) { attachment = metaFiles.attach(entity.getMetaFile(), related); attachments.save(attachment); } } // if not an attachment or has parent, do nothing if (parent != null || related == null) { return super.save(entity); } // create parent folders Mapper mapper = Mapper.of(related.getClass()); String homeName = null; try { homeName = mapper.getNameField().get(related).toString(); } catch (Exception e) { } if (homeName == null) { homeName = Strings.padStart("" + related.getId(), 5, '0'); } DMSFile dmsRoot = all() .filter( "(self.relatedId is null OR self.relatedId = 0) AND self.relatedModel = ? and self.isDirectory = true", entity.getRelatedModel()) .fetchOne(); final Inflector inflector = Inflector.getInstance(); if (dmsRoot == null) { dmsRoot = new DMSFile(); dmsRoot.setFileName( inflector.pluralize(inflector.humanize(related.getClass().getSimpleName()))); dmsRoot.setRelatedModel(entity.getRelatedModel()); dmsRoot.setIsDirectory(true); dmsRoot = super.save(dmsRoot); // should get id before it's child } DMSFile dmsHome = new DMSFile(); dmsHome.setFileName(homeName); dmsHome.setRelatedId(entity.getRelatedId()); dmsHome.setRelatedModel(entity.getRelatedModel()); dmsHome.setParent(dmsRoot); dmsHome.setIsDirectory(true); dmsHome = super.save(dmsHome); // should get id before it's child entity.setParent(dmsHome); return super.save(entity); }