/** returns a configurable shortener */ public static StringShortener shortener() { return new StringShortener(); }
public StringShortener separator(String separator) { this.separator = removeDisallowedCharacters(separator); return this; }
@Test public void testSimpleShortener() { StringShortener ss = new StringShortener() .separator("-") .append("1", "hello") .append("2", "new") .append("3", "world") .canRemove("2") .canTruncate("1", 2) .canTruncate("3", 2); Assert.assertEquals(ss.getStringOfMaxLength(12), "hello-world"); Assert.assertEquals(ss.getStringOfMaxLength(10), "hell-world"); Assert.assertEquals(ss.getStringOfMaxLength(6), "he-wor"); Assert.assertEquals(ss.getStringOfMaxLength(5), "he-wo"); Assert.assertEquals(ss.getStringOfMaxLength(4), "he-w"); Assert.assertEquals(ss.getStringOfMaxLength(0), ""); }
shortener.setAllowedCharacters(setup.get(CloudLocationConfig.VM_NAME_ALLOWED_CHARACTERS)); shortener.separator("-"); shortener.append("system", "brooklyn"); shortener.append("timeStamp", Long.toString(System.currentTimeMillis() / 1000L, Character.MAX_RADIX)); shortener.append("user", user); shortener.append("app", shortName(app)) .append("appId", app.getId()); shortener.append("entity", shortName(entity)) .append("entityId", entity.getId()); } else if (context!=null) { shortener.append("context", context.toString()); shortener.truncate("user", 12) .truncate("app", 16) .truncate("entity", 16) .truncate("appId", 4) .truncate("entityId", 4) .truncate("context", 12); shortener.canTruncate("user", 8) .canTruncate("app", 5) .canTruncate("entity", 5) .canTruncate("system", 2) .canTruncate("app", 3)
protected void runTest(Map<String,?> flags) throws Exception { StringShortener shortener = Strings.shortener().separator("-"); shortener.canTruncate(getClass().getSimpleName(), MAX_TAG_LENGTH); Map<String,?> allFlags = MutableMap.<String,Object>builder() .put("tags", ImmutableList.of(shortener.getStringOfMaxLength(MAX_TAG_LENGTH))) .put("vmNameMaxLength", MAX_VM_NAME_LENGTH) .putAll(flags) .build(); jcloudsLocation = mgmt.getLocationRegistry().getLocationManaged(PROVIDER, allFlags); doTest(jcloudsLocation); }
@Test public void testDisallowedCharactersShortener() { StringShortener ss = new StringShortener() .setAllowedCharacters(Identifiers.UPPER_CASE_ALPHA+Identifiers.LOWER_CASE_ALPHA+Identifiers.NUMERIC) .separator("-") .append("1", "he-llo") .append("2", "_new") .append("3", "w.o.r.l.d") .canRemove("2") .canTruncate("1", 2) .canTruncate("3", 2); Assert.assertEquals(ss.getStringOfMaxLength(12), "helloworld"); Assert.assertEquals(ss.getStringOfMaxLength(9), "hellworld"); Assert.assertEquals(ss.getStringOfMaxLength(6), "heworl"); Assert.assertEquals(ss.getStringOfMaxLength(5), "hewor"); Assert.assertEquals(ss.getStringOfMaxLength(4), "hewo"); Assert.assertEquals(ss.getStringOfMaxLength(0), ""); }
@Test public void testEdgeCases() { StringShortener ss = new StringShortener(); ss.separator(null); Assert.assertEquals(ss.getStringOfMaxLength(4), ""); ss.append("1", "hello"); Assert.assertEquals(ss.getStringOfMaxLength(8), "hello"); Assert.assertEquals(ss.getStringOfMaxLength(4), "hell"); ss.append("2", "world"); ss.append("3", null); Assert.assertEquals(ss.getStringOfMaxLength(15), "helloworld"); Assert.assertEquals(ss.getStringOfMaxLength(8), "hellowor"); ss.canTruncate("1", 2); Assert.assertEquals(ss.getStringOfMaxLength(8), "helworld"); Assert.assertEquals(ss.getStringOfMaxLength(5), "hewor"); Assert.assertEquals(ss.getStringOfMaxLength(2), "he"); Assert.assertEquals(ss.getStringOfMaxLength(0), ""); }
public StringShortener append(String id, String text) { text = removeDisallowedCharacters(text); String old = wordsByIdInOrder.put(id, text); if (old!=null) { throw new IllegalStateException("Cannot append with id '"+id+"' when id already present"); } // TODO expose a replace or update return this; }